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);
186 ff69268e 2020-12-13 stsp __dead static void
187 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
189 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
195 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
198 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
199 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
200 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
201 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
204 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
206 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 5c860e29 2018-03-12 stsp switch (ch) {
215 6879ba42 2020-10-01 naddy usage(hflag, 1);
216 5c860e29 2018-03-12 stsp /* NOTREACHED */
220 5c860e29 2018-03-12 stsp argc -= optind;
221 5c860e29 2018-03-12 stsp argv += optind;
222 9814e6a3 2020-09-27 naddy optind = 1;
223 9814e6a3 2020-09-27 naddy optreset = 1;
225 53ccebc2 2019-07-30 stsp if (Vflag) {
226 53ccebc2 2019-07-30 stsp got_version_print_str();
230 5c860e29 2018-03-12 stsp if (argc <= 0)
231 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
233 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
234 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
236 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
237 d7d4f210 2018-03-12 stsp const struct got_error *error;
239 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
241 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
246 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
248 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
249 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
250 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
251 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
252 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 70015d7a 2019-11-08 stsp !(sigint_received &&
254 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
262 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 6879ba42 2020-10-01 naddy list_commands(stderr);
267 4ed7e80c 2018-05-20 stsp __dead static void
268 6879ba42 2020-10-01 naddy usage(int hflag, int status)
270 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
272 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 53ccebc2 2019-07-30 stsp getprogname());
275 6879ba42 2020-10-01 naddy list_commands(fp);
276 6879ba42 2020-10-01 naddy exit(status);
279 0266afb7 2019-01-04 stsp static const struct got_error *
280 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
282 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
283 e2ba3d07 2019-05-13 stsp const char *editor;
285 8920fa04 2019-08-18 stsp *abspath = NULL;
287 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
288 e2ba3d07 2019-05-13 stsp if (editor == NULL)
289 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
291 0ee7065d 2019-05-13 stsp if (editor) {
292 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
294 0ee7065d 2019-05-13 stsp return err;
297 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
298 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
299 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
300 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
303 e2ba3d07 2019-05-13 stsp return NULL;
306 e2ba3d07 2019-05-13 stsp static const struct got_error *
307 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
308 c530dc23 2019-07-23 stsp const char *worktree_path)
310 163ce85a 2019-05-13 stsp const struct got_error *err;
312 37c06ea4 2019-07-15 stsp #ifdef PROFILE
313 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
314 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
316 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
319 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
322 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
326 163ce85a 2019-05-13 stsp if (err != NULL)
327 163ce85a 2019-05-13 stsp return err;
329 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
330 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
332 0266afb7 2019-01-04 stsp return NULL;
335 2c7829a4 2019-06-17 stsp __dead static void
336 2c7829a4 2019-06-17 stsp usage_init(void)
338 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 2c7829a4 2019-06-17 stsp static const struct got_error *
343 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
345 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
346 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
349 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
350 2c7829a4 2019-06-17 stsp switch (ch) {
352 2c7829a4 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp /* NOTREACHED */
357 2c7829a4 2019-06-17 stsp argc -= optind;
358 2c7829a4 2019-06-17 stsp argv += optind;
360 2c7829a4 2019-06-17 stsp #ifndef PROFILE
361 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 2c7829a4 2019-06-17 stsp err(1, "pledge");
364 2c7829a4 2019-06-17 stsp if (argc != 1)
365 bc20e173 2019-06-17 stsp usage_init();
367 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
368 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
369 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
371 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
373 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
374 2c7829a4 2019-06-17 stsp if (error &&
375 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
382 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
384 3ce1b845 2019-07-15 stsp free(repo_path);
385 3ce1b845 2019-07-15 stsp return error;
388 3ce1b845 2019-07-15 stsp __dead static void
389 3ce1b845 2019-07-15 stsp usage_import(void)
391 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
400 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
401 3ce1b845 2019-07-15 stsp int st = -1;
403 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
404 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
405 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
407 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
409 3ce1b845 2019-07-15 stsp goto doneediting;
411 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
412 3ce1b845 2019-07-15 stsp _exit(127);
415 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
416 3ce1b845 2019-07-15 stsp if (errno != EINTR)
419 3ce1b845 2019-07-15 stsp doneediting:
420 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
421 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
422 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
424 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
425 3ce1b845 2019-07-15 stsp errno = EINTR;
429 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
432 3ce1b845 2019-07-15 stsp static const struct got_error *
433 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
435 28cf319f 2021-01-28 stsp int require_modification)
437 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
438 bfa12d5e 2020-09-26 stsp char *line = NULL;
439 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
440 bfa12d5e 2020-09-26 stsp ssize_t linelen;
441 3ce1b845 2019-07-15 stsp struct stat st, st2;
442 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
443 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
444 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
448 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
449 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
451 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
454 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
457 28cf319f 2021-01-28 stsp if (require_modification &&
458 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
459 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
463 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
464 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
465 bfa12d5e 2020-09-26 stsp * has in fact been edited.
467 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
468 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
469 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
470 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
472 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
473 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
474 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
479 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
480 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
481 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
482 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
483 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
484 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
485 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
489 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
490 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
494 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
495 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
496 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
497 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
498 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
500 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
501 3ce1b845 2019-07-15 stsp if (fp == NULL) {
502 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
507 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
508 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
509 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
510 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
511 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
512 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
516 bfa12d5e 2020-09-26 stsp free(line);
517 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
518 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
521 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
522 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
526 bfa12d5e 2020-09-26 stsp if (len == 0) {
527 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
528 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
531 28cf319f 2021-01-28 stsp if (require_modification &&
532 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
533 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
534 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
536 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
538 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
539 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
541 3ce1b845 2019-07-15 stsp free(*logmsg);
542 3ce1b845 2019-07-15 stsp *logmsg = NULL;
544 3ce1b845 2019-07-15 stsp return err;
547 3ce1b845 2019-07-15 stsp static const struct got_error *
548 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
549 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
551 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
552 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
553 1601cb9f 2020-09-11 naddy int initial_content_len;
554 97972933 2020-09-11 stsp int fd = -1;
556 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
557 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
558 1601cb9f 2020-09-11 naddy branch_name);
559 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
560 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
562 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
563 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
567 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
568 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
572 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
573 0d5bb276 2020-12-15 stsp initial_content_len, 1);
575 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
576 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
577 3ce1b845 2019-07-15 stsp free(initial_content);
579 59f86c76 2020-09-11 stsp free(*logmsg_path);
580 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
582 3ce1b845 2019-07-15 stsp return err;
585 3ce1b845 2019-07-15 stsp static const struct got_error *
586 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
588 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
589 3ce1b845 2019-07-15 stsp return NULL;
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
594 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
596 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
597 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
598 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
600 84792843 2019-08-09 stsp *author = NULL;
602 50b0790e 2020-09-11 stsp if (worktree)
603 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
604 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
607 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
608 50b0790e 2020-09-11 stsp * significant to least significant:
609 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
610 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
611 50b0790e 2020-09-11 stsp * 3) repository's git config file
612 50b0790e 2020-09-11 stsp * 4) environment variables
613 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
616 50b0790e 2020-09-11 stsp if (worktree_conf)
617 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
618 50b0790e 2020-09-11 stsp if (got_author == NULL)
619 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
620 84792843 2019-08-09 stsp if (got_author == NULL) {
621 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
622 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
623 c9956ddf 2019-09-08 stsp if (name && email) {
624 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
625 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
626 c9956ddf 2019-09-08 stsp return NULL;
629 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
630 257add31 2020-09-09 stsp if (got_author == NULL) {
631 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
632 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
634 257add31 2020-09-09 stsp if (name && email) {
635 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
637 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
638 257add31 2020-09-09 stsp return NULL;
640 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
641 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
645 aba9c984 2019-09-08 stsp *author = strdup(got_author);
646 aba9c984 2019-09-08 stsp if (*author == NULL)
647 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
650 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
651 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
653 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
654 84792843 2019-08-09 stsp got_author++;
655 aba9c984 2019-09-08 stsp if (*got_author != '<') {
656 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
659 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
660 84792843 2019-08-09 stsp got_author++;
661 aba9c984 2019-09-08 stsp if (*got_author != '@') {
662 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
665 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
666 84792843 2019-08-09 stsp got_author++;
667 84792843 2019-08-09 stsp if (*got_author != '>')
668 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
671 aba9c984 2019-09-08 stsp free(*author);
672 aba9c984 2019-09-08 stsp *author = NULL;
674 aba9c984 2019-09-08 stsp return err;
677 c9956ddf 2019-09-08 stsp static const struct got_error *
678 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
680 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
682 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
683 c9956ddf 2019-09-08 stsp if (homedir) {
684 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
685 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
688 c9956ddf 2019-09-08 stsp return NULL;
691 84792843 2019-08-09 stsp static const struct got_error *
692 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
694 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
695 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
696 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
697 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
698 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
699 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
700 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
701 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
703 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
704 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
705 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
707 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
709 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
710 3ce1b845 2019-07-15 stsp switch (ch) {
712 3ce1b845 2019-07-15 stsp branch_name = optarg;
715 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
716 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
717 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
722 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
723 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
724 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
730 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
732 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
738 b2b65d18 2019-08-22 stsp usage_import();
739 3ce1b845 2019-07-15 stsp /* NOTREACHED */
743 3ce1b845 2019-07-15 stsp argc -= optind;
744 3ce1b845 2019-07-15 stsp argv += optind;
746 3ce1b845 2019-07-15 stsp #ifndef PROFILE
747 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
749 3ce1b845 2019-07-15 stsp NULL) == -1)
750 3ce1b845 2019-07-15 stsp err(1, "pledge");
752 3ce1b845 2019-07-15 stsp if (argc != 1)
753 3ce1b845 2019-07-15 stsp usage_import();
755 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
756 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
757 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
758 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
760 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
761 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
764 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
768 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
770 aba9c984 2019-09-08 stsp return error;
773 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
774 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
775 e560b7e0 2019-11-28 stsp * an unintended typo.
777 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
778 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
780 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
781 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
785 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
786 3ce1b845 2019-07-15 stsp if (error) {
787 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
790 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
791 3ce1b845 2019-07-15 stsp "import target branch already exists");
795 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
796 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
797 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
800 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
803 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
804 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
806 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
807 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
810 8e158b01 2019-09-22 stsp free(logmsg);
811 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
812 ef293bdd 2019-10-21 stsp path_dir, refname);
813 ef293bdd 2019-10-21 stsp if (error) {
814 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
815 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
822 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
823 ef293bdd 2019-10-21 stsp if (logmsg_path)
824 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
828 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
829 ef293bdd 2019-10-21 stsp if (error) {
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
835 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
836 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
837 ef293bdd 2019-10-21 stsp if (error) {
838 ef293bdd 2019-10-21 stsp if (logmsg_path)
839 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
843 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
844 ef293bdd 2019-10-21 stsp if (error) {
845 ef293bdd 2019-10-21 stsp if (logmsg_path)
846 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
850 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
851 ef293bdd 2019-10-21 stsp if (error) {
852 ef293bdd 2019-10-21 stsp if (logmsg_path)
853 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
857 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
858 ef293bdd 2019-10-21 stsp if (error) {
859 ef293bdd 2019-10-21 stsp if (logmsg_path)
860 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
864 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
865 3ce1b845 2019-07-15 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
867 ef293bdd 2019-10-21 stsp if (logmsg_path)
868 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
872 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
873 3ce1b845 2019-07-15 stsp branch_ref);
874 ef293bdd 2019-10-21 stsp if (error) {
875 ef293bdd 2019-10-21 stsp if (logmsg_path)
876 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
880 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
881 ef293bdd 2019-10-21 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (logmsg_path)
883 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
888 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
889 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
891 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
892 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
893 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
894 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
895 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
896 8e158b01 2019-09-22 stsp free(logmsg);
897 ef293bdd 2019-10-21 stsp free(logmsg_path);
898 2c7829a4 2019-06-17 stsp free(repo_path);
899 3ce1b845 2019-07-15 stsp free(editor);
900 3ce1b845 2019-07-15 stsp free(refname);
901 3ce1b845 2019-07-15 stsp free(new_commit_id);
902 3ce1b845 2019-07-15 stsp free(id_str);
903 aba9c984 2019-09-08 stsp free(author);
904 c9956ddf 2019-09-08 stsp free(gitconfig_path);
905 3ce1b845 2019-07-15 stsp if (branch_ref)
906 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
907 3ce1b845 2019-07-15 stsp if (head_ref)
908 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
909 2c7829a4 2019-06-17 stsp return error;
912 93658fb9 2020-03-18 stsp __dead static void
913 93658fb9 2020-03-18 stsp usage_clone(void)
915 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
916 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
920 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
921 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
922 892ac3b6 2020-03-18 stsp int last_p_indexed;
923 892ac3b6 2020-03-18 stsp int last_p_resolved;
924 68999b92 2020-03-18 stsp int verbosity;
926 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
928 04d9a9ec 2020-09-24 stsp int create_configs;
929 04d9a9ec 2020-09-24 stsp int configs_created;
931 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
932 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
933 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
934 04d9a9ec 2020-09-24 stsp const char *proto;
935 04d9a9ec 2020-09-24 stsp const char *host;
936 04d9a9ec 2020-09-24 stsp const char *port;
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
938 04d9a9ec 2020-09-24 stsp const char *git_url;
939 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
940 04d9a9ec 2020-09-24 stsp int mirror_references;
941 04d9a9ec 2020-09-24 stsp } config_info;
944 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
945 93658fb9 2020-03-18 stsp static const struct got_error *
946 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
947 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
948 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
949 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
950 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
952 04d9a9ec 2020-09-24 stsp static const struct got_error *
953 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
954 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
956 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
957 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
958 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
959 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
960 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
963 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
964 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
965 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
966 04d9a9ec 2020-09-24 stsp * we have all required information.
968 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
969 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
970 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
971 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
972 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
973 62d463ca 2020-10-20 naddy a->config_info.git_url,
974 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
975 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
976 62d463ca 2020-10-20 naddy a->config_info.symrefs,
977 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
978 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
980 04d9a9ec 2020-09-24 stsp return err;
981 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
984 68999b92 2020-03-18 stsp if (a->verbosity < 0)
985 68999b92 2020-03-18 stsp return NULL;
987 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
988 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
989 892ac3b6 2020-03-18 stsp fflush(stdout);
990 12d1281e 2020-03-19 stsp return NULL;
993 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
994 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
995 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
996 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
997 892ac3b6 2020-03-18 stsp print_size = 1;
998 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
999 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1000 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1002 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1003 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1004 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1005 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1006 892ac3b6 2020-03-18 stsp print_indexed = 1;
1007 892ac3b6 2020-03-18 stsp print_size = 1;
1010 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1011 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1012 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1013 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1014 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1015 892ac3b6 2020-03-18 stsp print_resolved = 1;
1016 892ac3b6 2020-03-18 stsp print_indexed = 1;
1017 892ac3b6 2020-03-18 stsp print_size = 1;
1022 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1023 892ac3b6 2020-03-18 stsp printf("\r");
1024 892ac3b6 2020-03-18 stsp if (print_size)
1025 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1026 d715f13e 2020-03-19 stsp if (print_indexed)
1027 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1028 d715f13e 2020-03-19 stsp if (print_resolved)
1029 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1030 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1031 892ac3b6 2020-03-18 stsp fflush(stdout);
1033 531c3985 2020-03-18 stsp return NULL;
1036 531c3985 2020-03-18 stsp static const struct got_error *
1037 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1038 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1040 4ba14133 2020-03-20 stsp const struct got_error *err;
1041 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1043 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1045 4ba14133 2020-03-20 stsp return err;
1047 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1048 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1049 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1050 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1052 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1053 4ba14133 2020-03-20 stsp return err;
1056 4ba14133 2020-03-20 stsp static const struct got_error *
1057 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1058 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1060 41b0de12 2020-03-21 stsp const struct got_error *err;
1061 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1063 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1064 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1065 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1067 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1073 41b0de12 2020-03-21 stsp char *id_str;
1075 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1077 41b0de12 2020-03-21 stsp return err;
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1079 41b0de12 2020-03-21 stsp free(id_str);
1082 41b0de12 2020-03-21 stsp return NULL;
1085 6338a6a1 2020-03-21 stsp static const struct got_error *
1086 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1087 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1089 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1090 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1091 6338a6a1 2020-03-21 stsp char *id_str;
1093 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1095 6338a6a1 2020-03-21 stsp return err;
1097 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1099 6338a6a1 2020-03-21 stsp goto done;
1101 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1102 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1104 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1105 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1107 6338a6a1 2020-03-21 stsp free(id_str);
1108 6338a6a1 2020-03-21 stsp return err;
1111 0e4002ca 2020-03-21 stsp static int
1112 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1116 0e4002ca 2020-03-21 stsp refname += 5;
1119 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1120 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1122 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1124 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1127 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1128 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1130 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1131 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1134 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1135 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1138 0e4002ca 2020-03-21 stsp static int
1139 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1141 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1143 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1144 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1151 41b0de12 2020-03-21 stsp static const struct got_error *
1152 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1153 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1155 0e4002ca 2020-03-21 stsp const struct got_error *err;
1156 0e4002ca 2020-03-21 stsp char *remote_refname;
1158 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1159 0e4002ca 2020-03-21 stsp refname += 5;
1161 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1162 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1163 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1165 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1166 0e4002ca 2020-03-21 stsp free(remote_refname);
1167 7c0b7f42 2020-09-24 stsp return err;
1170 7c0b7f42 2020-09-24 stsp static const struct got_error *
1171 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1172 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1173 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1174 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1175 99495ddb 2021-01-10 stsp struct got_repository *repo)
1177 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1178 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1179 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1180 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1181 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1182 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1183 7c0b7f42 2020-09-24 stsp ssize_t n;
1185 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1186 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1187 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1189 132af4a5 2021-01-05 stsp branchname = pe->path;
1190 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1191 132af4a5 2021-01-05 stsp branchname += 11;
1192 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1193 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1194 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1195 132af4a5 2021-01-05 stsp goto done;
1197 132af4a5 2021-01-05 stsp free(branches);
1198 132af4a5 2021-01-05 stsp branches = s;
1200 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1201 15d3c221 2021-01-05 stsp branchname = default_branch;
1202 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1203 15d3c221 2021-01-05 stsp branchname += 11;
1204 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1209 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1210 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1211 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1213 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1214 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1215 99495ddb 2021-01-10 stsp branchname += 5;
1216 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1217 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1218 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1219 99495ddb 2021-01-10 stsp goto done;
1221 99495ddb 2021-01-10 stsp free(refs);
1226 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1227 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1228 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1229 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1230 7c0b7f42 2020-09-24 stsp goto done;
1232 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1233 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1234 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1235 7c0b7f42 2020-09-24 stsp goto done;
1237 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1238 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1239 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1240 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1242 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1248 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1249 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1250 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1251 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1252 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1253 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1254 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1255 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1256 7c0b7f42 2020-09-24 stsp goto done;
1258 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1259 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1260 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1261 7c0b7f42 2020-09-24 stsp goto done;
1265 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1267 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1268 132af4a5 2021-01-05 stsp free(branches);
1269 7c0b7f42 2020-09-24 stsp return err;
1272 7c0b7f42 2020-09-24 stsp static const struct got_error *
1273 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1274 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1275 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1276 99495ddb 2021-01-10 stsp struct got_repository *repo)
1278 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1279 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1280 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1281 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1282 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1283 56d0a753 2021-01-20 stsp const char *branchname;
1284 7c0b7f42 2020-09-24 stsp ssize_t n;
1286 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1287 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1288 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1289 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1290 7c0b7f42 2020-09-24 stsp goto done;
1292 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1293 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1294 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1295 7c0b7f42 2020-09-24 stsp goto done;
1297 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1298 56d0a753 2021-01-20 stsp if (mirror_references) {
1299 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1300 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1301 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1302 56d0a753 2021-01-20 stsp goto done;
1304 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1305 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1306 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1307 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1308 7c0b7f42 2020-09-24 stsp goto done;
1310 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1311 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1312 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1314 132af4a5 2021-01-05 stsp branchname = pe->path;
1315 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1316 132af4a5 2021-01-05 stsp branchname += 11;
1317 56d0a753 2021-01-20 stsp if (mirror_references) {
1318 56d0a753 2021-01-20 stsp if (asprintf(&s,
1319 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1320 56d0a753 2021-01-20 stsp branches ? branches : "",
1321 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1322 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1323 56d0a753 2021-01-20 stsp goto done;
1325 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1327 132af4a5 2021-01-05 stsp branches ? branches : "",
1328 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1329 132af4a5 2021-01-05 stsp branchname) == -1) {
1330 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1331 132af4a5 2021-01-05 stsp goto done;
1333 132af4a5 2021-01-05 stsp free(branches);
1334 132af4a5 2021-01-05 stsp branches = s;
1338 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1339 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1341 04d9a9ec 2020-09-24 stsp if (default_branch) {
1342 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1343 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1344 7c0b7f42 2020-09-24 stsp branchname += 11;
1346 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1347 56d0a753 2021-01-20 stsp if (mirror_references) {
1348 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1349 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1350 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1351 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1352 56d0a753 2021-01-20 stsp goto done;
1354 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1355 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1356 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1357 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1358 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1359 7c0b7f42 2020-09-24 stsp goto done;
1362 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1363 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1364 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1366 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1367 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1368 99495ddb 2021-01-10 stsp refname += 5;
1369 56d0a753 2021-01-20 stsp if (mirror_references) {
1370 56d0a753 2021-01-20 stsp if (asprintf(&s,
1371 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1372 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1373 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1374 56d0a753 2021-01-20 stsp goto done;
1376 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1377 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1378 99495ddb 2021-01-10 stsp refs ? refs : "",
1379 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1380 99495ddb 2021-01-10 stsp refname) == -1) {
1381 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1382 99495ddb 2021-01-10 stsp goto done;
1384 99495ddb 2021-01-10 stsp free(refs);
1389 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1390 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1391 132af4a5 2021-01-05 stsp "\turl = %s\n"
1394 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1395 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1396 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1397 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1398 132af4a5 2021-01-05 stsp goto done;
1400 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1401 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1402 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1403 7c0b7f42 2020-09-24 stsp goto done;
1406 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1407 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1408 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1409 132af4a5 2021-01-05 stsp free(branches);
1410 0e4002ca 2020-03-21 stsp return err;
1413 0e4002ca 2020-03-21 stsp static const struct got_error *
1414 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1415 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1416 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1417 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1418 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1420 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1421 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1422 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1425 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1426 04d9a9ec 2020-09-24 stsp * one of those.
1428 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1429 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1430 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1432 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1433 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1434 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1435 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1437 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1440 04d9a9ec 2020-09-24 stsp default_branch = target;
1445 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1446 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1447 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1448 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1450 04d9a9ec 2020-09-24 stsp return err;
1452 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1453 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1454 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1457 04d9a9ec 2020-09-24 stsp static const struct got_error *
1458 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1460 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1461 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1462 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1463 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1464 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1465 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1466 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1467 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1468 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1469 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1470 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1471 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1472 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1473 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1474 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1476 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1477 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1478 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1479 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1481 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1482 93658fb9 2020-03-18 stsp switch (ch) {
1484 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1487 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1488 4ba14133 2020-03-20 stsp optarg, NULL);
1489 4ba14133 2020-03-20 stsp if (error)
1490 4ba14133 2020-03-20 stsp return error;
1493 41b0de12 2020-03-21 stsp list_refs_only = 1;
1496 469dd726 2020-03-20 stsp mirror_references = 1;
1499 68999b92 2020-03-18 stsp if (verbosity < 0)
1500 68999b92 2020-03-18 stsp verbosity = 0;
1501 68999b92 2020-03-18 stsp else if (verbosity < 3)
1502 68999b92 2020-03-18 stsp verbosity++;
1505 68999b92 2020-03-18 stsp verbosity = -1;
1508 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1509 0e4002ca 2020-03-21 stsp optarg, NULL);
1510 0e4002ca 2020-03-21 stsp if (error)
1511 0e4002ca 2020-03-21 stsp return error;
1514 93658fb9 2020-03-18 stsp usage_clone();
1518 93658fb9 2020-03-18 stsp argc -= optind;
1519 93658fb9 2020-03-18 stsp argv += optind;
1521 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1522 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1526 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1527 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1528 41b0de12 2020-03-21 stsp if (mirror_references)
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1530 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1534 93658fb9 2020-03-18 stsp uri = argv[0];
1536 9df6f38b 2020-03-18 stsp if (argc == 1)
1537 93658fb9 2020-03-18 stsp dirname = NULL;
1538 9df6f38b 2020-03-18 stsp else if (argc == 2)
1539 93658fb9 2020-03-18 stsp dirname = argv[1];
1541 93658fb9 2020-03-18 stsp usage_clone();
1543 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1544 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1545 39c64a6a 2020-03-18 stsp if (error)
1546 09f63084 2020-03-20 stsp goto done;
1548 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1549 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1550 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1551 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1552 09838ffc 2020-03-18 stsp goto done;
1555 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1556 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1557 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1558 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1559 39c64a6a 2020-03-18 stsp err(1, "pledge");
1561 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1562 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1563 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1564 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1566 39c64a6a 2020-03-18 stsp err(1, "pledge");
1568 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1570 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1571 39c64a6a 2020-03-18 stsp goto done;
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1574 39c64a6a 2020-03-18 stsp goto done;
1576 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1577 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1578 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1579 bb64b798 2020-03-18 stsp goto done;
1581 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1583 bb64b798 2020-03-18 stsp repo_path = dirname;
1585 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1586 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1587 2751fe64 2020-09-24 stsp if (error &&
1588 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1589 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1590 41b0de12 2020-03-21 stsp goto done;
1591 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1592 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1593 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1594 41b0de12 2020-03-21 stsp goto done;
1598 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1599 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1600 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1601 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1602 ee448f5f 2020-03-18 stsp goto done;
1605 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1606 ee448f5f 2020-03-18 stsp if (error)
1607 ee448f5f 2020-03-18 stsp goto done;
1609 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1610 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1611 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1613 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1614 9c52365f 2020-03-21 stsp server_path, verbosity);
1615 39c64a6a 2020-03-18 stsp if (error)
1616 bb64b798 2020-03-18 stsp goto done;
1618 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1619 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1620 2751fe64 2020-09-24 stsp if (error)
1621 2751fe64 2020-09-24 stsp goto done;
1622 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1627 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1628 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1629 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1630 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1631 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1632 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1633 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1634 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1636 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1644 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1645 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1646 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1647 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1648 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1649 39c64a6a 2020-03-18 stsp if (error)
1650 d9b4d0c0 2020-03-18 stsp goto done;
1652 41b0de12 2020-03-21 stsp if (list_refs_only) {
1653 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1654 41b0de12 2020-03-21 stsp goto done;
1657 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1658 39c64a6a 2020-03-18 stsp if (error)
1659 d9b4d0c0 2020-03-18 stsp goto done;
1660 68999b92 2020-03-18 stsp if (verbosity >= 0)
1661 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1662 d9b4d0c0 2020-03-18 stsp free(id_str);
1664 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1665 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1666 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1667 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1668 7ebc0570 2020-03-18 stsp char *remote_refname;
1670 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1671 0e4002ca 2020-03-21 stsp !mirror_references) {
1672 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1673 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1674 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1675 0e4002ca 2020-03-21 stsp if (error)
1676 0e4002ca 2020-03-21 stsp goto done;
1680 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1681 39c64a6a 2020-03-18 stsp if (error)
1682 d9b4d0c0 2020-03-18 stsp goto done;
1684 469dd726 2020-03-20 stsp if (mirror_references)
1687 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1690 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1691 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1692 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1693 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1694 7ebc0570 2020-03-18 stsp goto done;
1696 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1697 f298ae0f 2020-03-25 stsp free(remote_refname);
1698 39c64a6a 2020-03-18 stsp if (error)
1699 d9b4d0c0 2020-03-18 stsp goto done;
1702 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1703 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1704 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1705 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1706 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1707 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1709 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1712 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1713 39c64a6a 2020-03-18 stsp if (error) {
1714 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1715 55330abe 2020-03-20 stsp error = NULL;
1718 d9b4d0c0 2020-03-18 stsp goto done;
1721 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1722 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1723 f298ae0f 2020-03-25 stsp if (error)
1724 f298ae0f 2020-03-25 stsp goto done;
1726 f298ae0f 2020-03-25 stsp if (mirror_references)
1729 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1732 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1733 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1734 f298ae0f 2020-03-25 stsp refname) == -1) {
1735 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1736 f298ae0f 2020-03-25 stsp goto done;
1738 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1739 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1740 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1741 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1742 f298ae0f 2020-03-25 stsp free(remote_refname);
1743 f298ae0f 2020-03-25 stsp goto done;
1745 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1746 f298ae0f 2020-03-25 stsp if (error) {
1747 f298ae0f 2020-03-25 stsp free(remote_refname);
1748 f298ae0f 2020-03-25 stsp free(remote_target);
1749 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1750 f298ae0f 2020-03-25 stsp error = NULL;
1753 f298ae0f 2020-03-25 stsp goto done;
1755 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1756 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1757 f298ae0f 2020-03-25 stsp free(remote_refname);
1758 f298ae0f 2020-03-25 stsp free(remote_target);
1759 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1760 39c64a6a 2020-03-18 stsp if (error)
1761 d9b4d0c0 2020-03-18 stsp goto done;
1763 4ba14133 2020-03-20 stsp if (pe == NULL) {
1765 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1766 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1767 4ba14133 2020-03-20 stsp * which could be fetched instead.
1769 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1770 4ba14133 2020-03-20 stsp const char *target = pe->path;
1771 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1773 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1774 4ba14133 2020-03-20 stsp if (error) {
1775 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1776 4ba14133 2020-03-20 stsp error = NULL;
1779 4ba14133 2020-03-20 stsp goto done;
1782 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1783 04d9a9ec 2020-09-24 stsp verbosity, repo);
1784 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1785 4ba14133 2020-03-20 stsp if (error)
1786 4ba14133 2020-03-20 stsp goto done;
1791 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1792 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1793 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1795 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1796 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1797 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1798 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1799 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1801 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1802 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1804 bb64b798 2020-03-18 stsp got_repo_close(repo);
1805 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1806 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1807 d9b4d0c0 2020-03-18 stsp free(pe->data);
1809 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1810 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1811 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1812 d9b4d0c0 2020-03-18 stsp free(pe->data);
1814 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1815 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1816 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1817 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1818 09838ffc 2020-03-18 stsp free(proto);
1819 09838ffc 2020-03-18 stsp free(host);
1820 09838ffc 2020-03-18 stsp free(port);
1821 09838ffc 2020-03-18 stsp free(server_path);
1822 09838ffc 2020-03-18 stsp free(repo_name);
1823 bb64b798 2020-03-18 stsp free(default_destdir);
1824 b46f3e71 2020-03-18 stsp free(git_url);
1825 39c64a6a 2020-03-18 stsp return error;
1828 7848a0e1 2020-03-19 stsp static const struct got_error *
1829 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1830 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1832 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1833 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1834 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1836 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1838 7848a0e1 2020-03-19 stsp goto done;
1840 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1841 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1842 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1844 88609724 2020-03-21 stsp goto done;
1845 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1846 88609724 2020-03-21 stsp goto done;
1847 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1848 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1849 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1851 db6d8ad8 2020-03-21 stsp goto done;
1854 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1855 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1856 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1857 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1858 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1860 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1862 7848a0e1 2020-03-19 stsp goto done;
1863 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1865 e8a967e0 2020-03-21 stsp goto done;
1867 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1869 7848a0e1 2020-03-19 stsp goto done;
1870 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1871 6338a6a1 2020-03-21 stsp goto done;
1873 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1875 6338a6a1 2020-03-21 stsp goto done;
1876 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1878 6338a6a1 2020-03-21 stsp goto done;
1881 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1882 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1883 6338a6a1 2020-03-21 stsp new_id_str);
1885 7848a0e1 2020-03-19 stsp free(old_id);
1886 7848a0e1 2020-03-19 stsp free(new_id_str);
1887 7848a0e1 2020-03-19 stsp return err;
1890 f1bcca34 2020-03-25 stsp static const struct got_error *
1891 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1892 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1894 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1895 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1896 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1898 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1899 bcf34b0e 2020-03-26 stsp if (err) {
1900 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1901 bcf34b0e 2020-03-26 stsp return err;
1902 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1904 bcf34b0e 2020-03-26 stsp goto done;
1906 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1908 bcf34b0e 2020-03-26 stsp goto done;
1910 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1911 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1912 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1913 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1915 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1917 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1918 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1919 bcf34b0e 2020-03-26 stsp goto done;
1921 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1922 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1924 bcf34b0e 2020-03-26 stsp goto done;
1926 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1928 bcf34b0e 2020-03-26 stsp goto done;
1930 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1931 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1932 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1936 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1937 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1938 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1939 bcf34b0e 2020-03-26 stsp err = unlock_err;
1941 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1942 f1bcca34 2020-03-25 stsp return err;
1945 2ab43947 2020-03-18 stsp __dead static void
1946 7848a0e1 2020-03-19 stsp usage_fetch(void)
1948 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1949 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1950 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1951 13f12b09 2020-03-21 stsp getprogname());
1955 7848a0e1 2020-03-19 stsp static const struct got_error *
1956 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1957 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1959 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1960 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1961 3789fd73 2020-03-26 stsp char *id_str = NULL;
1963 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1964 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1966 3789fd73 2020-03-26 stsp return err;
1967 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1968 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1969 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1970 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1973 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1975 3789fd73 2020-03-26 stsp return err;
1976 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1978 3789fd73 2020-03-26 stsp goto done;
1980 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1982 3789fd73 2020-03-26 stsp goto done;
1983 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1984 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1985 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1990 3789fd73 2020-03-26 stsp free(id_str);
1991 3789fd73 2020-03-26 stsp return NULL;
1994 3789fd73 2020-03-26 stsp static const struct got_error *
1995 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1996 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1997 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1998 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2000 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2001 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2002 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2003 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2004 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2005 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2007 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2009 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2011 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2013 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2015 3789fd73 2020-03-26 stsp goto done;
2017 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2018 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2020 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2021 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2022 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2023 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2024 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2026 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2027 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2028 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2029 3789fd73 2020-03-26 stsp goto done;
2031 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2035 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2036 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2039 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2042 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2043 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2046 3789fd73 2020-03-26 stsp if (pe != NULL)
2049 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2053 3789fd73 2020-03-26 stsp if (local_refname) {
2054 3789fd73 2020-03-26 stsp struct got_reference *ref;
2055 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2056 3789fd73 2020-03-26 stsp if (err) {
2057 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2059 3789fd73 2020-03-26 stsp free(local_refname);
2060 3789fd73 2020-03-26 stsp local_refname = NULL;
2063 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2066 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2067 3789fd73 2020-03-26 stsp got_ref_close(ref);
2068 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2069 3789fd73 2020-03-26 stsp err = unlock_err;
2073 3789fd73 2020-03-26 stsp free(local_refname);
2074 3789fd73 2020-03-26 stsp local_refname = NULL;
2078 3789fd73 2020-03-26 stsp free(remote_namespace);
2079 3789fd73 2020-03-26 stsp free(local_refname);
2080 0e4002ca 2020-03-21 stsp return err;
2083 0e4002ca 2020-03-21 stsp static const struct got_error *
2084 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2085 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2087 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2088 0e4002ca 2020-03-21 stsp char *remote_refname;
2089 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2091 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2092 0e4002ca 2020-03-21 stsp refname += 5;
2094 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2095 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2096 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2098 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2099 0e4002ca 2020-03-21 stsp if (err) {
2100 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2101 0e4002ca 2020-03-21 stsp goto done;
2102 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2104 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2105 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2106 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2107 9f142382 2020-03-21 stsp err = unlock_err;
2108 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2111 0e4002ca 2020-03-21 stsp free(remote_refname);
2112 f21ec2f0 2020-03-21 stsp return err;
2115 f21ec2f0 2020-03-21 stsp static const struct got_error *
2116 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2118 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2119 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2120 7848a0e1 2020-03-19 stsp const char *remote_name;
2121 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2122 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2123 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2124 7848a0e1 2020-03-19 stsp int nremotes;
2125 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2126 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2127 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2128 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2129 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2130 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2131 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2132 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2133 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2134 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2135 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2136 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2138 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2139 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2140 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2141 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2143 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2144 7848a0e1 2020-03-19 stsp switch (ch) {
2146 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2149 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2150 4ba14133 2020-03-20 stsp optarg, NULL);
2151 4ba14133 2020-03-20 stsp if (error)
2152 4ba14133 2020-03-20 stsp return error;
2155 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2158 41b0de12 2020-03-21 stsp list_refs_only = 1;
2161 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2162 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2163 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2165 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2168 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2171 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2172 7848a0e1 2020-03-19 stsp verbosity = 0;
2173 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2174 7848a0e1 2020-03-19 stsp verbosity++;
2177 7848a0e1 2020-03-19 stsp verbosity = -1;
2180 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2181 0e4002ca 2020-03-21 stsp optarg, NULL);
2182 0e4002ca 2020-03-21 stsp if (error)
2183 0e4002ca 2020-03-21 stsp return error;
2186 7848a0e1 2020-03-19 stsp usage_fetch();
2190 7848a0e1 2020-03-19 stsp argc -= optind;
2191 7848a0e1 2020-03-19 stsp argv += optind;
2193 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2194 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2195 41b0de12 2020-03-21 stsp if (list_refs_only) {
2196 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2197 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2198 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2199 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2200 f21ec2f0 2020-03-21 stsp if (delete_refs)
2201 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2204 7848a0e1 2020-03-19 stsp if (argc == 0)
2205 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2206 7848a0e1 2020-03-19 stsp else if (argc == 1)
2207 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2209 7848a0e1 2020-03-19 stsp usage_fetch();
2211 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2212 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2213 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2214 7848a0e1 2020-03-19 stsp goto done;
2217 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2218 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2219 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2220 7848a0e1 2020-03-19 stsp goto done;
2222 7848a0e1 2020-03-19 stsp error = NULL;
2223 7848a0e1 2020-03-19 stsp if (worktree) {
2224 7848a0e1 2020-03-19 stsp repo_path =
2225 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2226 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2227 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2228 7848a0e1 2020-03-19 stsp if (error)
2229 7848a0e1 2020-03-19 stsp goto done;
2231 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2232 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2233 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2234 7848a0e1 2020-03-19 stsp goto done;
2239 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2240 7848a0e1 2020-03-19 stsp if (error)
2241 7848a0e1 2020-03-19 stsp goto done;
2243 50b0790e 2020-09-11 stsp if (worktree) {
2244 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2245 50b0790e 2020-09-11 stsp if (worktree_conf) {
2246 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2247 50b0790e 2020-09-11 stsp worktree_conf);
2248 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2249 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2250 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2256 50b0790e 2020-09-11 stsp if (remote == NULL) {
2257 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2258 50b0790e 2020-09-11 stsp if (repo_conf) {
2259 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2260 50b0790e 2020-09-11 stsp repo_conf);
2261 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2262 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2263 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2269 50b0790e 2020-09-11 stsp if (remote == NULL) {
2270 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2271 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2272 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2273 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2278 50b0790e 2020-09-11 stsp if (remote == NULL) {
2279 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2280 50b0790e 2020-09-11 stsp goto done;
2283 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2284 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2285 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2286 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2287 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2288 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2291 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2292 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2293 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2294 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2298 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2299 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2300 7848a0e1 2020-03-19 stsp if (error)
2301 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2304 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2305 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2306 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2307 7848a0e1 2020-03-19 stsp err(1, "pledge");
2309 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2310 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2311 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2312 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2313 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2314 7848a0e1 2020-03-19 stsp err(1, "pledge");
2316 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2317 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2318 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2319 7848a0e1 2020-03-19 stsp goto done;
2321 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2322 7848a0e1 2020-03-19 stsp goto done;
2325 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2326 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2327 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2328 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2329 7848a0e1 2020-03-19 stsp goto done;
2332 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2333 7848a0e1 2020-03-19 stsp if (error)
2334 7848a0e1 2020-03-19 stsp goto done;
2336 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2337 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2338 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2340 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2341 9c52365f 2020-03-21 stsp server_path, verbosity);
2342 7848a0e1 2020-03-19 stsp if (error)
2343 7848a0e1 2020-03-19 stsp goto done;
2345 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2346 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2347 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2348 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2349 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2350 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2351 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2352 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2353 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2354 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2355 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2356 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2357 7848a0e1 2020-03-19 stsp if (error)
2358 7848a0e1 2020-03-19 stsp goto done;
2360 41b0de12 2020-03-21 stsp if (list_refs_only) {
2361 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2362 41b0de12 2020-03-21 stsp goto done;
2365 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2366 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2367 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2368 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2369 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2370 984065c8 2020-03-19 stsp if (error)
2371 984065c8 2020-03-19 stsp goto done;
2372 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2373 984065c8 2020-03-19 stsp free(id_str);
2374 984065c8 2020-03-19 stsp id_str = NULL;
2377 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2378 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2379 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2380 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2381 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2382 7848a0e1 2020-03-19 stsp char *remote_refname;
2384 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2385 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2386 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2387 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2388 0e4002ca 2020-03-21 stsp if (error)
2389 0e4002ca 2020-03-21 stsp goto done;
2393 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2394 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2395 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2396 7848a0e1 2020-03-19 stsp if (error) {
2397 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2398 7848a0e1 2020-03-19 stsp goto done;
2399 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2401 7848a0e1 2020-03-19 stsp if (error)
2402 7848a0e1 2020-03-19 stsp goto done;
2404 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2405 db6d8ad8 2020-03-21 stsp verbosity, repo);
2406 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2407 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2408 9f142382 2020-03-21 stsp error = unlock_err;
2409 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2410 7848a0e1 2020-03-19 stsp if (error)
2411 7848a0e1 2020-03-19 stsp goto done;
2413 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2414 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2415 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2416 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2417 7848a0e1 2020-03-19 stsp goto done;
2420 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2421 7848a0e1 2020-03-19 stsp if (error) {
2422 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2423 7848a0e1 2020-03-19 stsp goto done;
2424 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2425 688f11b3 2020-03-21 stsp verbosity, repo);
2426 7848a0e1 2020-03-19 stsp if (error)
2427 7848a0e1 2020-03-19 stsp goto done;
2429 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2430 db6d8ad8 2020-03-21 stsp verbosity, repo);
2431 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2432 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2433 9f142382 2020-03-21 stsp error = unlock_err;
2434 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2435 7848a0e1 2020-03-19 stsp if (error)
2436 7848a0e1 2020-03-19 stsp goto done;
2439 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2440 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2441 2ec30c80 2020-03-20 stsp if (error) {
2442 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2443 2ec30c80 2020-03-20 stsp goto done;
2444 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2446 2ec30c80 2020-03-20 stsp if (error)
2447 2ec30c80 2020-03-20 stsp goto done;
2449 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2450 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2451 9f142382 2020-03-21 stsp error = unlock_err;
2452 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2456 f1bcca34 2020-03-25 stsp if (delete_refs) {
2457 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2458 3789fd73 2020-03-26 stsp verbosity, repo);
2459 f1bcca34 2020-03-25 stsp if (error)
2460 f1bcca34 2020-03-25 stsp goto done;
2463 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2464 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2465 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2466 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2467 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2468 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2469 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2471 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2474 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2477 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2478 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2479 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2480 f1bcca34 2020-03-25 stsp goto done;
2482 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2483 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2484 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2485 f1bcca34 2020-03-25 stsp free(remote_refname);
2486 f1bcca34 2020-03-25 stsp goto done;
2489 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2491 f1bcca34 2020-03-25 stsp if (error) {
2492 f1bcca34 2020-03-25 stsp free(remote_refname);
2493 f1bcca34 2020-03-25 stsp free(remote_target);
2494 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2495 f1bcca34 2020-03-25 stsp error = NULL;
2498 f1bcca34 2020-03-25 stsp goto done;
2500 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2501 f1bcca34 2020-03-25 stsp verbosity, repo);
2502 f1bcca34 2020-03-25 stsp free(remote_refname);
2503 f1bcca34 2020-03-25 stsp free(remote_target);
2504 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2505 f1bcca34 2020-03-25 stsp if (error)
2506 f1bcca34 2020-03-25 stsp goto done;
2510 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2511 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2512 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2513 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2514 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2516 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2517 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2519 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2520 7848a0e1 2020-03-19 stsp if (worktree)
2521 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2522 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2523 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2524 7848a0e1 2020-03-19 stsp free(pe->data);
2526 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2527 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2528 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2529 7848a0e1 2020-03-19 stsp free(pe->data);
2531 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2532 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2533 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2534 7848a0e1 2020-03-19 stsp free(id_str);
2535 7848a0e1 2020-03-19 stsp free(cwd);
2536 7848a0e1 2020-03-19 stsp free(repo_path);
2537 7848a0e1 2020-03-19 stsp free(pack_hash);
2538 7848a0e1 2020-03-19 stsp free(proto);
2539 7848a0e1 2020-03-19 stsp free(host);
2540 7848a0e1 2020-03-19 stsp free(port);
2541 7848a0e1 2020-03-19 stsp free(server_path);
2542 7848a0e1 2020-03-19 stsp free(repo_name);
2543 7848a0e1 2020-03-19 stsp return error;
2547 7848a0e1 2020-03-19 stsp __dead static void
2548 2ab43947 2020-03-18 stsp usage_checkout(void)
2550 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2551 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2555 2ab43947 2020-03-18 stsp static void
2556 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2558 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2559 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2560 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2561 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2562 2ab43947 2020-03-18 stsp getprogname());
2565 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2566 2ab43947 2020-03-18 stsp const char *worktree_path;
2567 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2570 2ab43947 2020-03-18 stsp static const struct got_error *
2571 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2573 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2575 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2576 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2577 2ab43947 2020-03-18 stsp return NULL;
2579 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2580 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2581 2ab43947 2020-03-18 stsp return NULL;
2584 2ab43947 2020-03-18 stsp while (path[0] == '/')
2587 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2588 2ab43947 2020-03-18 stsp return NULL;
2591 2ab43947 2020-03-18 stsp static const struct got_error *
2592 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2594 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2595 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2596 2ab43947 2020-03-18 stsp return NULL;
2599 2ab43947 2020-03-18 stsp static const struct got_error *
2600 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2601 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2602 2ab43947 2020-03-18 stsp struct got_repository *repo)
2604 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2605 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2607 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2608 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2610 2ab43947 2020-03-18 stsp return err;
2612 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2613 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2616 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2617 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2619 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2621 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2629 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2630 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2631 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2633 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2634 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2635 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2636 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2637 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2638 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2640 2ab43947 2020-03-18 stsp free(yca_id);
2641 2ab43947 2020-03-18 stsp return NULL;
2644 2ab43947 2020-03-18 stsp static const struct got_error *
2645 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2646 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2647 2ab43947 2020-03-18 stsp struct got_repository *repo)
2649 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2650 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2651 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2652 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2654 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2656 2ab43947 2020-03-18 stsp goto done;
2658 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2659 2ab43947 2020-03-18 stsp is_same_branch = 1;
2660 2ab43947 2020-03-18 stsp goto done;
2662 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2663 2ab43947 2020-03-18 stsp is_same_branch = 1;
2664 2ab43947 2020-03-18 stsp goto done;
2667 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2669 2ab43947 2020-03-18 stsp goto done;
2671 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2672 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2674 2ab43947 2020-03-18 stsp goto done;
2676 2ab43947 2020-03-18 stsp for (;;) {
2677 2ab43947 2020-03-18 stsp struct got_object_id *id;
2678 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2679 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2680 2ab43947 2020-03-18 stsp if (err) {
2681 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2682 2ab43947 2020-03-18 stsp err = NULL;
2687 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2689 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2690 2ab43947 2020-03-18 stsp is_same_branch = 1;
2696 2ab43947 2020-03-18 stsp if (graph)
2697 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2698 2ab43947 2020-03-18 stsp free(head_commit_id);
2699 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2700 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2701 2ab43947 2020-03-18 stsp return err;
2704 4ed7e80c 2018-05-20 stsp static const struct got_error *
2705 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2707 4b6c9460 2020-03-05 stsp static char msg[512];
2708 4b6c9460 2020-03-05 stsp const char *branch_name;
2710 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2711 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2713 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2715 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2716 4b6c9460 2020-03-05 stsp branch_name += 11;
2718 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2719 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2720 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2721 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2722 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2723 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2725 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2728 4b6c9460 2020-03-05 stsp static const struct got_error *
2729 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2731 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2732 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2733 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2734 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2735 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2736 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2737 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2738 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2739 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2740 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2741 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2742 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2743 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2745 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2747 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2748 0bb8a95e 2018-03-12 stsp switch (ch) {
2750 08573d5b 2019-05-14 stsp branch_name = optarg;
2753 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2754 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2758 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2761 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2764 2deda0b9 2019-03-07 stsp usage_checkout();
2765 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2769 0bb8a95e 2018-03-12 stsp argc -= optind;
2770 0bb8a95e 2018-03-12 stsp argv += optind;
2772 6715a751 2018-03-16 stsp #ifndef PROFILE
2773 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2774 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2775 c09a553d 2018-03-12 stsp err(1, "pledge");
2777 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2778 c47340da 2020-09-20 stsp char *base, *dotgit;
2779 f0207f6a 2020-10-19 stsp const char *path;
2780 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2781 76089277 2018-04-01 stsp if (repo_path == NULL)
2782 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2783 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2784 76089277 2018-04-01 stsp if (cwd == NULL) {
2785 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2786 76089277 2018-04-01 stsp goto done;
2788 c47340da 2020-09-20 stsp if (path_prefix[0])
2789 f0207f6a 2020-10-19 stsp path = path_prefix;
2791 f0207f6a 2020-10-19 stsp path = repo_path;
2792 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2793 f0207f6a 2020-10-19 stsp if (error)
2794 c47340da 2020-09-20 stsp goto done;
2795 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2796 c09a553d 2018-03-12 stsp if (dotgit)
2797 c09a553d 2018-03-12 stsp *dotgit = '\0';
2798 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2799 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2800 f0207f6a 2020-10-19 stsp free(base);
2801 76089277 2018-04-01 stsp goto done;
2803 f0207f6a 2020-10-19 stsp free(base);
2804 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2805 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2806 76089277 2018-04-01 stsp if (repo_path == NULL) {
2807 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2808 76089277 2018-04-01 stsp goto done;
2810 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2811 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2812 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2813 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2815 b4b3a7dd 2019-07-22 stsp goto done;
2817 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2818 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2819 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2820 b4b3a7dd 2019-07-22 stsp goto done;
2824 c09a553d 2018-03-12 stsp usage_checkout();
2826 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2827 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2829 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2830 c09a553d 2018-03-12 stsp if (error != NULL)
2831 c02c541e 2019-03-29 stsp goto done;
2833 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2834 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2835 c530dc23 2019-07-23 stsp if (error) {
2836 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2837 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2838 c530dc23 2019-07-23 stsp goto done;
2839 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2840 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2841 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2842 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2843 c530dc23 2019-07-23 stsp goto done;
2847 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2848 c02c541e 2019-03-29 stsp if (error)
2849 c09a553d 2018-03-12 stsp goto done;
2851 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2852 c09a553d 2018-03-12 stsp if (error != NULL)
2853 c09a553d 2018-03-12 stsp goto done;
2855 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2856 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2857 c09a553d 2018-03-12 stsp goto done;
2859 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2860 c09a553d 2018-03-12 stsp if (error != NULL)
2861 c09a553d 2018-03-12 stsp goto done;
2863 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2864 e5dc7198 2018-12-29 stsp path_prefix);
2865 e5dc7198 2018-12-29 stsp if (error != NULL)
2866 e5dc7198 2018-12-29 stsp goto done;
2867 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2868 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2869 49520a32 2018-12-29 stsp goto done;
2872 8069f636 2019-01-12 stsp if (commit_id_str) {
2873 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2874 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2875 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2876 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2878 84de9106 2020-12-26 stsp if (error)
2879 84de9106 2020-12-26 stsp goto done;
2880 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2881 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2882 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2883 30837e32 2019-07-25 stsp if (error)
2884 8069f636 2019-01-12 stsp goto done;
2885 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2886 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2887 8069f636 2019-01-12 stsp if (error != NULL) {
2888 8069f636 2019-01-12 stsp free(commit_id);
2889 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2890 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2891 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2893 8069f636 2019-01-12 stsp goto done;
2895 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2896 4b6c9460 2020-03-05 stsp if (error) {
2897 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2898 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2899 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2901 45d344f6 2019-05-14 stsp goto done;
2903 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2904 8069f636 2019-01-12 stsp commit_id);
2905 8069f636 2019-01-12 stsp free(commit_id);
2906 8069f636 2019-01-12 stsp if (error)
2907 8069f636 2019-01-12 stsp goto done;
2910 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2911 f2ea84fa 2019-07-27 stsp if (error)
2912 f2ea84fa 2019-07-27 stsp goto done;
2913 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2914 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2915 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2916 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2917 c09a553d 2018-03-12 stsp if (error != NULL)
2918 c09a553d 2018-03-12 stsp goto done;
2920 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2921 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2922 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2924 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2925 8069f636 2019-01-12 stsp free(commit_id_str);
2926 76089277 2018-04-01 stsp free(repo_path);
2927 507dc3bb 2018-12-29 stsp free(worktree_path);
2928 c47340da 2020-09-20 stsp free(cwd);
2929 507dc3bb 2018-12-29 stsp return error;
2932 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2933 9627c110 2020-04-18 stsp int did_something;
2934 9627c110 2020-04-18 stsp int conflicts;
2935 9627c110 2020-04-18 stsp int obstructed;
2936 9627c110 2020-04-18 stsp int not_updated;
2940 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2942 9627c110 2020-04-18 stsp if (!upa->did_something)
2945 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2946 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2947 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2948 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2949 9627c110 2020-04-18 stsp upa->obstructed);
2950 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2951 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2952 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2955 507dc3bb 2018-12-29 stsp __dead static void
2956 507dc3bb 2018-12-29 stsp usage_update(void)
2958 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2959 507dc3bb 2018-12-29 stsp getprogname());
2963 1ee397ad 2019-07-12 stsp static const struct got_error *
2964 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2966 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2968 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2969 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2970 1ee397ad 2019-07-12 stsp return NULL;
2972 9627c110 2020-04-18 stsp upa->did_something = 1;
2974 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2975 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2976 1ee397ad 2019-07-12 stsp return NULL;
2978 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2979 9627c110 2020-04-18 stsp upa->conflicts++;
2980 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2981 9627c110 2020-04-18 stsp upa->obstructed++;
2982 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2983 9627c110 2020-04-18 stsp upa->not_updated++;
2985 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2987 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2988 1ee397ad 2019-07-12 stsp return NULL;
2991 be7061eb 2018-12-30 stsp static const struct got_error *
2992 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2993 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2994 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2996 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2997 a1fb16d8 2019-05-24 stsp char *base_id_str;
2998 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3000 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3001 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3002 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3003 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3004 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3005 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3006 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3009 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3010 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3011 a1fb16d8 2019-05-24 stsp if (err) {
3012 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3013 a1fb16d8 2019-05-24 stsp return err;
3014 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3016 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3017 a1fb16d8 2019-05-24 stsp return NULL;
3019 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3020 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3021 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3023 a1fb16d8 2019-05-24 stsp return err;
3024 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3025 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3026 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3027 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3028 0ebf8283 2019-07-24 stsp return NULL;
3031 0ebf8283 2019-07-24 stsp static const struct got_error *
3032 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3034 0ebf8283 2019-07-24 stsp const struct got_error *err;
3035 0ebf8283 2019-07-24 stsp int in_progress;
3037 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3039 0ebf8283 2019-07-24 stsp return err;
3040 0ebf8283 2019-07-24 stsp if (in_progress)
3041 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3043 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3045 0ebf8283 2019-07-24 stsp return err;
3046 0ebf8283 2019-07-24 stsp if (in_progress)
3047 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3049 a1fb16d8 2019-05-24 stsp return NULL;
3052 a5edda0a 2019-07-27 stsp static const struct got_error *
3053 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3054 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3056 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3057 a5edda0a 2019-07-27 stsp char *path;
3060 a5edda0a 2019-07-27 stsp if (argc == 0) {
3061 a5edda0a 2019-07-27 stsp path = strdup("");
3062 a5edda0a 2019-07-27 stsp if (path == NULL)
3063 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3064 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3067 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3068 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3071 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3072 a5edda0a 2019-07-27 stsp if (err) {
3073 a5edda0a 2019-07-27 stsp free(path);
3078 a5edda0a 2019-07-27 stsp return err;
3081 a1fb16d8 2019-05-24 stsp static const struct got_error *
3082 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3083 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3085 fa51e947 2020-03-27 stsp const struct got_error *err;
3086 fa51e947 2020-03-27 stsp struct got_repository *repo;
3087 fa51e947 2020-03-27 stsp static char msg[512];
3089 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3091 fa51e947 2020-03-27 stsp return orig_err;
3093 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3094 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3095 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3096 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3097 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3098 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3099 fa51e947 2020-03-27 stsp got_repo_close(repo);
3100 fa51e947 2020-03-27 stsp return err;
3103 fa51e947 2020-03-27 stsp static const struct got_error *
3104 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3106 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3107 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3108 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3109 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3110 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3111 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3112 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3113 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3114 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3115 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3117 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3119 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3121 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3122 507dc3bb 2018-12-29 stsp switch (ch) {
3124 024e9686 2019-05-14 stsp branch_name = optarg;
3127 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3128 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3129 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3132 2deda0b9 2019-03-07 stsp usage_update();
3133 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3137 507dc3bb 2018-12-29 stsp argc -= optind;
3138 507dc3bb 2018-12-29 stsp argv += optind;
3140 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3141 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3142 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3143 507dc3bb 2018-12-29 stsp err(1, "pledge");
3145 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3146 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3147 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3148 c4cdcb68 2019-04-03 stsp goto done;
3150 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3151 fa51e947 2020-03-27 stsp if (error) {
3152 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3153 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3154 fa51e947 2020-03-27 stsp worktree_path);
3155 7d5807f4 2019-07-11 stsp goto done;
3158 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3159 f2ea84fa 2019-07-27 stsp if (error)
3160 f2ea84fa 2019-07-27 stsp goto done;
3162 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3164 507dc3bb 2018-12-29 stsp if (error != NULL)
3165 507dc3bb 2018-12-29 stsp goto done;
3167 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3168 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3169 087fb88c 2019-08-04 stsp if (error)
3170 087fb88c 2019-08-04 stsp goto done;
3172 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3173 0266afb7 2019-01-04 stsp if (error)
3174 0266afb7 2019-01-04 stsp goto done;
3176 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3177 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3178 024e9686 2019-05-14 stsp if (error != NULL)
3179 024e9686 2019-05-14 stsp goto done;
3180 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3181 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3182 9c4b8182 2019-01-02 stsp if (error != NULL)
3183 9c4b8182 2019-01-02 stsp goto done;
3184 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3185 507dc3bb 2018-12-29 stsp if (error != NULL)
3186 507dc3bb 2018-12-29 stsp goto done;
3188 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3189 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3190 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3192 84de9106 2020-12-26 stsp if (error)
3193 84de9106 2020-12-26 stsp goto done;
3194 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3195 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3196 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3197 04f57cb3 2019-07-25 stsp free(commit_id_str);
3198 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3199 30837e32 2019-07-25 stsp if (error)
3200 a297e751 2019-07-11 stsp goto done;
3201 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3202 a297e751 2019-07-11 stsp if (error)
3203 507dc3bb 2018-12-29 stsp goto done;
3206 a1fb16d8 2019-05-24 stsp if (branch_name) {
3207 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3208 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3209 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3211 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3212 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3213 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3214 024e9686 2019-05-14 stsp goto done;
3216 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3217 024e9686 2019-05-14 stsp if (error)
3218 024e9686 2019-05-14 stsp goto done;
3219 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3221 a367ff0f 2019-05-14 stsp free(head_commit_id);
3222 024e9686 2019-05-14 stsp if (error != NULL)
3223 024e9686 2019-05-14 stsp goto done;
3224 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3225 a367ff0f 2019-05-14 stsp if (error)
3226 a367ff0f 2019-05-14 stsp goto done;
3227 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3228 024e9686 2019-05-14 stsp if (error)
3229 024e9686 2019-05-14 stsp goto done;
3231 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3232 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3233 a367ff0f 2019-05-14 stsp if (error != NULL) {
3234 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3235 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3236 024e9686 2019-05-14 stsp goto done;
3238 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3239 a367ff0f 2019-05-14 stsp if (error)
3240 a367ff0f 2019-05-14 stsp goto done;
3243 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3244 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3245 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3246 507dc3bb 2018-12-29 stsp commit_id);
3247 507dc3bb 2018-12-29 stsp if (error)
3248 507dc3bb 2018-12-29 stsp goto done;
3251 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3252 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3253 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3254 507dc3bb 2018-12-29 stsp if (error != NULL)
3255 507dc3bb 2018-12-29 stsp goto done;
3257 9627c110 2020-04-18 stsp if (upa.did_something)
3258 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3260 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3261 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3263 c09a553d 2018-03-12 stsp free(worktree_path);
3264 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3265 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3266 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3267 507dc3bb 2018-12-29 stsp free(commit_id);
3268 9c4b8182 2019-01-02 stsp free(commit_id_str);
3269 c09a553d 2018-03-12 stsp return error;
3272 f42b1b34 2018-03-12 stsp static const struct got_error *
3273 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3274 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3275 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3277 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3278 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3280 44392932 2019-08-25 stsp if (blob_id1) {
3281 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3283 44392932 2019-08-25 stsp goto done;
3286 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3288 44392932 2019-08-25 stsp goto done;
3290 44392932 2019-08-25 stsp while (path[0] == '/')
3292 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3293 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3295 44392932 2019-08-25 stsp if (blob1)
3296 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3297 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3298 44392932 2019-08-25 stsp return err;
3301 44392932 2019-08-25 stsp static const struct got_error *
3302 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3303 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3304 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3306 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3307 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3308 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3310 44392932 2019-08-25 stsp if (tree_id1) {
3311 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3313 44392932 2019-08-25 stsp goto done;
3316 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3318 0f2b3dca 2018-12-22 stsp goto done;
3320 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3321 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3322 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3323 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3324 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3325 fe621944 2020-11-10 stsp arg.nlines = 0;
3326 44392932 2019-08-25 stsp while (path[0] == '/')
3328 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3329 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3331 79109fed 2018-03-27 stsp if (tree1)
3332 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3333 366e0a5f 2019-10-10 stsp if (tree2)
3334 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3335 44392932 2019-08-25 stsp return err;
3338 44392932 2019-08-25 stsp static const struct got_error *
3339 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3340 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3342 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3343 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3344 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3345 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3347 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3348 0208f208 2020-05-05 stsp if (qid != NULL) {
3349 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3350 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3353 0208f208 2020-05-05 stsp return err;
3355 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3356 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3360 0208f208 2020-05-05 stsp if (tree_id1) {
3361 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3363 0208f208 2020-05-05 stsp goto done;
3366 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3367 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3369 0208f208 2020-05-05 stsp goto done;
3371 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3372 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3374 0208f208 2020-05-05 stsp if (tree1)
3375 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3376 0208f208 2020-05-05 stsp if (tree2)
3377 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3378 0208f208 2020-05-05 stsp return err;
3381 0208f208 2020-05-05 stsp static const struct got_error *
3382 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3383 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3385 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3386 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3387 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3388 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3389 44392932 2019-08-25 stsp struct got_object_qid *qid;
3391 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3392 44392932 2019-08-25 stsp if (qid != NULL) {
3393 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3396 44392932 2019-08-25 stsp return err;
3399 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3400 44392932 2019-08-25 stsp int obj_type;
3401 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3403 44392932 2019-08-25 stsp goto done;
3404 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3405 44392932 2019-08-25 stsp if (err) {
3406 44392932 2019-08-25 stsp free(obj_id2);
3407 44392932 2019-08-25 stsp goto done;
3409 44392932 2019-08-25 stsp if (pcommit) {
3410 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3411 44392932 2019-08-25 stsp qid->id, path);
3412 44392932 2019-08-25 stsp if (err) {
3413 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3414 2e8c69d1 2020-05-04 stsp free(obj_id2);
3415 2e8c69d1 2020-05-04 stsp goto done;
3418 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3419 2e8c69d1 2020-05-04 stsp if (err) {
3420 2e8c69d1 2020-05-04 stsp free(obj_id2);
3421 2e8c69d1 2020-05-04 stsp goto done;
3425 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3426 44392932 2019-08-25 stsp if (err) {
3427 44392932 2019-08-25 stsp free(obj_id2);
3428 44392932 2019-08-25 stsp goto done;
3430 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3431 44392932 2019-08-25 stsp switch (obj_type) {
3432 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3433 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3434 64453f7e 2020-11-21 stsp 0, 0, repo);
3436 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3437 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3438 64453f7e 2020-11-21 stsp 0, 0, repo);
3441 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3444 44392932 2019-08-25 stsp free(obj_id1);
3445 44392932 2019-08-25 stsp free(obj_id2);
3447 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3448 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3450 44392932 2019-08-25 stsp goto done;
3451 579bd556 2020-10-24 stsp if (pcommit) {
3452 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3453 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3455 579bd556 2020-10-24 stsp goto done;
3457 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3459 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3463 0f2b3dca 2018-12-22 stsp free(id_str1);
3464 0f2b3dca 2018-12-22 stsp free(id_str2);
3465 44392932 2019-08-25 stsp if (pcommit)
3466 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3467 79109fed 2018-03-27 stsp return err;
3470 4bb494d5 2018-06-16 stsp static char *
3471 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3473 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3474 09867e48 2019-08-13 stsp char *p, *s;
3476 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3477 09867e48 2019-08-13 stsp if (tm == NULL)
3478 09867e48 2019-08-13 stsp return NULL;
3479 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3480 09867e48 2019-08-13 stsp if (s == NULL)
3481 09867e48 2019-08-13 stsp return NULL;
3482 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3484 6c281f94 2018-06-11 stsp *p = '\0';
3488 6841bf13 2019-11-29 kn static const struct got_error *
3489 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3490 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3492 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3493 6841bf13 2019-11-29 kn regmatch_t regmatch;
3494 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3496 6841bf13 2019-11-29 kn *have_match = 0;
3498 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3502 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3506 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3507 6841bf13 2019-11-29 kn *have_match = 1;
3509 6841bf13 2019-11-29 kn free(id_str);
3510 6841bf13 2019-11-29 kn free(logmsg);
3514 0208f208 2020-05-05 stsp static void
3515 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3516 0208f208 2020-05-05 stsp regex_t *regex)
3518 0208f208 2020-05-05 stsp regmatch_t regmatch;
3519 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3521 0208f208 2020-05-05 stsp *have_match = 0;
3523 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3524 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3525 0208f208 2020-05-05 stsp *have_match = 1;
3531 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3533 888b7d99 2020-12-26 stsp static const struct got_error*
3534 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3535 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3537 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3538 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3540 888b7d99 2020-12-26 stsp const char *name;
3542 888b7d99 2020-12-26 stsp *refs_str = NULL;
3544 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3545 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3546 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3549 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3550 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3552 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3553 199a4027 2019-02-02 stsp name += 5;
3554 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3556 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3557 e34f9ed6 2019-02-02 stsp name += 6;
3558 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3559 141c2bff 2019-02-04 stsp name += 8;
3560 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3561 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3564 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3567 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3568 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3569 5d844a1e 2019-08-13 stsp if (err) {
3570 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3571 48cae60d 2020-09-22 stsp free(ref_id);
3574 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3575 5d844a1e 2019-08-13 stsp err = NULL;
3576 5d844a1e 2019-08-13 stsp tag = NULL;
3579 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3580 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3581 48cae60d 2020-09-22 stsp free(ref_id);
3583 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3584 a436ad14 2019-08-13 stsp if (cmp != 0)
3586 888b7d99 2020-12-26 stsp s = *refs_str;
3587 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3588 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3589 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3591 888b7d99 2020-12-26 stsp *refs_str = NULL;
3597 888b7d99 2020-12-26 stsp return err;
3600 888b7d99 2020-12-26 stsp static const struct got_error *
3601 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3602 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3603 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3604 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3605 e600f124 2021-03-21 stsp const char *custom_refs_str)
3607 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3608 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3609 888b7d99 2020-12-26 stsp char datebuf[26];
3610 888b7d99 2020-12-26 stsp time_t committer_time;
3611 888b7d99 2020-12-26 stsp const char *author, *committer;
3612 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3614 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3616 8bf5b3c9 2018-03-17 stsp return err;
3618 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3619 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3620 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3621 e600f124 2021-03-21 stsp if (refs) {
3622 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3624 e600f124 2021-03-21 stsp goto done;
3628 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3629 e600f124 2021-03-21 stsp if (custom_refs_str)
3630 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3632 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3633 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3634 832c249c 2018-06-10 stsp free(id_str);
3635 d3d493d7 2019-02-21 stsp id_str = NULL;
3636 d3d493d7 2019-02-21 stsp free(refs_str);
3637 d3d493d7 2019-02-21 stsp refs_str = NULL;
3638 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3639 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3640 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3641 09867e48 2019-08-13 stsp if (datestr)
3642 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3643 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3644 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3645 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3646 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3647 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3648 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3649 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3650 3fe1abad 2018-06-10 stsp int n = 1;
3651 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3652 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3653 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3655 888b7d99 2020-12-26 stsp goto done;
3656 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3657 a0603db2 2018-06-10 stsp free(id_str);
3658 888b7d99 2020-12-26 stsp id_str = NULL;
3662 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3664 888b7d99 2020-12-26 stsp goto done;
3666 621015ac 2018-07-23 stsp logmsg = logmsg0;
3668 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3670 832c249c 2018-06-10 stsp printf(" %s\n", line);
3671 832c249c 2018-06-10 stsp } while (line);
3672 621015ac 2018-07-23 stsp free(logmsg0);
3674 0208f208 2020-05-05 stsp if (changed_paths) {
3675 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3676 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3677 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3678 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3680 0208f208 2020-05-05 stsp printf("\n");
3682 971751ac 2018-03-27 stsp if (show_patch) {
3683 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3684 971751ac 2018-03-27 stsp if (err == 0)
3685 971751ac 2018-03-27 stsp printf("\n");
3688 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3689 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3691 888b7d99 2020-12-26 stsp free(id_str);
3692 888b7d99 2020-12-26 stsp free(refs_str);
3693 07862c20 2018-09-15 stsp return err;
3696 f42b1b34 2018-03-12 stsp static const struct got_error *
3697 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3698 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3699 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3700 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3701 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3703 f42b1b34 2018-03-12 stsp const struct got_error *err;
3704 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3705 6841bf13 2019-11-29 kn regex_t regex;
3706 6841bf13 2019-11-29 kn int have_match;
3707 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3708 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3709 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3710 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3711 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3713 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3714 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3716 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3717 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3718 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3720 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3722 f42b1b34 2018-03-12 stsp return err;
3723 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3724 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3726 fcc85cad 2018-07-22 stsp goto done;
3728 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3730 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3733 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3734 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3735 372ccdbb 2018-06-10 stsp if (err) {
3736 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3737 9ba79e04 2018-06-11 stsp err = NULL;
3740 b43fbaa0 2018-06-11 stsp if (id == NULL)
3743 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3747 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3748 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3753 6841bf13 2019-11-29 kn if (search_pattern) {
3754 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3756 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3759 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3760 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3761 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3762 6841bf13 2019-11-29 kn if (have_match == 0) {
3763 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3764 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3765 0208f208 2020-05-05 stsp free((char *)pe->path);
3766 0208f208 2020-05-05 stsp free(pe->data);
3768 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3773 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3774 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3777 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3778 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3780 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3781 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3782 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3783 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3787 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3788 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3791 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3792 0208f208 2020-05-05 stsp free((char *)pe->path);
3793 0208f208 2020-05-05 stsp free(pe->data);
3795 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3797 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3798 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3799 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3802 502b9684 2020-07-31 stsp if (show_changed_paths) {
3803 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3804 502b9684 2020-07-31 stsp commit, repo);
3808 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3809 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3810 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3811 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3814 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3815 502b9684 2020-07-31 stsp free((char *)pe->path);
3816 502b9684 2020-07-31 stsp free(pe->data);
3818 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3822 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3823 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3824 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3825 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3827 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3828 0208f208 2020-05-05 stsp free((char *)pe->path);
3829 0208f208 2020-05-05 stsp free(pe->data);
3831 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3832 6841bf13 2019-11-29 kn if (search_pattern)
3833 6841bf13 2019-11-29 kn regfree(®ex);
3834 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3835 f42b1b34 2018-03-12 stsp return err;
3838 4ed7e80c 2018-05-20 stsp __dead static void
3839 6f3d1eb0 2018-03-12 stsp usage_log(void)
3841 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3842 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3843 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3847 b1ebc001 2019-08-13 stsp static int
3848 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3850 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3851 b1ebc001 2019-08-13 stsp long long n;
3852 b1ebc001 2019-08-13 stsp const char *errstr;
3854 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3855 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3857 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3858 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3863 4ed7e80c 2018-05-20 stsp static const struct got_error *
3864 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3866 f42b1b34 2018-03-12 stsp const struct got_error *error;
3867 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3868 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3869 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3870 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3871 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3872 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3873 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3874 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3875 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3876 64a96a6d 2018-04-01 stsp const char *errstr;
3877 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3878 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3880 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3882 6715a751 2018-03-16 stsp #ifndef PROFILE
3883 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3886 f42b1b34 2018-03-12 stsp err(1, "pledge");
3889 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3891 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3892 79109fed 2018-03-27 stsp switch (ch) {
3894 79109fed 2018-03-27 stsp show_patch = 1;
3897 0208f208 2020-05-05 stsp show_changed_paths = 1;
3900 d142fc45 2018-04-01 stsp start_commit = optarg;
3903 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3905 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3906 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3909 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3910 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3911 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3914 48c8c60d 2020-01-27 stsp log_branches = 1;
3917 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3918 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3919 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3921 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3924 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3927 6841bf13 2019-11-29 kn search_pattern = optarg;
3930 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3933 2deda0b9 2019-03-07 stsp usage_log();
3934 79109fed 2018-03-27 stsp /* NOTREACHED */
3938 79109fed 2018-03-27 stsp argc -= optind;
3939 79109fed 2018-03-27 stsp argv += optind;
3941 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3942 dc1edbfa 2019-11-29 kn diff_context = 3;
3943 dc1edbfa 2019-11-29 kn else if (!show_patch)
3944 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3946 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3947 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3948 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3949 04ca23f4 2018-07-16 stsp goto done;
3952 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3953 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3954 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3955 50f2fada 2020-04-24 stsp goto done;
3956 50f2fada 2020-04-24 stsp error = NULL;
3959 603cdeb0 2020-10-22 stsp if (argc == 1) {
3960 e7301579 2019-03-18 stsp if (worktree) {
3961 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3963 e7301579 2019-03-18 stsp if (error)
3964 e7301579 2019-03-18 stsp goto done;
3966 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3967 e7301579 2019-03-18 stsp if (path == NULL) {
3968 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3969 e7301579 2019-03-18 stsp goto done;
3972 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3973 cbd1af7a 2019-03-18 stsp usage_log();
3975 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3976 5486daa2 2019-05-11 stsp repo_path = worktree ?
3977 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3979 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3980 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3981 cffc0aa4 2019-02-05 stsp goto done;
3984 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3985 d7d4f210 2018-03-12 stsp if (error != NULL)
3986 04ca23f4 2018-07-16 stsp goto done;
3988 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3989 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3990 c02c541e 2019-03-29 stsp if (error)
3991 c02c541e 2019-03-29 stsp goto done;
3993 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3994 84de9106 2020-12-26 stsp if (error)
3995 84de9106 2020-12-26 stsp goto done;
3997 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
3998 888b7d99 2020-12-26 stsp if (error)
3999 888b7d99 2020-12-26 stsp goto done;
4001 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4002 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4003 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4004 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4005 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4006 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4007 3235492e 2018-04-01 stsp if (error != NULL)
4008 d1fe46f9 2020-04-18 stsp goto done;
4009 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4010 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4011 3235492e 2018-04-01 stsp if (error != NULL)
4012 d1fe46f9 2020-04-18 stsp goto done;
4013 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4014 d1fe46f9 2020-04-18 stsp start_id);
4015 d1fe46f9 2020-04-18 stsp if (error != NULL)
4016 d1fe46f9 2020-04-18 stsp goto done;
4017 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4019 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4020 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4021 d1fe46f9 2020-04-18 stsp if (error != NULL)
4022 d1fe46f9 2020-04-18 stsp goto done;
4024 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4025 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4026 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4027 d1fe46f9 2020-04-18 stsp if (error != NULL)
4028 d1fe46f9 2020-04-18 stsp goto done;
4031 deeabeae 2019-08-27 stsp if (worktree) {
4033 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4034 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4035 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4037 603cdeb0 2020-10-22 stsp if (path) {
4038 603cdeb0 2020-10-22 stsp const char *prefix;
4039 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4040 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4041 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4042 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4043 603cdeb0 2020-10-22 stsp goto done;
4047 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4048 8fa913ec 2020-11-14 stsp path ? path : "");
4049 04ca23f4 2018-07-16 stsp if (error != NULL)
4050 04ca23f4 2018-07-16 stsp goto done;
4051 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4052 04ca23f4 2018-07-16 stsp free(path);
4053 04ca23f4 2018-07-16 stsp path = in_repo_path;
4056 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4057 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4058 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4060 04ca23f4 2018-07-16 stsp free(path);
4061 04ca23f4 2018-07-16 stsp free(repo_path);
4062 04ca23f4 2018-07-16 stsp free(cwd);
4063 cffc0aa4 2019-02-05 stsp if (worktree)
4064 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4065 ad242220 2018-09-08 stsp if (repo) {
4066 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4067 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4068 ad242220 2018-09-08 stsp if (error == NULL)
4069 ad242220 2018-09-08 stsp error = repo_error;
4071 888b7d99 2020-12-26 stsp if (refs_idmap)
4072 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4073 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4074 8bf5b3c9 2018-03-17 stsp return error;
4077 4ed7e80c 2018-05-20 stsp __dead static void
4078 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4080 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4081 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4085 b72f483a 2019-02-05 stsp struct print_diff_arg {
4086 b72f483a 2019-02-05 stsp struct got_repository *repo;
4087 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4088 b72f483a 2019-02-05 stsp int diff_context;
4089 3fc0c068 2019-02-10 stsp const char *id_str;
4090 3fc0c068 2019-02-10 stsp int header_shown;
4091 98eaaa12 2019-08-03 stsp int diff_staged;
4092 63035f9f 2019-10-06 stsp int ignore_whitespace;
4093 64453f7e 2020-11-21 stsp int force_text_diff;
4097 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4098 39449a05 2020-07-23 stsp * it as content to the diff engine.
4100 39449a05 2020-07-23 stsp static const struct got_error *
4101 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4102 39449a05 2020-07-23 stsp const char *abspath)
4104 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4105 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4106 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4110 39449a05 2020-07-23 stsp if (dirfd != -1) {
4111 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4112 39449a05 2020-07-23 stsp if (target_len == -1)
4113 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4115 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4116 39449a05 2020-07-23 stsp if (target_len == -1)
4117 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4120 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4121 39449a05 2020-07-23 stsp if (*fd == -1)
4122 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4124 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4125 39449a05 2020-07-23 stsp if (outlen == -1) {
4126 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4127 39449a05 2020-07-23 stsp goto done;
4130 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4131 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4132 39449a05 2020-07-23 stsp goto done;
4135 39449a05 2020-07-23 stsp if (err) {
4136 39449a05 2020-07-23 stsp close(*fd);
4139 39449a05 2020-07-23 stsp return err;
4142 4ed7e80c 2018-05-20 stsp static const struct got_error *
4143 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4144 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4145 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4146 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4148 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4149 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4150 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4151 12463d8b 2019-12-13 stsp int fd = -1;
4152 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4153 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4154 b72f483a 2019-02-05 stsp struct stat sb;
4156 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4157 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4158 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4159 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4160 98eaaa12 2019-08-03 stsp return NULL;
4162 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4163 98eaaa12 2019-08-03 stsp return NULL;
4164 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4165 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4166 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4167 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4168 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4169 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4170 98eaaa12 2019-08-03 stsp return NULL;
4173 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4174 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4175 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4176 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4177 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4180 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4181 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4182 98eaaa12 2019-08-03 stsp switch (staged_status) {
4183 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4184 98eaaa12 2019-08-03 stsp label1 = path;
4185 98eaaa12 2019-08-03 stsp label2 = path;
4187 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4188 98eaaa12 2019-08-03 stsp label2 = path;
4190 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4191 98eaaa12 2019-08-03 stsp label1 = path;
4194 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4196 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4197 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4198 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4201 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4202 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4203 4ce46740 2019-08-08 stsp char *id_str;
4204 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4207 4ce46740 2019-08-08 stsp goto done;
4208 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4210 4ce46740 2019-08-08 stsp goto done;
4211 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4212 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4213 4ce46740 2019-08-08 stsp free(id_str);
4214 4ce46740 2019-08-08 stsp goto done;
4216 4ce46740 2019-08-08 stsp free(id_str);
4217 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4218 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4220 4ce46740 2019-08-08 stsp goto done;
4223 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4224 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4225 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4226 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4227 2ec1f75b 2019-03-26 stsp goto done;
4230 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4231 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4232 12463d8b 2019-12-13 stsp if (fd == -1) {
4233 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4234 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4236 39449a05 2020-07-23 stsp goto done;
4238 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4239 39449a05 2020-07-23 stsp de_name, abspath);
4241 39449a05 2020-07-23 stsp goto done;
4244 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4245 12463d8b 2019-12-13 stsp if (fd == -1) {
4246 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4247 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4249 39449a05 2020-07-23 stsp goto done;
4251 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4252 39449a05 2020-07-23 stsp de_name, abspath);
4254 39449a05 2020-07-23 stsp goto done;
4257 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4258 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4259 2ec1f75b 2019-03-26 stsp goto done;
4261 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4262 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4263 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4264 2ec1f75b 2019-03-26 stsp goto done;
4268 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4270 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4271 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4273 b72f483a 2019-02-05 stsp if (blob1)
4274 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4275 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4276 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4277 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4278 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4279 b72f483a 2019-02-05 stsp free(abspath);
4280 927df6b7 2019-02-10 stsp return err;
4283 927df6b7 2019-02-10 stsp static const struct got_error *
4284 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4286 b00d56cd 2018-04-01 stsp const struct got_error *error;
4287 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4288 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4289 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4290 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4291 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4292 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4293 15a94983 2018-12-23 stsp int type1, type2;
4294 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4295 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4296 c0cc5c62 2018-10-18 stsp const char *errstr;
4297 927df6b7 2019-02-10 stsp char *path = NULL;
4298 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4300 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4302 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4303 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4304 25eccc22 2019-01-04 stsp NULL) == -1)
4305 b00d56cd 2018-04-01 stsp err(1, "pledge");
4308 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4309 b00d56cd 2018-04-01 stsp switch (ch) {
4311 64453f7e 2020-11-21 stsp force_text_diff = 1;
4314 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4316 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4317 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4320 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4321 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4322 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4324 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4327 98eaaa12 2019-08-03 stsp diff_staged = 1;
4330 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4333 2deda0b9 2019-03-07 stsp usage_diff();
4334 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4338 b00d56cd 2018-04-01 stsp argc -= optind;
4339 b00d56cd 2018-04-01 stsp argv += optind;
4341 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4342 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4343 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4344 b72f483a 2019-02-05 stsp goto done;
4346 927df6b7 2019-02-10 stsp if (argc <= 1) {
4347 b72f483a 2019-02-05 stsp if (repo_path)
4349 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4350 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4351 fa51e947 2020-03-27 stsp if (error) {
4352 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4353 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4355 1f03b8da 2020-03-20 stsp goto done;
4357 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4358 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4359 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4360 927df6b7 2019-02-10 stsp goto done;
4362 927df6b7 2019-02-10 stsp if (argc == 1) {
4363 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4365 927df6b7 2019-02-10 stsp if (error)
4366 927df6b7 2019-02-10 stsp goto done;
4368 927df6b7 2019-02-10 stsp path = strdup("");
4369 927df6b7 2019-02-10 stsp if (path == NULL) {
4370 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4371 927df6b7 2019-02-10 stsp goto done;
4374 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4375 98eaaa12 2019-08-03 stsp if (diff_staged)
4376 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4377 98eaaa12 2019-08-03 stsp "objects in repository");
4378 15a94983 2018-12-23 stsp id_str1 = argv[0];
4379 15a94983 2018-12-23 stsp id_str2 = argv[1];
4380 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4381 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4382 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4383 30db809c 2019-06-05 stsp goto done;
4384 1f03b8da 2020-03-20 stsp if (worktree) {
4385 1f03b8da 2020-03-20 stsp repo_path = strdup(
4386 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4387 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4388 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4389 1f03b8da 2020-03-20 stsp goto done;
4392 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4393 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4394 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4395 1f03b8da 2020-03-20 stsp goto done;
4400 b00d56cd 2018-04-01 stsp usage_diff();
4402 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4403 76089277 2018-04-01 stsp free(repo_path);
4404 b00d56cd 2018-04-01 stsp if (error != NULL)
4405 b00d56cd 2018-04-01 stsp goto done;
4407 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4408 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4409 c02c541e 2019-03-29 stsp if (error)
4410 c02c541e 2019-03-29 stsp goto done;
4412 30db809c 2019-06-05 stsp if (argc <= 1) {
4413 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4414 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4415 b72f483a 2019-02-05 stsp char *id_str;
4417 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4419 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4420 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4421 b72f483a 2019-02-05 stsp if (error)
4422 b72f483a 2019-02-05 stsp goto done;
4423 b72f483a 2019-02-05 stsp arg.repo = repo;
4424 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4425 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4426 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4427 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4428 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4429 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4430 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4432 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4433 72ea6654 2019-07-27 stsp if (error)
4434 72ea6654 2019-07-27 stsp goto done;
4436 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4437 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4438 b72f483a 2019-02-05 stsp free(id_str);
4439 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4440 b72f483a 2019-02-05 stsp goto done;
4443 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4444 84de9106 2020-12-26 stsp if (error)
4445 84de9106 2020-12-26 stsp return error;
4447 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4448 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4449 0adb7196 2019-08-11 stsp if (error)
4450 0adb7196 2019-08-11 stsp goto done;
4452 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4453 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4454 0adb7196 2019-08-11 stsp if (error)
4455 0adb7196 2019-08-11 stsp goto done;
4457 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4458 15a94983 2018-12-23 stsp if (error)
4459 15a94983 2018-12-23 stsp goto done;
4461 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4462 15a94983 2018-12-23 stsp if (error)
4463 15a94983 2018-12-23 stsp goto done;
4465 15a94983 2018-12-23 stsp if (type1 != type2) {
4466 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4467 b00d56cd 2018-04-01 stsp goto done;
4470 15a94983 2018-12-23 stsp switch (type1) {
4471 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4472 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4473 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4474 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4476 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4477 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4478 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4479 64453f7e 2020-11-21 stsp repo, stdout);
4481 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4482 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4483 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4484 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4488 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4491 5e70831e 2019-05-28 stsp free(label1);
4492 5e70831e 2019-05-28 stsp free(label2);
4493 15a94983 2018-12-23 stsp free(id1);
4494 15a94983 2018-12-23 stsp free(id2);
4495 927df6b7 2019-02-10 stsp free(path);
4496 b72f483a 2019-02-05 stsp if (worktree)
4497 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4498 ad242220 2018-09-08 stsp if (repo) {
4499 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4500 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4501 ad242220 2018-09-08 stsp if (error == NULL)
4502 ad242220 2018-09-08 stsp error = repo_error;
4504 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4505 b00d56cd 2018-04-01 stsp return error;
4508 404c43c4 2018-06-21 stsp __dead static void
4509 404c43c4 2018-06-21 stsp usage_blame(void)
4511 9270e621 2019-02-05 stsp fprintf(stderr,
4512 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4513 404c43c4 2018-06-21 stsp getprogname());
4517 28315671 2019-08-14 stsp struct blame_line {
4518 28315671 2019-08-14 stsp int annotated;
4519 28315671 2019-08-14 stsp char *id_str;
4520 82f6abb8 2019-08-14 stsp char *committer;
4521 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4524 28315671 2019-08-14 stsp struct blame_cb_args {
4525 28315671 2019-08-14 stsp struct blame_line *lines;
4526 28315671 2019-08-14 stsp int nlines;
4527 7ef28ff8 2019-08-14 stsp int nlines_prec;
4528 28315671 2019-08-14 stsp int lineno_cur;
4529 28315671 2019-08-14 stsp off_t *line_offsets;
4531 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4534 404c43c4 2018-06-21 stsp static const struct got_error *
4535 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4537 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4538 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4539 28315671 2019-08-14 stsp struct blame_line *bline;
4540 82f6abb8 2019-08-14 stsp char *line = NULL;
4541 28315671 2019-08-14 stsp size_t linesize = 0;
4542 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4543 28315671 2019-08-14 stsp off_t offset;
4544 bcb49d15 2019-08-14 stsp struct tm tm;
4545 bcb49d15 2019-08-14 stsp time_t committer_time;
4547 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4548 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4549 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4551 28315671 2019-08-14 stsp if (sigint_received)
4552 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4554 2839f8b9 2019-08-18 stsp if (lineno == -1)
4555 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4557 28315671 2019-08-14 stsp /* Annotate this line. */
4558 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4559 28315671 2019-08-14 stsp if (bline->annotated)
4560 28315671 2019-08-14 stsp return NULL;
4561 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4563 28315671 2019-08-14 stsp return err;
4565 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4567 82f6abb8 2019-08-14 stsp goto done;
4569 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4570 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4571 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4572 bcb49d15 2019-08-14 stsp goto done;
4575 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4576 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4577 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4578 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4579 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4580 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4581 82f6abb8 2019-08-14 stsp goto done;
4583 28315671 2019-08-14 stsp bline->annotated = 1;
4585 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4586 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4587 28315671 2019-08-14 stsp if (!bline->annotated)
4588 82f6abb8 2019-08-14 stsp goto done;
4590 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4591 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4592 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4593 82f6abb8 2019-08-14 stsp goto done;
4596 28315671 2019-08-14 stsp while (bline->annotated) {
4597 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4598 82f6abb8 2019-08-14 stsp size_t len;
4600 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4601 28315671 2019-08-14 stsp if (ferror(a->f))
4602 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4606 82f6abb8 2019-08-14 stsp committer = bline->committer;
4607 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4608 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4609 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4610 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4612 82f6abb8 2019-08-14 stsp *at = '\0';
4613 82f6abb8 2019-08-14 stsp len = strlen(committer);
4614 82f6abb8 2019-08-14 stsp if (len >= 9)
4615 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4617 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4619 28315671 2019-08-14 stsp *nl = '\0';
4620 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4621 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4623 28315671 2019-08-14 stsp a->lineno_cur++;
4624 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4627 82f6abb8 2019-08-14 stsp if (commit)
4628 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4629 28315671 2019-08-14 stsp free(line);
4630 28315671 2019-08-14 stsp return err;
4633 28315671 2019-08-14 stsp static const struct got_error *
4634 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4636 404c43c4 2018-06-21 stsp const struct got_error *error;
4637 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4638 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4639 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4640 0587e10c 2020-07-23 stsp char *link_target = NULL;
4641 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4642 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4643 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4644 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4645 28315671 2019-08-14 stsp struct blame_cb_args bca;
4646 28315671 2019-08-14 stsp int ch, obj_type, i;
4647 be659d10 2020-11-18 stsp off_t filesize;
4649 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4651 404c43c4 2018-06-21 stsp #ifndef PROFILE
4652 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4653 36e2fb66 2019-01-04 stsp NULL) == -1)
4654 404c43c4 2018-06-21 stsp err(1, "pledge");
4657 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4658 404c43c4 2018-06-21 stsp switch (ch) {
4660 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4663 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4664 66bea077 2018-08-02 stsp if (repo_path == NULL)
4665 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4667 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4670 2deda0b9 2019-03-07 stsp usage_blame();
4671 404c43c4 2018-06-21 stsp /* NOTREACHED */
4675 404c43c4 2018-06-21 stsp argc -= optind;
4676 404c43c4 2018-06-21 stsp argv += optind;
4678 a39318fd 2018-08-02 stsp if (argc == 1)
4679 404c43c4 2018-06-21 stsp path = argv[0];
4681 404c43c4 2018-06-21 stsp usage_blame();
4683 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4684 66bea077 2018-08-02 stsp if (cwd == NULL) {
4685 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4686 66bea077 2018-08-02 stsp goto done;
4688 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4689 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4690 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4691 66bea077 2018-08-02 stsp goto done;
4693 0c06baac 2019-02-05 stsp error = NULL;
4694 0c06baac 2019-02-05 stsp if (worktree) {
4695 0c06baac 2019-02-05 stsp repo_path =
4696 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4697 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4698 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4699 1f03b8da 2020-03-20 stsp if (error)
4700 1f03b8da 2020-03-20 stsp goto done;
4703 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4704 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4705 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4706 0c06baac 2019-02-05 stsp goto done;
4711 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4712 c02c541e 2019-03-29 stsp if (error != NULL)
4713 404c43c4 2018-06-21 stsp goto done;
4715 0c06baac 2019-02-05 stsp if (worktree) {
4716 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4719 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4720 01740607 2020-11-04 stsp if (error)
4721 01740607 2020-11-04 stsp goto done;
4722 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4723 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4724 01740607 2020-11-04 stsp p) == -1) {
4725 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4727 0c06baac 2019-02-05 stsp goto done;
4730 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4732 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4733 01740607 2020-11-04 stsp if (error)
4734 01740607 2020-11-04 stsp goto done;
4735 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4737 0c06baac 2019-02-05 stsp if (error)
4738 66bea077 2018-08-02 stsp goto done;
4740 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4741 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4742 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4743 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4744 404c43c4 2018-06-21 stsp if (error != NULL)
4745 66bea077 2018-08-02 stsp goto done;
4746 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4747 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4748 404c43c4 2018-06-21 stsp if (error != NULL)
4749 66bea077 2018-08-02 stsp goto done;
4751 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4752 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4753 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4755 84de9106 2020-12-26 stsp if (error)
4756 84de9106 2020-12-26 stsp goto done;
4757 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4758 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4759 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4760 30837e32 2019-07-25 stsp if (error)
4761 66bea077 2018-08-02 stsp goto done;
4764 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4765 0587e10c 2020-07-23 stsp commit_id, repo);
4766 0587e10c 2020-07-23 stsp if (error)
4767 0587e10c 2020-07-23 stsp goto done;
4769 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4770 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4771 28315671 2019-08-14 stsp if (error)
4772 28315671 2019-08-14 stsp goto done;
4774 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4775 28315671 2019-08-14 stsp if (error)
4776 28315671 2019-08-14 stsp goto done;
4778 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4779 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4780 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4781 28315671 2019-08-14 stsp goto done;
4784 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4785 28315671 2019-08-14 stsp if (error)
4786 28315671 2019-08-14 stsp goto done;
4787 28315671 2019-08-14 stsp bca.f = got_opentemp();
4788 28315671 2019-08-14 stsp if (bca.f == NULL) {
4789 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4790 28315671 2019-08-14 stsp goto done;
4792 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4793 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4794 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4795 28315671 2019-08-14 stsp goto done;
4797 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4798 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4799 b02560ec 2019-08-19 stsp bca.nlines--;
4801 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4802 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4803 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4804 28315671 2019-08-14 stsp goto done;
4806 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4807 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4808 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4809 7ef28ff8 2019-08-14 stsp while (i > 0) {
4811 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4813 82f6abb8 2019-08-14 stsp bca.repo = repo;
4815 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4816 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4818 66bea077 2018-08-02 stsp free(in_repo_path);
4819 0587e10c 2020-07-23 stsp free(link_target);
4820 66bea077 2018-08-02 stsp free(repo_path);
4821 66bea077 2018-08-02 stsp free(cwd);
4822 404c43c4 2018-06-21 stsp free(commit_id);
4823 28315671 2019-08-14 stsp free(obj_id);
4825 28315671 2019-08-14 stsp got_object_blob_close(blob);
4826 0c06baac 2019-02-05 stsp if (worktree)
4827 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4828 ad242220 2018-09-08 stsp if (repo) {
4829 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4830 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4831 ad242220 2018-09-08 stsp if (error == NULL)
4832 ad242220 2018-09-08 stsp error = repo_error;
4834 3affba96 2019-09-22 stsp if (bca.lines) {
4835 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4836 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4837 3affba96 2019-09-22 stsp free(bline->id_str);
4838 3affba96 2019-09-22 stsp free(bline->committer);
4840 3affba96 2019-09-22 stsp free(bca.lines);
4842 28315671 2019-08-14 stsp free(bca.line_offsets);
4843 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4844 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4845 404c43c4 2018-06-21 stsp return error;
4848 5de5890b 2018-10-18 stsp __dead static void
4849 5de5890b 2018-10-18 stsp usage_tree(void)
4851 c1669e2e 2019-01-09 stsp fprintf(stderr,
4852 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4853 5de5890b 2018-10-18 stsp getprogname());
4857 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4858 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4859 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4861 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4862 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4863 848d6979 2019-08-12 stsp const char *modestr = "";
4864 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4865 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4867 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4868 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4871 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4872 63c5ca5d 2019-08-24 stsp modestr = "$";
4873 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4876 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4878 0d6c6ee3 2020-05-20 stsp return err;
4879 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4880 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4881 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4884 848d6979 2019-08-12 stsp modestr = "@";
4886 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4887 848d6979 2019-08-12 stsp modestr = "/";
4888 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4889 848d6979 2019-08-12 stsp modestr = "*";
4891 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4892 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4893 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4895 0d6c6ee3 2020-05-20 stsp free(link_target);
4896 0d6c6ee3 2020-05-20 stsp return NULL;
4899 5de5890b 2018-10-18 stsp static const struct got_error *
4900 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4901 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4902 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4904 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4905 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4906 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4907 56e0773d 2019-11-28 stsp int nentries, i;
4909 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4911 5de5890b 2018-10-18 stsp goto done;
4913 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4915 5de5890b 2018-10-18 stsp goto done;
4916 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4917 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4918 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4919 5de5890b 2018-10-18 stsp char *id = NULL;
4921 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4924 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4925 5de5890b 2018-10-18 stsp if (show_ids) {
4926 5de5890b 2018-10-18 stsp char *id_str;
4927 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4928 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4930 5de5890b 2018-10-18 stsp goto done;
4931 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4932 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4933 5de5890b 2018-10-18 stsp free(id_str);
4934 5de5890b 2018-10-18 stsp goto done;
4936 5de5890b 2018-10-18 stsp free(id_str);
4938 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4941 0d6c6ee3 2020-05-20 stsp goto done;
4943 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4944 c1669e2e 2019-01-09 stsp char *child_path;
4945 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4946 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4947 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4948 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4949 c1669e2e 2019-01-09 stsp goto done;
4951 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4952 c1669e2e 2019-01-09 stsp root_path, repo);
4953 c1669e2e 2019-01-09 stsp free(child_path);
4955 c1669e2e 2019-01-09 stsp goto done;
4960 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4961 5de5890b 2018-10-18 stsp free(tree_id);
4962 5de5890b 2018-10-18 stsp return err;
4965 5de5890b 2018-10-18 stsp static const struct got_error *
4966 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4968 5de5890b 2018-10-18 stsp const struct got_error *error;
4969 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4970 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4971 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4972 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4973 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4974 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4975 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4978 5de5890b 2018-10-18 stsp #ifndef PROFILE
4979 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4980 0f8d269b 2019-01-04 stsp NULL) == -1)
4981 5de5890b 2018-10-18 stsp err(1, "pledge");
4984 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4985 5de5890b 2018-10-18 stsp switch (ch) {
4987 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4990 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4991 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4992 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4994 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4997 5de5890b 2018-10-18 stsp show_ids = 1;
5000 c1669e2e 2019-01-09 stsp recurse = 1;
5003 2deda0b9 2019-03-07 stsp usage_tree();
5004 5de5890b 2018-10-18 stsp /* NOTREACHED */
5008 5de5890b 2018-10-18 stsp argc -= optind;
5009 5de5890b 2018-10-18 stsp argv += optind;
5011 5de5890b 2018-10-18 stsp if (argc == 1)
5012 5de5890b 2018-10-18 stsp path = argv[0];
5013 5de5890b 2018-10-18 stsp else if (argc > 1)
5014 5de5890b 2018-10-18 stsp usage_tree();
5016 7a2c19d6 2019-02-05 stsp path = NULL;
5018 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5019 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5020 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5021 9bf7a39b 2019-02-05 stsp goto done;
5023 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5024 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5025 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5026 7a2c19d6 2019-02-05 stsp goto done;
5028 7a2c19d6 2019-02-05 stsp error = NULL;
5029 7a2c19d6 2019-02-05 stsp if (worktree) {
5030 7a2c19d6 2019-02-05 stsp repo_path =
5031 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5032 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5033 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5034 7a2c19d6 2019-02-05 stsp if (error)
5035 7a2c19d6 2019-02-05 stsp goto done;
5037 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5038 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5039 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5040 7a2c19d6 2019-02-05 stsp goto done;
5045 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5046 5de5890b 2018-10-18 stsp if (error != NULL)
5047 c02c541e 2019-03-29 stsp goto done;
5049 4fedbf4c 2020-11-07 stsp if (worktree) {
5050 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5053 4fedbf4c 2020-11-07 stsp if (path == NULL)
5054 4fedbf4c 2020-11-07 stsp path = "";
5055 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5056 4fedbf4c 2020-11-07 stsp if (error)
5057 4fedbf4c 2020-11-07 stsp goto done;
5058 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5059 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5060 4fedbf4c 2020-11-07 stsp p) == -1) {
5061 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5063 4fedbf4c 2020-11-07 stsp goto done;
5066 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5067 4fedbf4c 2020-11-07 stsp if (error)
5068 4fedbf4c 2020-11-07 stsp goto done;
5070 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5071 4fedbf4c 2020-11-07 stsp if (error)
5072 4fedbf4c 2020-11-07 stsp goto done;
5073 4fedbf4c 2020-11-07 stsp if (path == NULL)
5074 9bf7a39b 2019-02-05 stsp path = "/";
5075 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5076 9bf7a39b 2019-02-05 stsp if (error != NULL)
5077 9bf7a39b 2019-02-05 stsp goto done;
5080 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5081 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5082 4e0a20a4 2020-03-23 tracey if (worktree)
5083 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5085 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5086 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5087 5de5890b 2018-10-18 stsp if (error != NULL)
5088 5de5890b 2018-10-18 stsp goto done;
5089 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5090 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5091 5de5890b 2018-10-18 stsp if (error != NULL)
5092 5de5890b 2018-10-18 stsp goto done;
5094 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5095 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5096 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5098 84de9106 2020-12-26 stsp if (error)
5099 84de9106 2020-12-26 stsp goto done;
5100 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5101 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5102 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5103 30837e32 2019-07-25 stsp if (error)
5104 5de5890b 2018-10-18 stsp goto done;
5107 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5108 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5110 5de5890b 2018-10-18 stsp free(in_repo_path);
5111 5de5890b 2018-10-18 stsp free(repo_path);
5112 5de5890b 2018-10-18 stsp free(cwd);
5113 5de5890b 2018-10-18 stsp free(commit_id);
5114 7a2c19d6 2019-02-05 stsp if (worktree)
5115 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5116 5de5890b 2018-10-18 stsp if (repo) {
5117 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
5118 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
5119 5de5890b 2018-10-18 stsp if (error == NULL)
5120 5de5890b 2018-10-18 stsp error = repo_error;
5122 5de5890b 2018-10-18 stsp return error;
5125 6bad629b 2019-02-04 stsp __dead static void
5126 6bad629b 2019-02-04 stsp usage_status(void)
5128 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
5129 081470ac 2020-08-13 stsp getprogname());
5133 b72f483a 2019-02-05 stsp static const struct got_error *
5134 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5135 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5136 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5137 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5139 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5140 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5141 081470ac 2020-08-13 stsp if (arg) {
5142 081470ac 2020-08-13 stsp char *status_codes = arg;
5143 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5145 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5146 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5147 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5150 081470ac 2020-08-13 stsp if (i == ncodes)
5151 081470ac 2020-08-13 stsp return NULL;
5153 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5154 b72f483a 2019-02-05 stsp return NULL;
5157 6bad629b 2019-02-04 stsp static const struct got_error *
5158 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5160 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5161 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5162 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5163 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5164 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5165 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5166 081470ac 2020-08-13 stsp int ch, i;
5168 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5170 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
5171 6bad629b 2019-02-04 stsp switch (ch) {
5173 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5174 081470ac 2020-08-13 stsp switch (optarg[i]) {
5175 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5176 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5177 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5178 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5179 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5180 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5181 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5182 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5183 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5186 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5187 081470ac 2020-08-13 stsp optarg[i]);
5190 081470ac 2020-08-13 stsp status_codes = optarg;
5193 2deda0b9 2019-03-07 stsp usage_status();
5194 6bad629b 2019-02-04 stsp /* NOTREACHED */
5198 6bad629b 2019-02-04 stsp argc -= optind;
5199 6bad629b 2019-02-04 stsp argv += optind;
5201 6bad629b 2019-02-04 stsp #ifndef PROFILE
5202 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5203 6bad629b 2019-02-04 stsp NULL) == -1)
5204 6bad629b 2019-02-04 stsp err(1, "pledge");
5206 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5207 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5208 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5209 927df6b7 2019-02-10 stsp goto done;
5212 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5213 fa51e947 2020-03-27 stsp if (error) {
5214 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5215 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5216 a5edda0a 2019-07-27 stsp goto done;
5219 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5221 6bad629b 2019-02-04 stsp if (error != NULL)
5222 6bad629b 2019-02-04 stsp goto done;
5224 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5225 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5226 087fb88c 2019-08-04 stsp if (error)
5227 087fb88c 2019-08-04 stsp goto done;
5229 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5230 6bad629b 2019-02-04 stsp if (error)
5231 6bad629b 2019-02-04 stsp goto done;
5233 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5234 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5236 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5237 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5238 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5239 927df6b7 2019-02-10 stsp free(cwd);
5240 d0eebce4 2019-03-11 stsp return error;
5243 d0eebce4 2019-03-11 stsp __dead static void
5244 d0eebce4 2019-03-11 stsp usage_ref(void)
5246 d0eebce4 2019-03-11 stsp fprintf(stderr,
5247 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5248 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5249 d0eebce4 2019-03-11 stsp getprogname());
5253 d0eebce4 2019-03-11 stsp static const struct got_error *
5254 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5256 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5257 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5258 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5260 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5261 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5263 d0eebce4 2019-03-11 stsp return err;
5265 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5266 d0eebce4 2019-03-11 stsp char *refstr;
5267 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5268 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5269 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5270 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5271 d0eebce4 2019-03-11 stsp free(refstr);
5274 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5275 d0eebce4 2019-03-11 stsp return NULL;
5278 d0eebce4 2019-03-11 stsp static const struct got_error *
5279 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5281 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5282 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5284 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5286 d0eebce4 2019-03-11 stsp return err;
5288 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5289 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5290 d0eebce4 2019-03-11 stsp return err;
5293 d0eebce4 2019-03-11 stsp static const struct got_error *
5294 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5296 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5297 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5298 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5301 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5302 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5303 d1644381 2019-07-14 stsp * an unintended typo.
5305 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5306 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5308 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5310 d83d9d5c 2019-05-13 stsp if (err) {
5311 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5313 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5314 d83d9d5c 2019-05-13 stsp return err;
5315 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5317 d83d9d5c 2019-05-13 stsp return err;
5318 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5319 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5321 d83d9d5c 2019-05-13 stsp return err;
5324 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5326 d0eebce4 2019-03-11 stsp goto done;
5328 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5331 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5333 d1c1ae5f 2019-08-12 stsp return err;
5336 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5337 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5339 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5340 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5341 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5344 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5345 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5346 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5348 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5349 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5351 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5353 d1c1ae5f 2019-08-12 stsp return err;
5355 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5357 d1c1ae5f 2019-08-12 stsp goto done;
5359 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5361 d1c1ae5f 2019-08-12 stsp if (target_ref)
5362 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5364 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5365 d0eebce4 2019-03-11 stsp return err;
5368 d0eebce4 2019-03-11 stsp static const struct got_error *
5369 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5371 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5372 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5373 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5374 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5375 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5376 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5377 b2070a3f 2020-03-22 stsp char *refname = NULL;
5379 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5380 d0eebce4 2019-03-11 stsp switch (ch) {
5382 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5385 e31abbf2 2020-03-22 stsp do_delete = 1;
5388 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5389 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5390 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5392 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5395 d0eebce4 2019-03-11 stsp do_list = 1;
5398 e31abbf2 2020-03-22 stsp symref_target = optarg;
5401 d0eebce4 2019-03-11 stsp usage_ref();
5402 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5406 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5407 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5408 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5409 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5410 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5411 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5412 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5413 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5414 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5415 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5416 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5417 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5419 d0eebce4 2019-03-11 stsp argc -= optind;
5420 d0eebce4 2019-03-11 stsp argv += optind;
5422 e31abbf2 2020-03-22 stsp if (do_list) {
5423 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5424 d0eebce4 2019-03-11 stsp usage_ref();
5425 b2070a3f 2020-03-22 stsp if (argc == 1) {
5426 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5427 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5428 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5429 b2070a3f 2020-03-22 stsp goto done;
5433 e31abbf2 2020-03-22 stsp if (argc != 1)
5434 e31abbf2 2020-03-22 stsp usage_ref();
5435 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5436 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5437 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5438 b2070a3f 2020-03-22 stsp goto done;
5442 b2070a3f 2020-03-22 stsp if (refname)
5443 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5445 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5446 e0b57350 2019-03-12 stsp if (do_list) {
5447 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5448 e0b57350 2019-03-12 stsp NULL) == -1)
5449 e0b57350 2019-03-12 stsp err(1, "pledge");
5451 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5452 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5453 e0b57350 2019-03-12 stsp err(1, "pledge");
5456 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5457 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5458 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5459 d0eebce4 2019-03-11 stsp goto done;
5462 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5463 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5464 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5465 d0eebce4 2019-03-11 stsp goto done;
5467 d0eebce4 2019-03-11 stsp error = NULL;
5468 d0eebce4 2019-03-11 stsp if (worktree) {
5469 d0eebce4 2019-03-11 stsp repo_path =
5470 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5471 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5473 d0eebce4 2019-03-11 stsp if (error)
5474 d0eebce4 2019-03-11 stsp goto done;
5476 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5477 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5478 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5479 d0eebce4 2019-03-11 stsp goto done;
5484 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5485 d0eebce4 2019-03-11 stsp if (error != NULL)
5486 d0eebce4 2019-03-11 stsp goto done;
5488 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5489 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5490 c02c541e 2019-03-29 stsp if (error)
5491 c02c541e 2019-03-29 stsp goto done;
5493 d0eebce4 2019-03-11 stsp if (do_list)
5494 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5495 e31abbf2 2020-03-22 stsp else if (do_delete)
5496 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5497 e31abbf2 2020-03-22 stsp else if (symref_target)
5498 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5500 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5501 e31abbf2 2020-03-22 stsp usage_ref();
5502 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5505 b2070a3f 2020-03-22 stsp free(refname);
5507 4e759de4 2019-06-26 stsp got_repo_close(repo);
5508 4e759de4 2019-06-26 stsp if (worktree)
5509 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5510 4e759de4 2019-06-26 stsp free(cwd);
5511 4e759de4 2019-06-26 stsp free(repo_path);
5512 4e759de4 2019-06-26 stsp return error;
5515 4e759de4 2019-06-26 stsp __dead static void
5516 4e759de4 2019-06-26 stsp usage_branch(void)
5518 4e759de4 2019-06-26 stsp fprintf(stderr,
5519 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5520 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5524 4e759de4 2019-06-26 stsp static const struct got_error *
5525 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5526 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5528 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5529 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5530 4e99b47e 2019-10-04 stsp char *refstr;
5532 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5533 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5534 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5535 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5537 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5539 4e99b47e 2019-10-04 stsp return err;
5540 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5541 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5542 4e99b47e 2019-10-04 stsp marker = "* ";
5544 4e99b47e 2019-10-04 stsp marker = "~ ";
5548 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5549 4e99b47e 2019-10-04 stsp refname += 11;
5550 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5551 4e99b47e 2019-10-04 stsp refname += 18;
5552 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5553 34d4e04c 2021-02-08 stsp refname += 13;
5555 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5556 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5557 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5559 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5560 4e99b47e 2019-10-04 stsp free(refstr);
5561 4e99b47e 2019-10-04 stsp return NULL;
5564 4e99b47e 2019-10-04 stsp static const struct got_error *
5565 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5567 ad89fa31 2019-10-04 stsp const char *refname;
5569 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5570 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5572 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5574 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5575 ad89fa31 2019-10-04 stsp refname += 11;
5576 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5577 ad89fa31 2019-10-04 stsp refname += 18;
5579 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5581 ad89fa31 2019-10-04 stsp return NULL;
5584 ad89fa31 2019-10-04 stsp static const struct got_error *
5585 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5587 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5588 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5589 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5590 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5591 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5593 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5595 4e99b47e 2019-10-04 stsp if (worktree) {
5596 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5597 4e99b47e 2019-10-04 stsp worktree);
5599 4e99b47e 2019-10-04 stsp return err;
5601 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5602 4e99b47e 2019-10-04 stsp worktree);
5604 4e99b47e 2019-10-04 stsp return err;
5606 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5607 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5608 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5610 4e99b47e 2019-10-04 stsp return err;
5611 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5612 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5616 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5617 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5619 4e759de4 2019-06-26 stsp return err;
5621 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5622 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5624 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5626 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5627 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5629 34d4e04c 2021-02-08 stsp return err;
5631 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5632 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5634 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5636 4e759de4 2019-06-26 stsp return NULL;
5639 4e759de4 2019-06-26 stsp static const struct got_error *
5640 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5641 45cd4e47 2019-08-25 stsp const char *branch_name)
5643 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5644 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5645 4e759de4 2019-06-26 stsp char *refname;
5647 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5648 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5650 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5652 4e759de4 2019-06-26 stsp goto done;
5654 45cd4e47 2019-08-25 stsp if (worktree &&
5655 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5656 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5657 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5658 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5659 45cd4e47 2019-08-25 stsp goto done;
5662 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5665 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5666 4e759de4 2019-06-26 stsp free(refname);
5667 4e759de4 2019-06-26 stsp return err;
5670 4e759de4 2019-06-26 stsp static const struct got_error *
5671 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5672 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5674 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5675 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5676 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5679 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5680 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5681 d3f84d51 2019-07-11 stsp * an unintended typo.
5683 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5684 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5686 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5687 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5688 62d463ca 2020-10-20 naddy goto done;
5691 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5692 4e759de4 2019-06-26 stsp if (err == NULL) {
5693 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5694 4e759de4 2019-06-26 stsp goto done;
5695 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5696 4e759de4 2019-06-26 stsp goto done;
5698 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5700 4e759de4 2019-06-26 stsp goto done;
5702 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5705 4e759de4 2019-06-26 stsp got_ref_close(ref);
5706 4e759de4 2019-06-26 stsp free(base_refname);
5707 4e759de4 2019-06-26 stsp free(refname);
5708 4e759de4 2019-06-26 stsp return err;
5711 4e759de4 2019-06-26 stsp static const struct got_error *
5712 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5714 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5715 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5716 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5717 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5718 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5719 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5720 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5721 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5722 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5723 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5724 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5726 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5728 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5729 4e759de4 2019-06-26 stsp switch (ch) {
5731 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5734 4e759de4 2019-06-26 stsp delref = optarg;
5737 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5738 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5739 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5741 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5744 4e759de4 2019-06-26 stsp do_list = 1;
5747 da76fce2 2020-02-24 stsp do_update = 0;
5750 4e759de4 2019-06-26 stsp usage_branch();
5751 4e759de4 2019-06-26 stsp /* NOTREACHED */
5755 4e759de4 2019-06-26 stsp if (do_list && delref)
5756 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5758 4e759de4 2019-06-26 stsp argc -= optind;
5759 4e759de4 2019-06-26 stsp argv += optind;
5761 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5762 ad89fa31 2019-10-04 stsp do_show = 1;
5764 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5765 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5767 4e759de4 2019-06-26 stsp if (do_list || delref) {
5768 4e759de4 2019-06-26 stsp if (argc > 0)
5769 4e759de4 2019-06-26 stsp usage_branch();
5770 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5771 4e759de4 2019-06-26 stsp usage_branch();
5773 4e759de4 2019-06-26 stsp #ifndef PROFILE
5774 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5775 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5776 4e759de4 2019-06-26 stsp NULL) == -1)
5777 4e759de4 2019-06-26 stsp err(1, "pledge");
5779 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5780 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5781 4e759de4 2019-06-26 stsp err(1, "pledge");
5784 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5785 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5786 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5787 4e759de4 2019-06-26 stsp goto done;
5790 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5791 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5792 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5793 4e759de4 2019-06-26 stsp goto done;
5795 4e759de4 2019-06-26 stsp error = NULL;
5796 4e759de4 2019-06-26 stsp if (worktree) {
5797 4e759de4 2019-06-26 stsp repo_path =
5798 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5799 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5800 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5801 4e759de4 2019-06-26 stsp if (error)
5802 4e759de4 2019-06-26 stsp goto done;
5804 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5805 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5806 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5807 4e759de4 2019-06-26 stsp goto done;
5812 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5813 4e759de4 2019-06-26 stsp if (error != NULL)
5814 4e759de4 2019-06-26 stsp goto done;
5816 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5817 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5818 4e759de4 2019-06-26 stsp if (error)
5819 4e759de4 2019-06-26 stsp goto done;
5821 ad89fa31 2019-10-04 stsp if (do_show)
5822 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5823 ad89fa31 2019-10-04 stsp else if (do_list)
5824 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5825 4e759de4 2019-06-26 stsp else if (delref)
5826 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5828 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5829 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5830 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5832 84de9106 2020-12-26 stsp if (error)
5833 84de9106 2020-12-26 stsp goto done;
5834 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5835 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5836 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5837 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5838 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5839 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5840 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5841 a74f7e83 2019-11-10 stsp if (error)
5842 a74f7e83 2019-11-10 stsp goto done;
5843 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5844 da76fce2 2020-02-24 stsp if (error)
5845 da76fce2 2020-02-24 stsp goto done;
5846 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5847 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5848 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5850 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5851 da76fce2 2020-02-24 stsp if (error)
5852 da76fce2 2020-02-24 stsp goto done;
5853 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5854 da76fce2 2020-02-24 stsp worktree);
5855 da76fce2 2020-02-24 stsp if (error)
5856 da76fce2 2020-02-24 stsp goto done;
5857 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5859 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5860 da76fce2 2020-02-24 stsp goto done;
5862 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5863 da76fce2 2020-02-24 stsp free(branch_refname);
5864 da76fce2 2020-02-24 stsp if (error)
5865 da76fce2 2020-02-24 stsp goto done;
5866 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5868 da76fce2 2020-02-24 stsp if (error)
5869 da76fce2 2020-02-24 stsp goto done;
5870 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5871 da76fce2 2020-02-24 stsp commit_id);
5872 da76fce2 2020-02-24 stsp if (error)
5873 da76fce2 2020-02-24 stsp goto done;
5874 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5875 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5876 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5878 da76fce2 2020-02-24 stsp if (error)
5879 da76fce2 2020-02-24 stsp goto done;
5880 9627c110 2020-04-18 stsp if (upa.did_something)
5881 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5882 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5887 da76fce2 2020-02-24 stsp got_ref_close(ref);
5889 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5890 d0eebce4 2019-03-11 stsp if (worktree)
5891 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5892 d0eebce4 2019-03-11 stsp free(cwd);
5893 d0eebce4 2019-03-11 stsp free(repo_path);
5894 da76fce2 2020-02-24 stsp free(commit_id);
5895 da76fce2 2020-02-24 stsp free(commit_id_str);
5896 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5897 da76fce2 2020-02-24 stsp free((char *)pe->path);
5898 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5899 d00136be 2019-03-26 stsp return error;
5903 d00136be 2019-03-26 stsp __dead static void
5904 8e7bd50a 2019-08-22 stsp usage_tag(void)
5906 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5907 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5908 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5913 b8bad2ba 2019-08-23 stsp static const struct got_error *
5914 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5916 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5917 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5918 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5919 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5920 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5922 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5923 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5924 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5925 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5927 b8bad2ba 2019-08-23 stsp return err;
5928 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5931 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5934 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5935 b8bad2ba 2019-08-23 stsp free(re_id);
5938 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5939 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5942 b8bad2ba 2019-08-23 stsp while (se) {
5943 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5946 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5947 b8bad2ba 2019-08-23 stsp free(se_id);
5950 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5951 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5953 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5954 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5956 b8bad2ba 2019-08-23 stsp return err;
5957 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5960 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5965 b8bad2ba 2019-08-23 stsp return err;
5969 b8bad2ba 2019-08-23 stsp static const struct got_error *
5970 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5972 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5973 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5974 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5976 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5978 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5980 8e7bd50a 2019-08-22 stsp return err;
5982 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5983 8e7bd50a 2019-08-22 stsp const char *refname;
5984 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5985 8e7bd50a 2019-08-22 stsp char datebuf[26];
5986 d4efa91b 2020-01-14 stsp const char *tagger;
5987 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5988 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5989 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5990 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5992 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5993 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5995 8e7bd50a 2019-08-22 stsp refname += 10;
5996 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5997 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5998 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6001 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6002 8e7bd50a 2019-08-22 stsp free(refstr);
6004 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6007 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6008 d4efa91b 2020-01-14 stsp if (err) {
6009 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6013 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6014 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6015 d4efa91b 2020-01-14 stsp if (err) {
6019 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6020 d4efa91b 2020-01-14 stsp tagger_time =
6021 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6022 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6028 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6029 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6030 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6031 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6035 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6036 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6037 2417344c 2019-08-23 stsp if (datestr)
6038 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6039 d4efa91b 2020-01-14 stsp if (commit)
6040 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6042 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6043 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6044 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6047 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6048 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6051 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6052 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6055 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6056 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6063 8e7bd50a 2019-08-22 stsp free(id_str);
6064 d4efa91b 2020-01-14 stsp if (commit) {
6065 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6068 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6070 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6071 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6072 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6073 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6078 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6080 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6082 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6083 8e7bd50a 2019-08-22 stsp } while (line);
6084 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6087 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6088 8e7bd50a 2019-08-22 stsp return NULL;
6091 8e7bd50a 2019-08-22 stsp static const struct got_error *
6092 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6093 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6095 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6096 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6097 f372d5cd 2019-10-21 stsp char *editor = NULL;
6098 1601cb9f 2020-09-11 naddy int initial_content_len;
6099 8e7bd50a 2019-08-22 stsp int fd = -1;
6101 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6102 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6103 8e7bd50a 2019-08-22 stsp goto done;
6106 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6107 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6108 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6109 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6110 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6111 8e7bd50a 2019-08-22 stsp goto done;
6114 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6116 8e7bd50a 2019-08-22 stsp goto done;
6118 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6119 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6120 97972933 2020-09-11 stsp goto done;
6123 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6125 8e7bd50a 2019-08-22 stsp goto done;
6126 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6127 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6129 8e7bd50a 2019-08-22 stsp free(initial_content);
6130 8e7bd50a 2019-08-22 stsp free(template);
6131 8e7bd50a 2019-08-22 stsp free(editor);
6133 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6134 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6136 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6137 59f86c76 2020-09-11 stsp if (err == NULL)
6138 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6139 59f86c76 2020-09-11 stsp if (err) {
6140 59f86c76 2020-09-11 stsp free(*tagmsg);
6141 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6143 8e7bd50a 2019-08-22 stsp return err;
6146 8e7bd50a 2019-08-22 stsp static const struct got_error *
6147 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6148 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6150 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6151 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6152 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6153 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6154 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6155 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6156 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6157 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6159 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6162 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6163 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6164 8e7bd50a 2019-08-22 stsp * an unintended typo.
6166 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6167 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6169 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6171 8e7bd50a 2019-08-22 stsp return err;
6173 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6175 84de9106 2020-12-26 stsp goto done;
6177 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6178 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6180 8e7bd50a 2019-08-22 stsp goto done;
6182 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6184 8e7bd50a 2019-08-22 stsp goto done;
6186 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6187 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6188 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6189 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6190 62d463ca 2020-10-20 naddy goto done;
6192 8e7bd50a 2019-08-22 stsp tag_name += 10;
6193 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6194 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6195 62d463ca 2020-10-20 naddy goto done;
6198 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6199 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6200 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6201 8e7bd50a 2019-08-22 stsp goto done;
6202 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6203 8e7bd50a 2019-08-22 stsp goto done;
6205 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6206 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6207 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6208 f372d5cd 2019-10-21 stsp if (err) {
6209 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6210 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6211 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6212 8e7bd50a 2019-08-22 stsp goto done;
6216 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6217 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6218 f372d5cd 2019-10-21 stsp if (err) {
6219 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6220 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6221 8e7bd50a 2019-08-22 stsp goto done;
6224 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6225 f372d5cd 2019-10-21 stsp if (err) {
6226 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6227 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6228 8e7bd50a 2019-08-22 stsp goto done;
6231 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6232 f372d5cd 2019-10-21 stsp if (err) {
6233 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6234 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6235 f372d5cd 2019-10-21 stsp goto done;
6238 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6239 f372d5cd 2019-10-21 stsp if (err) {
6240 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6241 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6242 f372d5cd 2019-10-21 stsp goto done;
6244 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6246 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6247 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6248 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6249 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6250 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6251 f372d5cd 2019-10-21 stsp free(tag_id_str);
6253 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6254 8e7bd50a 2019-08-22 stsp free(commit_id);
6255 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6256 8e7bd50a 2019-08-22 stsp free(refname);
6257 8e7bd50a 2019-08-22 stsp free(tagmsg);
6258 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6259 aba9c984 2019-09-08 stsp free(tagger);
6260 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6261 8e7bd50a 2019-08-22 stsp return err;
6264 8e7bd50a 2019-08-22 stsp static const struct got_error *
6265 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6267 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6268 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6269 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6270 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6271 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6272 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6273 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6275 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6276 8e7bd50a 2019-08-22 stsp switch (ch) {
6278 80106605 2020-02-24 stsp commit_id_arg = optarg;
6281 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6284 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6285 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6286 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6288 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6291 8e7bd50a 2019-08-22 stsp do_list = 1;
6294 8e7bd50a 2019-08-22 stsp usage_tag();
6295 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6299 8e7bd50a 2019-08-22 stsp argc -= optind;
6300 8e7bd50a 2019-08-22 stsp argv += optind;
6302 8e7bd50a 2019-08-22 stsp if (do_list) {
6303 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6305 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6306 8e7bd50a 2019-08-22 stsp if (tagmsg)
6307 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6308 8e7bd50a 2019-08-22 stsp if (argc > 0)
6309 8e7bd50a 2019-08-22 stsp usage_tag();
6310 80106605 2020-02-24 stsp } else if (argc != 1)
6311 8e7bd50a 2019-08-22 stsp usage_tag();
6313 a2887370 2019-08-23 stsp tag_name = argv[0];
6315 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6316 8e7bd50a 2019-08-22 stsp if (do_list) {
6317 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6318 8e7bd50a 2019-08-22 stsp NULL) == -1)
6319 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6321 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6322 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6323 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6326 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6327 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6328 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6329 8e7bd50a 2019-08-22 stsp goto done;
6332 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6333 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6334 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6335 8e7bd50a 2019-08-22 stsp goto done;
6337 8e7bd50a 2019-08-22 stsp error = NULL;
6338 8e7bd50a 2019-08-22 stsp if (worktree) {
6339 8e7bd50a 2019-08-22 stsp repo_path =
6340 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6341 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6342 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6343 8e7bd50a 2019-08-22 stsp if (error)
6344 8e7bd50a 2019-08-22 stsp goto done;
6346 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6347 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6348 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6349 8e7bd50a 2019-08-22 stsp goto done;
6354 8e7bd50a 2019-08-22 stsp if (do_list) {
6355 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6356 c9956ddf 2019-09-08 stsp if (error != NULL)
6357 c9956ddf 2019-09-08 stsp goto done;
6358 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6359 8e7bd50a 2019-08-22 stsp if (error)
6360 8e7bd50a 2019-08-22 stsp goto done;
6361 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6363 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6364 c9956ddf 2019-09-08 stsp if (error)
6365 c9956ddf 2019-09-08 stsp goto done;
6366 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6367 c9956ddf 2019-09-08 stsp if (error != NULL)
6368 c9956ddf 2019-09-08 stsp goto done;
6370 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6371 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6372 8e7bd50a 2019-08-22 stsp if (error)
6373 8e7bd50a 2019-08-22 stsp goto done;
6376 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6377 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6378 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6379 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6380 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6381 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6382 8e7bd50a 2019-08-22 stsp if (error)
6383 8e7bd50a 2019-08-22 stsp goto done;
6384 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6385 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6386 8e7bd50a 2019-08-22 stsp if (error)
6387 8e7bd50a 2019-08-22 stsp goto done;
6388 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6389 8e7bd50a 2019-08-22 stsp free(commit_id);
6390 8e7bd50a 2019-08-22 stsp if (error)
6391 8e7bd50a 2019-08-22 stsp goto done;
6394 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6395 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6399 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6400 8e7bd50a 2019-08-22 stsp if (worktree)
6401 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6402 8e7bd50a 2019-08-22 stsp free(cwd);
6403 8e7bd50a 2019-08-22 stsp free(repo_path);
6404 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6405 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6406 8e7bd50a 2019-08-22 stsp return error;
6409 8e7bd50a 2019-08-22 stsp __dead static void
6410 d00136be 2019-03-26 stsp usage_add(void)
6412 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6413 022fae89 2019-12-06 tracey getprogname());
6417 d00136be 2019-03-26 stsp static const struct got_error *
6418 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6420 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6422 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6423 4e68cba3 2019-11-23 stsp return NULL;
6426 4e68cba3 2019-11-23 stsp static const struct got_error *
6427 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6429 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6430 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6431 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6432 1dd54920 2019-05-11 stsp char *cwd = NULL;
6433 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6434 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6435 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6437 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6439 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6440 d00136be 2019-03-26 stsp switch (ch) {
6441 022fae89 2019-12-06 tracey case 'I':
6442 022fae89 2019-12-06 tracey no_ignores = 1;
6445 4e68cba3 2019-11-23 stsp can_recurse = 1;
6448 d00136be 2019-03-26 stsp usage_add();
6449 d00136be 2019-03-26 stsp /* NOTREACHED */
6453 d00136be 2019-03-26 stsp argc -= optind;
6454 d00136be 2019-03-26 stsp argv += optind;
6456 43012d58 2019-07-14 stsp #ifndef PROFILE
6457 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6458 43012d58 2019-07-14 stsp NULL) == -1)
6459 43012d58 2019-07-14 stsp err(1, "pledge");
6461 723c305c 2019-05-11 jcs if (argc < 1)
6462 d00136be 2019-03-26 stsp usage_add();
6464 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6465 d00136be 2019-03-26 stsp if (cwd == NULL) {
6466 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6467 d00136be 2019-03-26 stsp goto done;
6470 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6471 fa51e947 2020-03-27 stsp if (error) {
6472 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6473 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6474 d00136be 2019-03-26 stsp goto done;
6477 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6479 031a5338 2019-03-26 stsp if (error != NULL)
6480 031a5338 2019-03-26 stsp goto done;
6482 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6483 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6484 d00136be 2019-03-26 stsp if (error)
6485 d00136be 2019-03-26 stsp goto done;
6487 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6488 6d022e97 2019-08-04 stsp if (error)
6489 022fae89 2019-12-06 tracey goto done;
6491 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6492 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6493 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6494 6d022e97 2019-08-04 stsp goto done;
6498 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6499 4e68cba3 2019-11-23 stsp char *ondisk_path;
6500 4e68cba3 2019-11-23 stsp struct stat sb;
6501 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6502 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6503 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6504 62d463ca 2020-10-20 naddy pe->path) == -1) {
6505 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6506 4e68cba3 2019-11-23 stsp goto done;
6508 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6509 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6510 4e68cba3 2019-11-23 stsp free(ondisk_path);
6513 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6514 4e68cba3 2019-11-23 stsp ondisk_path);
6515 4e68cba3 2019-11-23 stsp free(ondisk_path);
6516 4e68cba3 2019-11-23 stsp goto done;
6518 4e68cba3 2019-11-23 stsp free(ondisk_path);
6519 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6520 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6521 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6522 4e68cba3 2019-11-23 stsp goto done;
6527 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6528 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6531 031a5338 2019-03-26 stsp got_repo_close(repo);
6532 d00136be 2019-03-26 stsp if (worktree)
6533 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6534 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6535 1dd54920 2019-05-11 stsp free((char *)pe->path);
6536 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6537 2ec1f75b 2019-03-26 stsp free(cwd);
6538 2ec1f75b 2019-03-26 stsp return error;
6541 2ec1f75b 2019-03-26 stsp __dead static void
6542 648e4ef7 2019-07-09 stsp usage_remove(void)
6544 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6545 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6549 2a06fe5f 2019-08-24 stsp static const struct got_error *
6550 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6551 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6553 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6555 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6556 2a06fe5f 2019-08-24 stsp return NULL;
6557 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6558 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6559 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6560 2a06fe5f 2019-08-24 stsp return NULL;
6563 2ec1f75b 2019-03-26 stsp static const struct got_error *
6564 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6566 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6567 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6568 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6569 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6570 17ed4618 2019-06-02 stsp char *cwd = NULL;
6571 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6572 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6573 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6575 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6577 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6578 2ec1f75b 2019-03-26 stsp switch (ch) {
6580 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6582 70e3e7f5 2019-12-13 tracey case 'k':
6583 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6585 f2a9dc41 2019-12-13 tracey case 'R':
6586 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6589 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6590 766841c2 2020-08-13 stsp switch (optarg[i]) {
6591 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6592 766841c2 2020-08-13 stsp delete_local_mods = 1;
6594 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6597 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6598 766841c2 2020-08-13 stsp optarg[i]);
6601 766841c2 2020-08-13 stsp status_codes = optarg;
6604 f2a9dc41 2019-12-13 tracey usage_remove();
6605 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6609 2ec1f75b 2019-03-26 stsp argc -= optind;
6610 2ec1f75b 2019-03-26 stsp argv += optind;
6612 43012d58 2019-07-14 stsp #ifndef PROFILE
6613 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6614 43012d58 2019-07-14 stsp NULL) == -1)
6615 43012d58 2019-07-14 stsp err(1, "pledge");
6617 17ed4618 2019-06-02 stsp if (argc < 1)
6618 648e4ef7 2019-07-09 stsp usage_remove();
6620 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6621 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6622 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6623 2ec1f75b 2019-03-26 stsp goto done;
6625 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6626 fa51e947 2020-03-27 stsp if (error) {
6627 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6628 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6629 2ec1f75b 2019-03-26 stsp goto done;
6632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6635 2ec1f75b 2019-03-26 stsp goto done;
6637 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6638 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6639 2ec1f75b 2019-03-26 stsp if (error)
6640 2ec1f75b 2019-03-26 stsp goto done;
6642 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6643 6d022e97 2019-08-04 stsp if (error)
6644 6d022e97 2019-08-04 stsp goto done;
6646 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6647 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6648 f2a9dc41 2019-12-13 tracey struct stat sb;
6649 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6650 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6651 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6652 62d463ca 2020-10-20 naddy pe->path) == -1) {
6653 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6654 f2a9dc41 2019-12-13 tracey goto done;
6656 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6657 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6658 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6659 f2a9dc41 2019-12-13 tracey continue;
6661 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6662 f2a9dc41 2019-12-13 tracey ondisk_path);
6663 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6664 f2a9dc41 2019-12-13 tracey goto done;
6666 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6667 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6668 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6669 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6670 f2a9dc41 2019-12-13 tracey goto done;
6675 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6676 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6677 766841c2 2020-08-13 stsp repo, keep_on_disk);
6680 a129376b 2019-03-28 stsp got_repo_close(repo);
6681 a129376b 2019-03-28 stsp if (worktree)
6682 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6683 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6684 17ed4618 2019-06-02 stsp free((char *)pe->path);
6685 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6686 a129376b 2019-03-28 stsp free(cwd);
6687 a129376b 2019-03-28 stsp return error;
6690 a129376b 2019-03-28 stsp __dead static void
6691 a129376b 2019-03-28 stsp usage_revert(void)
6693 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6694 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6698 1ee397ad 2019-07-12 stsp static const struct got_error *
6699 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6701 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6702 fb9704af 2020-01-27 stsp return NULL;
6704 a129376b 2019-03-28 stsp while (path[0] == '/')
6706 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6707 33aa809d 2019-08-08 stsp return NULL;
6710 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6711 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6712 33aa809d 2019-08-08 stsp const char *action;
6715 33aa809d 2019-08-08 stsp static const struct got_error *
6716 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6717 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6719 33aa809d 2019-08-08 stsp char *line = NULL;
6720 33aa809d 2019-08-08 stsp size_t linesize = 0;
6721 33aa809d 2019-08-08 stsp ssize_t linelen;
6723 33aa809d 2019-08-08 stsp switch (status) {
6724 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6725 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6727 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6728 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6730 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6731 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6732 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6733 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6734 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6735 33aa809d 2019-08-08 stsp printf("%s", line);
6736 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6737 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6738 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6739 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6740 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6743 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6746 33aa809d 2019-08-08 stsp return NULL;
6749 33aa809d 2019-08-08 stsp static const struct got_error *
6750 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6751 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6753 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6754 33aa809d 2019-08-08 stsp char *line = NULL;
6755 33aa809d 2019-08-08 stsp size_t linesize = 0;
6756 33aa809d 2019-08-08 stsp ssize_t linelen;
6757 33aa809d 2019-08-08 stsp int resp = ' ';
6758 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6760 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6762 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6764 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6765 33aa809d 2019-08-08 stsp a->action);
6767 33aa809d 2019-08-08 stsp return err;
6768 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6769 33aa809d 2019-08-08 stsp if (linelen == -1) {
6770 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6771 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6772 33aa809d 2019-08-08 stsp return NULL;
6774 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6776 33aa809d 2019-08-08 stsp *nl = '\0';
6777 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6778 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6779 33aa809d 2019-08-08 stsp printf("y\n");
6780 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6781 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6782 33aa809d 2019-08-08 stsp printf("n\n");
6783 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6784 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6785 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6786 33aa809d 2019-08-08 stsp printf("q\n");
6788 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6789 33aa809d 2019-08-08 stsp free(line);
6790 33aa809d 2019-08-08 stsp return NULL;
6793 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6794 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6795 33aa809d 2019-08-08 stsp a->action);
6797 33aa809d 2019-08-08 stsp return err;
6798 33aa809d 2019-08-08 stsp resp = getchar();
6799 33aa809d 2019-08-08 stsp if (resp == '\n')
6800 33aa809d 2019-08-08 stsp resp = getchar();
6801 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6802 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6803 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6804 33aa809d 2019-08-08 stsp resp = ' ';
6806 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6807 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6808 33aa809d 2019-08-08 stsp resp = ' ';
6812 33aa809d 2019-08-08 stsp if (resp == 'y')
6813 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6814 33aa809d 2019-08-08 stsp else if (resp == 'n')
6815 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6816 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6817 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6819 1ee397ad 2019-07-12 stsp return NULL;
6823 a129376b 2019-03-28 stsp static const struct got_error *
6824 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6826 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6827 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6828 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6829 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6830 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6831 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6832 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6833 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6834 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6835 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6837 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6839 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6840 a129376b 2019-03-28 stsp switch (ch) {
6842 33aa809d 2019-08-08 stsp pflag = 1;
6845 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6848 0f6d7415 2019-08-08 stsp can_recurse = 1;
6851 a129376b 2019-03-28 stsp usage_revert();
6852 a129376b 2019-03-28 stsp /* NOTREACHED */
6856 a129376b 2019-03-28 stsp argc -= optind;
6857 a129376b 2019-03-28 stsp argv += optind;
6859 43012d58 2019-07-14 stsp #ifndef PROFILE
6860 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6861 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6862 43012d58 2019-07-14 stsp err(1, "pledge");
6864 e20a8b6f 2019-06-04 stsp if (argc < 1)
6865 a129376b 2019-03-28 stsp usage_revert();
6866 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6867 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6869 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6870 a129376b 2019-03-28 stsp if (cwd == NULL) {
6871 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6872 a129376b 2019-03-28 stsp goto done;
6874 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6875 fa51e947 2020-03-27 stsp if (error) {
6876 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6877 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6878 2ec1f75b 2019-03-26 stsp goto done;
6881 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6883 a129376b 2019-03-28 stsp if (error != NULL)
6884 a129376b 2019-03-28 stsp goto done;
6886 33aa809d 2019-08-08 stsp if (patch_script_path) {
6887 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6888 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6889 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6890 33aa809d 2019-08-08 stsp patch_script_path);
6891 33aa809d 2019-08-08 stsp goto done;
6894 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6895 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6896 a129376b 2019-03-28 stsp if (error)
6897 a129376b 2019-03-28 stsp goto done;
6899 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6900 6d022e97 2019-08-04 stsp if (error)
6901 6d022e97 2019-08-04 stsp goto done;
6903 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6904 0f6d7415 2019-08-08 stsp char *ondisk_path;
6905 0f6d7415 2019-08-08 stsp struct stat sb;
6906 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6907 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6908 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6909 62d463ca 2020-10-20 naddy pe->path) == -1) {
6910 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6911 0f6d7415 2019-08-08 stsp goto done;
6913 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6914 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6915 0f6d7415 2019-08-08 stsp free(ondisk_path);
6918 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6919 0f6d7415 2019-08-08 stsp ondisk_path);
6920 0f6d7415 2019-08-08 stsp free(ondisk_path);
6921 0f6d7415 2019-08-08 stsp goto done;
6923 0f6d7415 2019-08-08 stsp free(ondisk_path);
6924 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6925 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6926 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6927 0f6d7415 2019-08-08 stsp goto done;
6932 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6933 33aa809d 2019-08-08 stsp cpa.action = "revert";
6934 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6935 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6937 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6938 33aa809d 2019-08-08 stsp error == NULL)
6939 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6941 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6942 2ec1f75b 2019-03-26 stsp if (worktree)
6943 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6944 2ec1f75b 2019-03-26 stsp free(path);
6945 d00136be 2019-03-26 stsp free(cwd);
6946 6bad629b 2019-02-04 stsp return error;
6949 c4296144 2019-05-09 stsp __dead static void
6950 c4296144 2019-05-09 stsp usage_commit(void)
6952 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
6953 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
6957 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6958 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6959 28cf319f 2021-01-28 stsp const char *prepared_log;
6960 28cf319f 2021-01-28 stsp int non_interactive;
6961 e2ba3d07 2019-05-13 stsp const char *editor;
6962 e0870e44 2019-05-13 stsp const char *worktree_path;
6963 76d98825 2019-06-03 stsp const char *branch_name;
6964 314a6357 2019-05-13 stsp const char *repo_path;
6965 e0870e44 2019-05-13 stsp char *logmsg_path;
6969 28cf319f 2021-01-28 stsp static const struct got_error *
6970 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
6972 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
6973 28cf319f 2021-01-28 stsp FILE *f = NULL;
6974 28cf319f 2021-01-28 stsp struct stat sb;
6977 28cf319f 2021-01-28 stsp *logmsg = NULL;
6978 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
6980 28cf319f 2021-01-28 stsp f = fopen(path, "r");
6981 28cf319f 2021-01-28 stsp if (f == NULL)
6982 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
6984 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
6985 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
6986 28cf319f 2021-01-28 stsp goto done;
6988 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
6989 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6990 28cf319f 2021-01-28 stsp goto done;
6993 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
6994 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
6995 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
6996 28cf319f 2021-01-28 stsp goto done;
6999 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7000 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7001 28cf319f 2021-01-28 stsp if (ferror(f))
7002 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7004 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7005 28cf319f 2021-01-28 stsp goto done;
7007 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7009 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7010 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7011 28cf319f 2021-01-28 stsp if (err) {
7012 28cf319f 2021-01-28 stsp free(*logmsg);
7013 28cf319f 2021-01-28 stsp *logmsg = NULL;
7015 28cf319f 2021-01-28 stsp return err;
7019 33ad4cbe 2019-05-12 jcs static const struct got_error *
7020 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7023 76d98825 2019-06-03 stsp char *initial_content = NULL;
7024 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7025 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7026 e0870e44 2019-05-13 stsp char *template = NULL;
7027 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7028 1601cb9f 2020-09-11 naddy int initial_content_len;
7029 97972933 2020-09-11 stsp int fd = -1;
7030 33ad4cbe 2019-05-12 jcs size_t len;
7032 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7033 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7034 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7035 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7036 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7037 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7038 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7039 33ad4cbe 2019-05-12 jcs return NULL;
7040 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7041 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7043 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7044 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7046 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7048 28cf319f 2021-01-28 stsp goto done;
7050 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7051 28cf319f 2021-01-28 stsp char *msg;
7052 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7054 28cf319f 2021-01-28 stsp goto done;
7055 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7056 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7057 28cf319f 2021-01-28 stsp free(msg);
7058 28cf319f 2021-01-28 stsp goto done;
7060 28cf319f 2021-01-28 stsp free(msg);
7063 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7064 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7065 1601cb9f 2020-09-11 naddy a->branch_name);
7066 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7067 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7068 e0870e44 2019-05-13 stsp goto done;
7071 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7072 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7073 97972933 2020-09-11 stsp goto done;
7076 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7077 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7078 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7079 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7080 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7083 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7084 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7086 76d98825 2019-06-03 stsp free(initial_content);
7087 e0870e44 2019-05-13 stsp free(template);
7089 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7090 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7092 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7093 59f86c76 2020-09-11 stsp if (err == NULL)
7094 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7095 59f86c76 2020-09-11 stsp if (err) {
7096 59f86c76 2020-09-11 stsp free(*logmsg);
7097 59f86c76 2020-09-11 stsp *logmsg = NULL;
7099 33ad4cbe 2019-05-12 jcs return err;
7102 c4296144 2019-05-09 stsp static const struct got_error *
7103 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7105 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7106 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7107 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7108 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7109 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7110 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7111 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7112 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7113 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7114 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7115 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7116 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7118 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7119 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7121 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7122 c4296144 2019-05-09 stsp switch (ch) {
7124 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7125 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7126 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7127 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7128 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7132 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7133 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7134 c4296144 2019-05-09 stsp logmsg = optarg;
7137 28cf319f 2021-01-28 stsp non_interactive = 1;
7140 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7143 c4296144 2019-05-09 stsp usage_commit();
7144 c4296144 2019-05-09 stsp /* NOTREACHED */
7148 c4296144 2019-05-09 stsp argc -= optind;
7149 c4296144 2019-05-09 stsp argv += optind;
7151 43012d58 2019-07-14 stsp #ifndef PROFILE
7152 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7153 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7154 43012d58 2019-07-14 stsp err(1, "pledge");
7156 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7157 c4296144 2019-05-09 stsp if (cwd == NULL) {
7158 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7159 c4296144 2019-05-09 stsp goto done;
7161 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7162 fa51e947 2020-03-27 stsp if (error) {
7163 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7164 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7165 7d5807f4 2019-07-11 stsp goto done;
7168 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7169 c4296144 2019-05-09 stsp if (error)
7170 a698f62e 2019-07-25 stsp goto done;
7171 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7172 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7173 c4296144 2019-05-09 stsp goto done;
7176 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7177 916f288c 2019-07-30 stsp worktree);
7178 5c1e53bc 2019-07-28 stsp if (error)
7179 5c1e53bc 2019-07-28 stsp goto done;
7181 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7182 c9956ddf 2019-09-08 stsp if (error)
7183 c9956ddf 2019-09-08 stsp goto done;
7184 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7185 c9956ddf 2019-09-08 stsp gitconfig_path);
7186 c4296144 2019-05-09 stsp if (error != NULL)
7187 c4296144 2019-05-09 stsp goto done;
7189 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7190 aba9c984 2019-09-08 stsp if (error)
7191 aba9c984 2019-09-08 stsp return error;
7194 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7195 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7197 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7198 314a6357 2019-05-13 stsp error = get_editor(&editor);
7200 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7201 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7202 c4296144 2019-05-09 stsp if (error)
7203 c4296144 2019-05-09 stsp goto done;
7205 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7206 087fb88c 2019-08-04 stsp if (error)
7207 087fb88c 2019-08-04 stsp goto done;
7209 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7210 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7211 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7212 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7213 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7214 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7215 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7216 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7217 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7218 916f288c 2019-07-30 stsp goto done;
7220 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7222 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7223 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7224 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7225 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7226 e0870e44 2019-05-13 stsp if (error) {
7227 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7228 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7229 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7230 c4296144 2019-05-09 stsp goto done;
7233 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7234 c4296144 2019-05-09 stsp if (error)
7235 c4296144 2019-05-09 stsp goto done;
7236 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7238 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7239 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7240 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7241 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7242 7266f21f 2019-10-21 stsp error == NULL)
7243 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7244 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7246 c4296144 2019-05-09 stsp got_repo_close(repo);
7247 c4296144 2019-05-09 stsp if (worktree)
7248 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7249 c4296144 2019-05-09 stsp free(cwd);
7250 c4296144 2019-05-09 stsp free(id_str);
7251 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7252 e2ba3d07 2019-05-13 stsp free(editor);
7253 aba9c984 2019-09-08 stsp free(author);
7254 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7255 234035bc 2019-06-01 stsp return error;
7258 234035bc 2019-06-01 stsp __dead static void
7259 234035bc 2019-06-01 stsp usage_cherrypick(void)
7261 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7265 234035bc 2019-06-01 stsp static const struct got_error *
7266 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7268 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7269 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7270 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7271 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7272 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7273 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7274 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7275 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7277 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7279 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7280 234035bc 2019-06-01 stsp switch (ch) {
7282 234035bc 2019-06-01 stsp usage_cherrypick();
7283 234035bc 2019-06-01 stsp /* NOTREACHED */
7287 234035bc 2019-06-01 stsp argc -= optind;
7288 234035bc 2019-06-01 stsp argv += optind;
7290 43012d58 2019-07-14 stsp #ifndef PROFILE
7291 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7292 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7293 43012d58 2019-07-14 stsp err(1, "pledge");
7295 234035bc 2019-06-01 stsp if (argc != 1)
7296 234035bc 2019-06-01 stsp usage_cherrypick();
7298 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7299 234035bc 2019-06-01 stsp if (cwd == NULL) {
7300 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7301 234035bc 2019-06-01 stsp goto done;
7303 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7304 fa51e947 2020-03-27 stsp if (error) {
7305 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7306 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7308 234035bc 2019-06-01 stsp goto done;
7311 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7313 234035bc 2019-06-01 stsp if (error != NULL)
7314 234035bc 2019-06-01 stsp goto done;
7316 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7317 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7318 234035bc 2019-06-01 stsp if (error)
7319 234035bc 2019-06-01 stsp goto done;
7321 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7322 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7323 234035bc 2019-06-01 stsp if (error != NULL) {
7324 234035bc 2019-06-01 stsp struct got_reference *ref;
7325 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7326 234035bc 2019-06-01 stsp goto done;
7327 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7328 234035bc 2019-06-01 stsp if (error != NULL)
7329 234035bc 2019-06-01 stsp goto done;
7330 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7331 234035bc 2019-06-01 stsp got_ref_close(ref);
7332 234035bc 2019-06-01 stsp if (error != NULL)
7333 234035bc 2019-06-01 stsp goto done;
7335 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7336 234035bc 2019-06-01 stsp if (error)
7337 234035bc 2019-06-01 stsp goto done;
7339 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7340 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7341 234035bc 2019-06-01 stsp if (error != NULL)
7342 234035bc 2019-06-01 stsp goto done;
7344 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7345 234035bc 2019-06-01 stsp if (error) {
7346 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7347 234035bc 2019-06-01 stsp goto done;
7348 234035bc 2019-06-01 stsp error = NULL;
7350 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7351 234035bc 2019-06-01 stsp goto done;
7354 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7355 234035bc 2019-06-01 stsp if (error)
7356 234035bc 2019-06-01 stsp goto done;
7357 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7358 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7359 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7360 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7362 234035bc 2019-06-01 stsp if (error != NULL)
7363 234035bc 2019-06-01 stsp goto done;
7365 9627c110 2020-04-18 stsp if (upa.did_something)
7366 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7367 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7369 234035bc 2019-06-01 stsp if (commit)
7370 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7371 234035bc 2019-06-01 stsp free(commit_id_str);
7372 234035bc 2019-06-01 stsp if (head_ref)
7373 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7374 234035bc 2019-06-01 stsp if (worktree)
7375 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7377 234035bc 2019-06-01 stsp got_repo_close(repo);
7378 c4296144 2019-05-09 stsp return error;
7381 5ef14e63 2019-06-02 stsp __dead static void
7382 5ef14e63 2019-06-02 stsp usage_backout(void)
7384 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7388 5ef14e63 2019-06-02 stsp static const struct got_error *
7389 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7391 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7392 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7393 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7394 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7395 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7396 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7397 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7398 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7400 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7402 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7403 5ef14e63 2019-06-02 stsp switch (ch) {
7405 5ef14e63 2019-06-02 stsp usage_backout();
7406 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7410 5ef14e63 2019-06-02 stsp argc -= optind;
7411 5ef14e63 2019-06-02 stsp argv += optind;
7413 43012d58 2019-07-14 stsp #ifndef PROFILE
7414 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7415 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7416 43012d58 2019-07-14 stsp err(1, "pledge");
7418 5ef14e63 2019-06-02 stsp if (argc != 1)
7419 5ef14e63 2019-06-02 stsp usage_backout();
7421 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7422 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7423 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7424 5ef14e63 2019-06-02 stsp goto done;
7426 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7427 fa51e947 2020-03-27 stsp if (error) {
7428 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7429 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7430 5ef14e63 2019-06-02 stsp goto done;
7433 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7435 5ef14e63 2019-06-02 stsp if (error != NULL)
7436 5ef14e63 2019-06-02 stsp goto done;
7438 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7439 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7440 5ef14e63 2019-06-02 stsp if (error)
7441 5ef14e63 2019-06-02 stsp goto done;
7443 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7444 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7445 5ef14e63 2019-06-02 stsp if (error != NULL) {
7446 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7447 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7448 5ef14e63 2019-06-02 stsp goto done;
7449 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7450 5ef14e63 2019-06-02 stsp if (error != NULL)
7451 5ef14e63 2019-06-02 stsp goto done;
7452 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7453 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7454 5ef14e63 2019-06-02 stsp if (error != NULL)
7455 5ef14e63 2019-06-02 stsp goto done;
7457 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7458 5ef14e63 2019-06-02 stsp if (error)
7459 5ef14e63 2019-06-02 stsp goto done;
7461 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7462 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7463 5ef14e63 2019-06-02 stsp if (error != NULL)
7464 5ef14e63 2019-06-02 stsp goto done;
7466 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7467 5ef14e63 2019-06-02 stsp if (error)
7468 5ef14e63 2019-06-02 stsp goto done;
7470 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7471 5ef14e63 2019-06-02 stsp if (error)
7472 5ef14e63 2019-06-02 stsp goto done;
7473 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7474 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7475 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7476 5ef14e63 2019-06-02 stsp goto done;
7479 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7480 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7481 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7482 5ef14e63 2019-06-02 stsp if (error != NULL)
7483 5ef14e63 2019-06-02 stsp goto done;
7485 9627c110 2020-04-18 stsp if (upa.did_something)
7486 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7487 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7489 5ef14e63 2019-06-02 stsp if (commit)
7490 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7491 5ef14e63 2019-06-02 stsp free(commit_id_str);
7492 5ef14e63 2019-06-02 stsp if (head_ref)
7493 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7494 818c7501 2019-07-11 stsp if (worktree)
7495 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7497 818c7501 2019-07-11 stsp got_repo_close(repo);
7498 818c7501 2019-07-11 stsp return error;
7501 818c7501 2019-07-11 stsp __dead static void
7502 818c7501 2019-07-11 stsp usage_rebase(void)
7504 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [branch]\n",
7505 af54c8f8 2019-07-11 stsp getprogname());
7510 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7513 0ebf8283 2019-07-24 stsp size_t len;
7515 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7516 0ebf8283 2019-07-24 stsp if (len > limit)
7517 0ebf8283 2019-07-24 stsp len = limit;
7518 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7519 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7521 0ebf8283 2019-07-24 stsp *nl = '\0';
7524 0ebf8283 2019-07-24 stsp static const struct got_error *
7525 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7527 5943eee2 2019-08-13 stsp const struct got_error *err;
7528 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7529 5943eee2 2019-08-13 stsp const char *s;
7531 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7533 5943eee2 2019-08-13 stsp return err;
7535 5943eee2 2019-08-13 stsp s = logmsg0;
7536 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7539 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7540 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7541 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7542 5943eee2 2019-08-13 stsp goto done;
7545 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7547 5943eee2 2019-08-13 stsp free(logmsg0);
7548 a0ea4fc0 2020-02-28 stsp return err;
7551 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7552 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7554 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7555 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7556 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7558 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7560 a0ea4fc0 2020-02-28 stsp return err;
7562 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7564 a0ea4fc0 2020-02-28 stsp goto done;
7566 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7568 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7570 a0ea4fc0 2020-02-28 stsp goto done;
7572 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7574 a0ea4fc0 2020-02-28 stsp free(id_str);
7575 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7576 a0ea4fc0 2020-02-28 stsp free(logmsg);
7577 5943eee2 2019-08-13 stsp return err;
7580 818c7501 2019-07-11 stsp static const struct got_error *
7581 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7582 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7584 818c7501 2019-07-11 stsp const struct got_error *err;
7585 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7587 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7589 818c7501 2019-07-11 stsp goto done;
7591 ff0d2220 2019-07-11 stsp if (new_id) {
7592 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7594 ff0d2220 2019-07-11 stsp goto done;
7597 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7598 ff0d2220 2019-07-11 stsp if (new_id_str)
7599 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7601 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7603 0ebf8283 2019-07-24 stsp goto done;
7605 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7606 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7608 818c7501 2019-07-11 stsp free(old_id_str);
7609 818c7501 2019-07-11 stsp free(new_id_str);
7610 272a1371 2020-02-28 stsp free(logmsg);
7611 818c7501 2019-07-11 stsp return err;
7614 1ee397ad 2019-07-12 stsp static const struct got_error *
7615 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7616 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7617 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
7618 e600f124 2021-03-21 stsp int create_backup)
7620 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7621 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7622 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
7625 818c7501 2019-07-11 stsp static const struct got_error *
7626 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7627 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7628 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7629 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7631 ff0d2220 2019-07-11 stsp const struct got_error *error;
7632 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7633 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7635 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7636 ff0d2220 2019-07-11 stsp if (error)
7637 ff0d2220 2019-07-11 stsp return error;
7639 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7640 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7641 ff0d2220 2019-07-11 stsp if (error) {
7642 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7643 ff0d2220 2019-07-11 stsp goto done;
7644 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7646 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7647 ff0d2220 2019-07-11 stsp free(new_commit_id);
7650 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7651 ff0d2220 2019-07-11 stsp return error;
7654 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7655 64c6d990 2019-07-11 stsp const char *path_prefix;
7656 64c6d990 2019-07-11 stsp size_t len;
7657 8ca9bd68 2019-07-25 stsp int errcode;
7660 64c6d990 2019-07-11 stsp static const struct got_error *
7661 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7662 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7663 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7664 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7666 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7668 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7669 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7670 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7672 64c6d990 2019-07-11 stsp return NULL;
7675 64c6d990 2019-07-11 stsp static const struct got_error *
7676 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7677 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7678 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7680 64c6d990 2019-07-11 stsp const struct got_error *err;
7681 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7682 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7683 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7685 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7686 64c6d990 2019-07-11 stsp return NULL;
7688 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7690 64c6d990 2019-07-11 stsp goto done;
7692 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7694 64c6d990 2019-07-11 stsp goto done;
7696 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7697 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7699 64c6d990 2019-07-11 stsp goto done;
7701 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7702 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7704 64c6d990 2019-07-11 stsp goto done;
7706 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7707 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7708 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7709 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7710 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7711 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7712 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7714 64c6d990 2019-07-11 stsp if (tree1)
7715 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7716 64c6d990 2019-07-11 stsp if (tree2)
7717 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7718 64c6d990 2019-07-11 stsp if (commit)
7719 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7720 64c6d990 2019-07-11 stsp if (parent_commit)
7721 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7722 64c6d990 2019-07-11 stsp return err;
7725 ff0d2220 2019-07-11 stsp static const struct got_error *
7726 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7727 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7728 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7729 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7730 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7732 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7733 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7734 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7735 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7736 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7738 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7740 af61c510 2019-07-19 stsp return err;
7742 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7743 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7745 af61c510 2019-07-19 stsp goto done;
7746 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7747 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7748 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7749 af61c510 2019-07-19 stsp if (err) {
7750 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7751 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7752 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7753 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7754 af61c510 2019-07-19 stsp "been reached?!?");
7756 ee780d5c 2020-01-04 stsp goto done;
7758 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7759 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7761 af61c510 2019-07-19 stsp goto done;
7763 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7765 af61c510 2019-07-19 stsp goto done;
7766 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7767 af61c510 2019-07-19 stsp commit_id = parent_id;
7771 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7772 af61c510 2019-07-19 stsp return err;
7775 e600f124 2021-03-21 stsp static const struct got_error *
7776 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
7778 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7779 e600f124 2021-03-21 stsp time_t committer_time;
7780 e600f124 2021-03-21 stsp struct tm tm;
7781 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
7782 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
7783 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
7785 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
7786 e600f124 2021-03-21 stsp if (localtime_r(&committer_time, &tm) == NULL)
7787 e600f124 2021-03-21 stsp return got_error_from_errno("localtime_r");
7788 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
7789 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
7791 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
7792 e600f124 2021-03-21 stsp if (author0 == NULL)
7793 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
7794 e600f124 2021-03-21 stsp author = author0;
7795 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
7796 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
7797 e600f124 2021-03-21 stsp author = smallerthan + 1;
7798 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
7800 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7802 e600f124 2021-03-21 stsp goto done;
7803 e600f124 2021-03-21 stsp logmsg = logmsg0;
7804 e600f124 2021-03-21 stsp while (*logmsg == '\n')
7806 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
7807 e600f124 2021-03-21 stsp if (newline)
7808 e600f124 2021-03-21 stsp *newline = '\0';
7810 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
7811 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
7812 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7814 e600f124 2021-03-21 stsp free(author0);
7815 e600f124 2021-03-21 stsp free(logmsg0);
7816 e600f124 2021-03-21 stsp return err;
7819 e600f124 2021-03-21 stsp static const struct got_error *
7820 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
7821 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
7822 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
7823 e600f124 2021-03-21 stsp struct got_repository *repo)
7825 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
7826 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
7827 e600f124 2021-03-21 stsp char *refs_str = NULL;
7828 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
7829 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
7830 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
7831 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
7832 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
7833 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
7834 e600f124 2021-03-21 stsp char *custom_refs_str;
7836 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
7837 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
7839 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
7840 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
7842 e600f124 2021-03-21 stsp goto done;
7844 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
7846 e600f124 2021-03-21 stsp goto done;
7848 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
7849 e600f124 2021-03-21 stsp if (refs) {
7850 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
7852 e600f124 2021-03-21 stsp goto done;
7855 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
7857 e600f124 2021-03-21 stsp goto done;
7859 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
7861 e600f124 2021-03-21 stsp goto done;
7863 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7864 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
7866 e600f124 2021-03-21 stsp goto done;
7868 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
7869 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
7870 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
7871 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
7872 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
7873 e600f124 2021-03-21 stsp free(refs_str);
7874 e600f124 2021-03-21 stsp refs_str = NULL;
7876 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
7878 e600f124 2021-03-21 stsp goto done;
7880 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
7882 e600f124 2021-03-21 stsp goto done;
7884 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
7886 e600f124 2021-03-21 stsp goto done;
7888 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
7889 e600f124 2021-03-21 stsp if (refs) {
7890 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
7892 e600f124 2021-03-21 stsp goto done;
7894 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
7895 e600f124 2021-03-21 stsp yca_id_str,
7896 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
7897 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
7900 e600f124 2021-03-21 stsp free(custom_refs_str);
7901 e600f124 2021-03-21 stsp free(new_commit_id);
7902 e600f124 2021-03-21 stsp free(refs_str);
7903 e600f124 2021-03-21 stsp free(yca_id);
7904 e600f124 2021-03-21 stsp free(yca_id_str);
7905 e600f124 2021-03-21 stsp free(yca_brief_str);
7906 e600f124 2021-03-21 stsp if (new_commit)
7907 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
7908 e600f124 2021-03-21 stsp if (yca_commit)
7909 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
7911 e600f124 2021-03-21 stsp return NULL;
7914 af61c510 2019-07-19 stsp static const struct got_error *
7915 e600f124 2021-03-21 stsp list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
7916 e600f124 2021-03-21 stsp struct got_repository *repo)
7918 e600f124 2021-03-21 stsp const struct got_error *err;
7919 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
7920 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
7921 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
7922 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7923 e600f124 2021-03-21 stsp char *branch_name = NULL;
7924 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
7925 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
7926 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
7928 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
7929 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
7931 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7933 e600f124 2021-03-21 stsp return err;
7935 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
7937 e600f124 2021-03-21 stsp goto done;
7939 e600f124 2021-03-21 stsp if (wanted_branch_name) {
7940 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
7941 e600f124 2021-03-21 stsp wanted_branch_name += 11;
7944 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
7945 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
7947 e600f124 2021-03-21 stsp goto done;
7949 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
7950 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
7951 e600f124 2021-03-21 stsp char *slash;
7953 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
7957 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
7958 e600f124 2021-03-21 stsp old_commit_id);
7962 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
7963 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
7964 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
7966 e600f124 2021-03-21 stsp while (refname[0] == '/')
7967 e600f124 2021-03-21 stsp refname++;
7969 e600f124 2021-03-21 stsp branch_name = strdup(refname);
7970 e600f124 2021-03-21 stsp if (branch_name == NULL) {
7971 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
7974 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
7975 e600f124 2021-03-21 stsp if (slash) {
7976 e600f124 2021-03-21 stsp *slash = '\0';
7977 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
7980 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
7981 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
7982 9e822917 2021-03-23 stsp wanted_branch_found = 1;
7983 e600f124 2021-03-21 stsp err = print_backup_ref(branch_name, refname,
7984 e600f124 2021-03-21 stsp old_commit_id, old_commit, refs_idmap, repo);
7989 e600f124 2021-03-21 stsp free(old_commit_id);
7990 e600f124 2021-03-21 stsp old_commit_id = NULL;
7991 e600f124 2021-03-21 stsp free(branch_name);
7992 e600f124 2021-03-21 stsp branch_name = NULL;
7993 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
7994 e600f124 2021-03-21 stsp old_commit = NULL;
7997 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
7998 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
7999 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8002 e600f124 2021-03-21 stsp if (refs_idmap)
8003 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8004 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8005 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8006 e600f124 2021-03-21 stsp free(old_commit_id);
8007 e600f124 2021-03-21 stsp free(branch_name);
8008 e600f124 2021-03-21 stsp if (old_commit)
8009 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8010 e600f124 2021-03-21 stsp return err;
8013 e600f124 2021-03-21 stsp static const struct got_error *
8014 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8016 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8017 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8018 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8019 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8020 818c7501 2019-07-11 stsp char *cwd = NULL;
8021 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8022 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8023 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8024 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8025 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8026 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8027 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8028 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8029 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8030 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8031 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8032 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8033 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8035 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
8036 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8038 e600f124 2021-03-21 stsp while ((ch = getopt(argc, argv, "acl")) != -1) {
8039 818c7501 2019-07-11 stsp switch (ch) {
8041 818c7501 2019-07-11 stsp abort_rebase = 1;
8044 818c7501 2019-07-11 stsp continue_rebase = 1;
8047 e600f124 2021-03-21 stsp list_backups = 1;
8050 818c7501 2019-07-11 stsp usage_rebase();
8051 818c7501 2019-07-11 stsp /* NOTREACHED */
8055 818c7501 2019-07-11 stsp argc -= optind;
8056 818c7501 2019-07-11 stsp argv += optind;
8058 43012d58 2019-07-14 stsp #ifndef PROFILE
8059 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8060 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8061 43012d58 2019-07-14 stsp err(1, "pledge");
8063 e600f124 2021-03-21 stsp if (list_backups) {
8064 e600f124 2021-03-21 stsp if (abort_rebase)
8065 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8066 e600f124 2021-03-21 stsp if (continue_rebase)
8067 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8068 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8069 818c7501 2019-07-11 stsp usage_rebase();
8071 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8072 e600f124 2021-03-21 stsp usage_rebase();
8073 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8074 e600f124 2021-03-21 stsp if (argc != 0)
8075 e600f124 2021-03-21 stsp usage_rebase();
8076 e600f124 2021-03-21 stsp } else if (argc != 1)
8077 e600f124 2021-03-21 stsp usage_rebase();
8080 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8081 818c7501 2019-07-11 stsp if (cwd == NULL) {
8082 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8083 818c7501 2019-07-11 stsp goto done;
8085 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8086 fa51e947 2020-03-27 stsp if (error) {
8087 e600f124 2021-03-21 stsp if (list_backups) {
8088 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8089 e600f124 2021-03-21 stsp goto done;
8091 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8092 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8093 e600f124 2021-03-21 stsp "rebase", cwd);
8094 e600f124 2021-03-21 stsp goto done;
8098 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8099 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8100 818c7501 2019-07-11 stsp if (error != NULL)
8101 818c7501 2019-07-11 stsp goto done;
8103 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8104 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8105 7ef62c4e 2020-02-24 stsp if (error)
8106 7ef62c4e 2020-02-24 stsp goto done;
8108 e600f124 2021-03-21 stsp if (list_backups) {
8109 e600f124 2021-03-21 stsp error = list_backup_refs(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8110 e600f124 2021-03-21 stsp argc == 1 ? argv[0] : NULL, repo);
8111 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8114 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8115 7ef62c4e 2020-02-24 stsp worktree);
8116 818c7501 2019-07-11 stsp if (error)
8117 7ef62c4e 2020-02-24 stsp goto done;
8118 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8119 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8120 818c7501 2019-07-11 stsp goto done;
8123 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8124 818c7501 2019-07-11 stsp if (error)
8125 818c7501 2019-07-11 stsp goto done;
8127 f6794adc 2019-07-23 stsp if (abort_rebase) {
8128 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8129 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8130 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8131 f6794adc 2019-07-23 stsp goto done;
8133 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8134 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8135 3e3a69f1 2019-07-25 stsp worktree, repo);
8136 818c7501 2019-07-11 stsp if (error)
8137 818c7501 2019-07-11 stsp goto done;
8138 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8139 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8140 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8141 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8142 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8143 818c7501 2019-07-11 stsp if (error)
8144 818c7501 2019-07-11 stsp goto done;
8145 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8146 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8147 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8150 818c7501 2019-07-11 stsp if (continue_rebase) {
8151 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8152 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8153 f6794adc 2019-07-23 stsp goto done;
8155 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8156 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8157 3e3a69f1 2019-07-25 stsp worktree, repo);
8158 818c7501 2019-07-11 stsp if (error)
8159 818c7501 2019-07-11 stsp goto done;
8161 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8162 01757395 2019-07-12 stsp resume_commit_id, repo);
8163 818c7501 2019-07-11 stsp if (error)
8164 818c7501 2019-07-11 stsp goto done;
8166 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8167 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8168 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8169 818c7501 2019-07-11 stsp goto done;
8172 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8173 818c7501 2019-07-11 stsp if (error != NULL)
8174 818c7501 2019-07-11 stsp goto done;
8177 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8178 ff0d2220 2019-07-11 stsp if (error)
8179 ff0d2220 2019-07-11 stsp goto done;
8181 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8182 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8184 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8185 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8186 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8187 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8188 818c7501 2019-07-11 stsp if (error)
8189 818c7501 2019-07-11 stsp goto done;
8190 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8191 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8192 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8193 818c7501 2019-07-11 stsp "with work tree's branch");
8194 818c7501 2019-07-11 stsp goto done;
8197 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8198 a51a74b3 2019-07-27 stsp if (error) {
8199 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8200 a51a74b3 2019-07-27 stsp goto done;
8201 a51a74b3 2019-07-27 stsp error = NULL;
8203 df3ed485 2021-01-31 stsp static char msg[128];
8204 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8205 df3ed485 2021-01-31 stsp "%s is already based on %s",
8206 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8207 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8208 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8209 a51a74b3 2019-07-27 stsp goto done;
8211 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8212 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8213 818c7501 2019-07-11 stsp if (error)
8214 818c7501 2019-07-11 stsp goto done;
8217 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8218 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8219 818c7501 2019-07-11 stsp if (error)
8220 818c7501 2019-07-11 stsp goto done;
8222 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8223 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
8224 fc66b545 2019-08-12 stsp if (pid == NULL) {
8225 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8226 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8227 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8228 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8229 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8230 fc66b545 2019-08-12 stsp if (error)
8231 fc66b545 2019-08-12 stsp goto done;
8232 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8233 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8234 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8237 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8238 fc66b545 2019-08-12 stsp goto done;
8240 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8241 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8242 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8243 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8244 818c7501 2019-07-11 stsp commit = NULL;
8245 818c7501 2019-07-11 stsp if (error)
8246 818c7501 2019-07-11 stsp goto done;
8248 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
8249 38b0338b 2019-11-29 stsp if (continue_rebase) {
8250 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8251 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8252 e600f124 2021-03-21 stsp create_backup);
8253 38b0338b 2019-11-29 stsp goto done;
8255 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8256 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8257 38b0338b 2019-11-29 stsp char *id_str;
8258 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8259 38b0338b 2019-11-29 stsp new_base_branch);
8260 38b0338b 2019-11-29 stsp if (error)
8261 38b0338b 2019-11-29 stsp goto done;
8262 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8263 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8264 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8265 38b0338b 2019-11-29 stsp free(id_str);
8266 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8267 38b0338b 2019-11-29 stsp new_head_commit_id);
8268 38b0338b 2019-11-29 stsp if (error)
8269 38b0338b 2019-11-29 stsp goto done;
8270 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8271 e600f124 2021-03-21 stsp create_backup = 0;
8275 818c7501 2019-07-11 stsp pid = NULL;
8276 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
8277 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8279 818c7501 2019-07-11 stsp commit_id = qid->id;
8280 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8281 818c7501 2019-07-11 stsp pid = qid;
8283 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8284 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8285 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8286 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8287 818c7501 2019-07-11 stsp if (error)
8288 818c7501 2019-07-11 stsp goto done;
8290 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8291 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8292 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8294 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8295 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8296 a0ea4fc0 2020-02-28 stsp if (error)
8297 a0ea4fc0 2020-02-28 stsp goto done;
8298 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8302 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8303 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8304 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8305 818c7501 2019-07-11 stsp if (error)
8306 818c7501 2019-07-11 stsp goto done;
8309 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8310 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8311 818c7501 2019-07-11 stsp if (error)
8312 818c7501 2019-07-11 stsp goto done;
8313 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8314 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8316 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8317 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8319 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8320 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8321 818c7501 2019-07-11 stsp free(resume_commit_id);
8322 818c7501 2019-07-11 stsp free(yca_id);
8323 818c7501 2019-07-11 stsp if (commit)
8324 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8325 818c7501 2019-07-11 stsp if (branch)
8326 818c7501 2019-07-11 stsp got_ref_close(branch);
8327 818c7501 2019-07-11 stsp if (new_base_branch)
8328 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8329 818c7501 2019-07-11 stsp if (tmp_branch)
8330 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8331 5ef14e63 2019-06-02 stsp if (worktree)
8332 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8334 5ef14e63 2019-06-02 stsp got_repo_close(repo);
8335 5ef14e63 2019-06-02 stsp return error;
8338 0ebf8283 2019-07-24 stsp __dead static void
8339 0ebf8283 2019-07-24 stsp usage_histedit(void)
8341 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8342 e600f124 2021-03-21 stsp "[-F histedit-script] [-m] [-l] [branch]\n", getprogname());
8346 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
8347 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
8348 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
8349 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
8350 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
8352 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
8353 0ebf8283 2019-07-24 stsp unsigned char code;
8354 0ebf8283 2019-07-24 stsp const char *name;
8355 0ebf8283 2019-07-24 stsp const char *desc;
8356 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
8357 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
8358 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
8359 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
8360 82997472 2020-01-29 stsp "be used" },
8361 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
8362 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
8363 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
8366 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
8367 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
8368 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
8369 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8370 0ebf8283 2019-07-24 stsp char *logmsg;
8372 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
8374 0ebf8283 2019-07-24 stsp static const struct got_error *
8375 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
8376 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8378 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8379 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
8380 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8383 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8385 0ebf8283 2019-07-24 stsp goto done;
8387 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
8389 0ebf8283 2019-07-24 stsp goto done;
8391 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
8393 0ebf8283 2019-07-24 stsp goto done;
8395 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
8396 0ebf8283 2019-07-24 stsp if (n < 0)
8397 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8399 0ebf8283 2019-07-24 stsp if (commit)
8400 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8401 0ebf8283 2019-07-24 stsp free(id_str);
8402 0ebf8283 2019-07-24 stsp free(logmsg);
8403 0ebf8283 2019-07-24 stsp return err;
8406 0ebf8283 2019-07-24 stsp static const struct got_error *
8407 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
8408 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
8410 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8411 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
8412 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
8414 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
8415 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8417 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8418 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
8419 466785b9 2020-12-10 jrick if (fold_only && SIMPLEQ_NEXT(qid, entry) != NULL)
8420 466785b9 2020-12-10 jrick histedit_cmd = "fold";
8421 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
8424 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8425 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
8426 083957f4 2020-02-24 stsp if (n < 0) {
8427 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
8433 0ebf8283 2019-07-24 stsp return err;
8436 0ebf8283 2019-07-24 stsp static const struct got_error *
8437 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
8438 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
8440 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8443 514f2ffe 2020-01-29 stsp char *id_str;
8444 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
8446 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
8447 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
8449 514f2ffe 2020-01-29 stsp return err;
8451 514f2ffe 2020-01-29 stsp n = fprintf(f,
8452 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
8453 514f2ffe 2020-01-29 stsp "# commit %s\n"
8454 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
8455 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
8456 514f2ffe 2020-01-29 stsp if (n < 0) {
8457 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8458 514f2ffe 2020-01-29 stsp goto done;
8461 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
8462 514f2ffe 2020-01-29 stsp if (n < 0) {
8463 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
8464 514f2ffe 2020-01-29 stsp goto done;
8467 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8468 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
8469 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
8470 0ebf8283 2019-07-24 stsp cmd->desc);
8471 0ebf8283 2019-07-24 stsp if (n < 0) {
8472 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8477 514f2ffe 2020-01-29 stsp free(id_str);
8478 0ebf8283 2019-07-24 stsp return err;
8481 0ebf8283 2019-07-24 stsp static const struct got_error *
8482 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
8484 0ebf8283 2019-07-24 stsp static char msg[42];
8487 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
8489 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
8490 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
8492 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
8495 0ebf8283 2019-07-24 stsp static const struct got_error *
8496 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
8497 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
8499 0ebf8283 2019-07-24 stsp const struct got_error *err;
8500 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
8501 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
8503 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8505 0ebf8283 2019-07-24 stsp return err;
8507 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
8509 0ebf8283 2019-07-24 stsp goto done;
8511 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
8513 5943eee2 2019-08-13 stsp goto done;
8514 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
8515 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
8516 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
8517 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8520 0ebf8283 2019-07-24 stsp if (folded_commit)
8521 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
8522 0ebf8283 2019-07-24 stsp free(id_str);
8523 5943eee2 2019-08-13 stsp free(folded_logmsg);
8524 0ebf8283 2019-07-24 stsp return err;
8527 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
8528 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
8530 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
8532 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
8533 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
8534 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
8535 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
8536 3f9de99f 2019-07-24 stsp folded = prev;
8537 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
8540 0ebf8283 2019-07-24 stsp return folded;
8543 0ebf8283 2019-07-24 stsp static const struct got_error *
8544 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
8545 0ebf8283 2019-07-24 stsp struct got_repository *repo)
8547 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
8548 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
8549 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8550 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8551 1601cb9f 2020-09-11 naddy int logmsg_len;
8553 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
8555 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8557 0ebf8283 2019-07-24 stsp return err;
8559 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
8560 0ebf8283 2019-07-24 stsp if (folded) {
8561 0ebf8283 2019-07-24 stsp while (folded != hle) {
8562 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
8563 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8566 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
8567 0ebf8283 2019-07-24 stsp logmsg, repo);
8569 0ebf8283 2019-07-24 stsp goto done;
8570 0ebf8283 2019-07-24 stsp free(logmsg);
8571 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8572 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8576 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8578 0ebf8283 2019-07-24 stsp goto done;
8579 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8581 5943eee2 2019-08-13 stsp goto done;
8582 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8583 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8584 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8585 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8586 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8587 0ebf8283 2019-07-24 stsp goto done;
8589 0ebf8283 2019-07-24 stsp free(logmsg);
8590 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8592 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8594 0ebf8283 2019-07-24 stsp goto done;
8596 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8597 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8599 0ebf8283 2019-07-24 stsp goto done;
8601 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8602 0ebf8283 2019-07-24 stsp close(fd);
8604 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8606 0ebf8283 2019-07-24 stsp goto done;
8608 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8609 0d5bb276 2020-12-15 stsp logmsg_len, 0);
8610 0ebf8283 2019-07-24 stsp if (err) {
8611 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8612 0ebf8283 2019-07-24 stsp goto done;
8613 71392a05 2020-12-13 stsp err = NULL;
8614 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
8615 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
8616 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
8619 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8620 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8621 0ebf8283 2019-07-24 stsp free(logmsg_path);
8622 0ebf8283 2019-07-24 stsp free(logmsg);
8623 5943eee2 2019-08-13 stsp free(orig_logmsg);
8624 0ebf8283 2019-07-24 stsp free(editor);
8625 0ebf8283 2019-07-24 stsp if (commit)
8626 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8627 0ebf8283 2019-07-24 stsp return err;
8630 0ebf8283 2019-07-24 stsp static const struct got_error *
8631 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8632 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8634 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8635 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8636 6059809a 2020-12-17 stsp size_t i, size;
8637 0ebf8283 2019-07-24 stsp ssize_t len;
8638 6059809a 2020-12-17 stsp int lineno = 0;
8639 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8640 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8641 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8643 0ebf8283 2019-07-24 stsp for (;;) {
8644 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8645 0ebf8283 2019-07-24 stsp if (len == -1) {
8646 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8647 0ebf8283 2019-07-24 stsp if (feof(f))
8649 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8650 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8655 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8657 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8658 0ebf8283 2019-07-24 stsp free(line);
8659 0ebf8283 2019-07-24 stsp line = NULL;
8662 0ebf8283 2019-07-24 stsp cmd = NULL;
8663 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8664 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8665 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8666 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8667 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8670 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8675 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8676 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8679 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8681 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8682 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8683 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8686 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8687 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8691 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8692 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8693 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8697 0ebf8283 2019-07-24 stsp free(line);
8698 0ebf8283 2019-07-24 stsp line = NULL;
8702 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8704 0ebf8283 2019-07-24 stsp *end = '\0';
8706 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8707 0ebf8283 2019-07-24 stsp if (err) {
8708 0ebf8283 2019-07-24 stsp /* override error code */
8709 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8713 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8714 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8715 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8718 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8719 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8720 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8721 0ebf8283 2019-07-24 stsp commit_id = NULL;
8722 0ebf8283 2019-07-24 stsp free(line);
8723 0ebf8283 2019-07-24 stsp line = NULL;
8724 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8727 0ebf8283 2019-07-24 stsp free(line);
8728 0ebf8283 2019-07-24 stsp free(commit_id);
8729 0ebf8283 2019-07-24 stsp return err;
8732 0ebf8283 2019-07-24 stsp static const struct got_error *
8733 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8734 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8736 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8737 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8738 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8739 5b87815e 2020-03-05 stsp static char msg[92];
8740 bfce7f83 2019-07-27 stsp char *id_str;
8742 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8743 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8744 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8745 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8746 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8748 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8749 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8750 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8751 5b87815e 2020-03-05 stsp if (hle == hle2)
8753 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8754 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8756 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8758 5b87815e 2020-03-05 stsp return err;
8759 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8760 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8761 5b87815e 2020-03-05 stsp free(id_str);
8762 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8766 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8767 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8768 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8771 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8772 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8774 bfce7f83 2019-07-27 stsp return err;
8775 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8776 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8777 bfce7f83 2019-07-27 stsp free(id_str);
8778 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8782 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8783 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8784 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8785 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8787 bfce7f83 2019-07-27 stsp return NULL;
8790 bfce7f83 2019-07-27 stsp static const struct got_error *
8791 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8792 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8793 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8795 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8796 0ebf8283 2019-07-24 stsp char *editor;
8797 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8799 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8801 0ebf8283 2019-07-24 stsp return err;
8803 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8804 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8805 0ebf8283 2019-07-24 stsp goto done;
8808 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8809 0ebf8283 2019-07-24 stsp if (f == NULL) {
8810 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8811 0ebf8283 2019-07-24 stsp goto done;
8813 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8815 bfce7f83 2019-07-27 stsp goto done;
8817 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8819 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8820 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8821 0ebf8283 2019-07-24 stsp free(editor);
8822 0ebf8283 2019-07-24 stsp return err;
8825 0ebf8283 2019-07-24 stsp static const struct got_error *
8826 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8827 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8828 514f2ffe 2020-01-29 stsp struct got_repository *);
8830 0ebf8283 2019-07-24 stsp static const struct got_error *
8831 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8832 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8833 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
8835 0ebf8283 2019-07-24 stsp const struct got_error *err;
8836 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8837 0ebf8283 2019-07-24 stsp char *path = NULL;
8839 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8841 0ebf8283 2019-07-24 stsp return err;
8843 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8845 0ebf8283 2019-07-24 stsp goto done;
8847 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
8848 466785b9 2020-12-10 jrick fold_only, repo);
8850 0ebf8283 2019-07-24 stsp goto done;
8852 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
8853 083957f4 2020-02-24 stsp rewind(f);
8854 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8856 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
8857 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8858 0ebf8283 2019-07-24 stsp goto done;
8861 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8862 083957f4 2020-02-24 stsp if (err) {
8863 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8864 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8865 083957f4 2020-02-24 stsp goto done;
8866 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8867 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8871 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8872 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8873 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8874 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8875 0ebf8283 2019-07-24 stsp free(path);
8876 0ebf8283 2019-07-24 stsp return err;
8879 0ebf8283 2019-07-24 stsp static const struct got_error *
8880 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8881 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8883 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8884 0ebf8283 2019-07-24 stsp char *path = NULL;
8885 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8886 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8887 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8889 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8891 0ebf8283 2019-07-24 stsp return err;
8893 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8894 0ebf8283 2019-07-24 stsp if (f == NULL) {
8895 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8896 0ebf8283 2019-07-24 stsp goto done;
8898 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8899 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8904 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8905 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8906 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8907 0ebf8283 2019-07-24 stsp if (n < 0) {
8908 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8914 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8915 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8916 0ebf8283 2019-07-24 stsp free(path);
8917 0ebf8283 2019-07-24 stsp if (commit)
8918 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8919 0ebf8283 2019-07-24 stsp return err;
8923 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8925 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8927 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8928 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8929 bfce7f83 2019-07-27 stsp free(hle);
8933 0ebf8283 2019-07-24 stsp static const struct got_error *
8934 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8935 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8937 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8938 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8940 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8941 0ebf8283 2019-07-24 stsp if (f == NULL) {
8942 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8943 0ebf8283 2019-07-24 stsp goto done;
8946 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8948 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8949 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8950 0ebf8283 2019-07-24 stsp return err;
8953 0ebf8283 2019-07-24 stsp static const struct got_error *
8954 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8955 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8956 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8958 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8959 0ebf8283 2019-07-24 stsp int resp = ' ';
8961 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8962 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8963 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8964 0ebf8283 2019-07-24 stsp resp = getchar();
8965 a61a4414 2019-08-07 stsp if (resp == '\n')
8966 a61a4414 2019-08-07 stsp resp = getchar();
8967 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8968 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8969 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8971 426ebf2e 2019-07-25 stsp if (err) {
8972 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8973 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8975 bfce7f83 2019-07-27 stsp prev_err = err;
8976 426ebf2e 2019-07-25 stsp resp = ' ';
8980 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8981 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8982 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8983 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
8984 426ebf2e 2019-07-25 stsp if (err) {
8985 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8986 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8988 bfce7f83 2019-07-27 stsp prev_err = err;
8989 426ebf2e 2019-07-25 stsp resp = ' ';
8993 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8994 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8997 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9000 0ebf8283 2019-07-24 stsp return err;
9003 0ebf8283 2019-07-24 stsp static const struct got_error *
9004 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9005 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9006 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9008 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9009 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9010 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9011 3e3a69f1 2019-07-25 stsp branch, repo);
9014 0ebf8283 2019-07-24 stsp static const struct got_error *
9015 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9016 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9018 0ebf8283 2019-07-24 stsp const struct got_error *err;
9019 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9021 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9023 0ebf8283 2019-07-24 stsp goto done;
9025 0ebf8283 2019-07-24 stsp if (new_id) {
9026 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9028 0ebf8283 2019-07-24 stsp goto done;
9031 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9032 0ebf8283 2019-07-24 stsp if (new_id_str)
9033 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9035 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9036 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9037 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9038 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9039 0ebf8283 2019-07-24 stsp goto done;
9041 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9043 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9045 0ebf8283 2019-07-24 stsp goto done;
9048 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9049 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9050 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9051 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9052 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9054 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9055 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9056 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9063 0ebf8283 2019-07-24 stsp free(old_id_str);
9064 0ebf8283 2019-07-24 stsp free(new_id_str);
9065 0ebf8283 2019-07-24 stsp return err;
9068 0ebf8283 2019-07-24 stsp static const struct got_error *
9069 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9070 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9071 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9072 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9074 0ebf8283 2019-07-24 stsp const struct got_error *err;
9075 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9076 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9078 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9079 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9080 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9082 0ebf8283 2019-07-24 stsp return err;
9085 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9087 0ebf8283 2019-07-24 stsp return err;
9089 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9090 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9091 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9092 0ebf8283 2019-07-24 stsp if (err) {
9093 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9094 0ebf8283 2019-07-24 stsp goto done;
9095 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9097 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9098 0ebf8283 2019-07-24 stsp free(new_commit_id);
9101 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9102 0ebf8283 2019-07-24 stsp return err;
9105 0ebf8283 2019-07-24 stsp static const struct got_error *
9106 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9107 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9109 0ebf8283 2019-07-24 stsp const struct got_error *error;
9110 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9112 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9114 0ebf8283 2019-07-24 stsp if (error)
9115 0ebf8283 2019-07-24 stsp return error;
9117 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9118 0ebf8283 2019-07-24 stsp if (error)
9119 0ebf8283 2019-07-24 stsp return error;
9121 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9122 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9123 0ebf8283 2019-07-24 stsp return error;
9126 ab20a43a 2020-01-29 stsp static const struct got_error *
9127 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9128 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9129 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9130 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9132 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9134 ab20a43a 2020-01-29 stsp switch (status) {
9135 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9136 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9137 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9138 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9139 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9140 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9145 ab20a43a 2020-01-29 stsp switch (staged_status) {
9146 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9147 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9148 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9149 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9150 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9155 ab20a43a 2020-01-29 stsp return NULL;
9158 0ebf8283 2019-07-24 stsp static const struct got_error *
9159 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9161 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9162 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9163 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9164 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9165 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9166 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9167 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9168 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9169 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9170 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9171 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9172 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9173 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9174 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9175 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9176 e600f124 2021-03-21 stsp int list_backups = 0;
9177 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9178 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9179 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9180 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9181 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9182 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9183 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9184 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9186 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
9187 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9188 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9189 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9191 e600f124 2021-03-21 stsp while ((ch = getopt(argc, argv, "acfF:ml")) != -1) {
9192 0ebf8283 2019-07-24 stsp switch (ch) {
9194 0ebf8283 2019-07-24 stsp abort_edit = 1;
9197 0ebf8283 2019-07-24 stsp continue_edit = 1;
9199 466785b9 2020-12-10 jrick case 'f':
9200 466785b9 2020-12-10 jrick fold_only = 1;
9203 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9206 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9209 e600f124 2021-03-21 stsp list_backups = 1;
9212 0ebf8283 2019-07-24 stsp usage_histedit();
9213 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9217 0ebf8283 2019-07-24 stsp argc -= optind;
9218 0ebf8283 2019-07-24 stsp argv += optind;
9220 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9221 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9222 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9223 0ebf8283 2019-07-24 stsp err(1, "pledge");
9225 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9226 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9227 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9228 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9229 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9230 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9231 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9232 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9233 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9234 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9235 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9236 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9237 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9238 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9239 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9240 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9241 e600f124 2021-03-21 stsp if (list_backups) {
9242 e600f124 2021-03-21 stsp if (abort_edit)
9243 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9244 e600f124 2021-03-21 stsp if (continue_edit)
9245 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9246 e600f124 2021-03-21 stsp if (edit_script_path)
9247 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9248 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9249 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9250 e600f124 2021-03-21 stsp if (fold_only)
9251 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9252 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9253 e600f124 2021-03-21 stsp usage_histedit();
9254 e600f124 2021-03-21 stsp } else if (argc != 0)
9255 0ebf8283 2019-07-24 stsp usage_histedit();
9258 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9259 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9260 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9261 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9262 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9263 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9266 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9267 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9268 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9269 0ebf8283 2019-07-24 stsp goto done;
9271 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9272 fa51e947 2020-03-27 stsp if (error) {
9273 e600f124 2021-03-21 stsp if (list_backups) {
9274 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9275 e600f124 2021-03-21 stsp goto done;
9277 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9278 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9279 e600f124 2021-03-21 stsp "histedit", cwd);
9280 e600f124 2021-03-21 stsp goto done;
9284 e600f124 2021-03-21 stsp if (list_backups) {
9285 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9286 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9288 e600f124 2021-03-21 stsp if (error != NULL)
9289 e600f124 2021-03-21 stsp goto done;
9290 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9291 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9292 e600f124 2021-03-21 stsp if (error)
9293 e600f124 2021-03-21 stsp goto done;
9294 e600f124 2021-03-21 stsp error = list_backup_refs(
9295 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9296 e600f124 2021-03-21 stsp argc == 1 ? argv[0] : NULL, repo);
9297 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9300 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9302 0ebf8283 2019-07-24 stsp if (error != NULL)
9303 0ebf8283 2019-07-24 stsp goto done;
9305 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9306 0ebf8283 2019-07-24 stsp if (error)
9307 0ebf8283 2019-07-24 stsp goto done;
9308 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9309 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9310 0ebf8283 2019-07-24 stsp goto done;
9313 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9314 0ebf8283 2019-07-24 stsp if (error)
9315 0ebf8283 2019-07-24 stsp goto done;
9317 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9318 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9319 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9320 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9321 083957f4 2020-02-24 stsp "before the -m option can be used");
9322 083957f4 2020-02-24 stsp goto done;
9324 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
9325 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9326 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
9327 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
9328 466785b9 2020-12-10 jrick "before the -f option can be used");
9329 466785b9 2020-12-10 jrick goto done;
9332 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
9333 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9334 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9335 3e3a69f1 2019-07-25 stsp worktree, repo);
9336 0ebf8283 2019-07-24 stsp if (error)
9337 0ebf8283 2019-07-24 stsp goto done;
9338 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9339 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9340 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
9341 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
9342 0ebf8283 2019-07-24 stsp if (error)
9343 0ebf8283 2019-07-24 stsp goto done;
9344 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
9345 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9346 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9347 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
9348 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
9349 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9350 0ebf8283 2019-07-24 stsp goto done;
9353 0ebf8283 2019-07-24 stsp if (continue_edit) {
9354 0ebf8283 2019-07-24 stsp char *path;
9356 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
9357 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
9358 0ebf8283 2019-07-24 stsp goto done;
9361 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
9362 0ebf8283 2019-07-24 stsp if (error)
9363 0ebf8283 2019-07-24 stsp goto done;
9365 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
9366 0ebf8283 2019-07-24 stsp free(path);
9367 0ebf8283 2019-07-24 stsp if (error)
9368 0ebf8283 2019-07-24 stsp goto done;
9370 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
9371 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
9372 3e3a69f1 2019-07-25 stsp worktree, repo);
9373 0ebf8283 2019-07-24 stsp if (error)
9374 0ebf8283 2019-07-24 stsp goto done;
9376 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9377 0ebf8283 2019-07-24 stsp if (error)
9378 0ebf8283 2019-07-24 stsp goto done;
9380 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9381 0ebf8283 2019-07-24 stsp head_commit_id);
9382 0ebf8283 2019-07-24 stsp if (error)
9383 0ebf8283 2019-07-24 stsp goto done;
9384 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9385 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9386 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9387 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9388 3aac7cf7 2019-07-25 stsp goto done;
9390 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9391 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
9392 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9393 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9394 0ebf8283 2019-07-24 stsp commit = NULL;
9395 0ebf8283 2019-07-24 stsp if (error)
9396 0ebf8283 2019-07-24 stsp goto done;
9398 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
9399 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9400 0ebf8283 2019-07-24 stsp goto done;
9403 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
9404 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
9405 0ebf8283 2019-07-24 stsp if (error != NULL)
9406 0ebf8283 2019-07-24 stsp goto done;
9408 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
9409 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
9410 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
9411 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
9412 c7d20a3f 2019-07-30 stsp goto done;
9415 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
9416 bfce7f83 2019-07-27 stsp got_ref_close(branch);
9417 bfce7f83 2019-07-27 stsp branch = NULL;
9418 0ebf8283 2019-07-24 stsp if (error)
9419 0ebf8283 2019-07-24 stsp goto done;
9421 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9422 0ebf8283 2019-07-24 stsp head_commit_id);
9423 0ebf8283 2019-07-24 stsp if (error)
9424 0ebf8283 2019-07-24 stsp goto done;
9425 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9426 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9427 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
9428 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9429 3aac7cf7 2019-07-25 stsp goto done;
9431 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
9432 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
9433 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
9434 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
9435 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9436 0ebf8283 2019-07-24 stsp commit = NULL;
9437 0ebf8283 2019-07-24 stsp if (error)
9438 0ebf8283 2019-07-24 stsp goto done;
9440 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
9441 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
9442 c5996fff 2020-01-29 stsp goto done;
9445 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
9446 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
9447 bfce7f83 2019-07-27 stsp if (error)
9448 bfce7f83 2019-07-27 stsp goto done;
9450 0ebf8283 2019-07-24 stsp if (edit_script_path) {
9451 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
9452 0ebf8283 2019-07-24 stsp edit_script_path, repo);
9453 6ba2bea2 2019-07-27 stsp if (error) {
9454 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9455 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9456 9627c110 2020-04-18 stsp update_progress, &upa);
9457 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9458 0ebf8283 2019-07-24 stsp goto done;
9461 514f2ffe 2020-01-29 stsp const char *branch_name;
9462 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
9463 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
9464 514f2ffe 2020-01-29 stsp branch_name += 11;
9465 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
9466 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
9467 6ba2bea2 2019-07-27 stsp if (error) {
9468 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9469 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9470 9627c110 2020-04-18 stsp update_progress, &upa);
9471 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9472 0ebf8283 2019-07-24 stsp goto done;
9477 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
9479 6ba2bea2 2019-07-27 stsp if (error) {
9480 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
9481 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
9482 9627c110 2020-04-18 stsp update_progress, &upa);
9483 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9484 0ebf8283 2019-07-24 stsp goto done;
9489 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
9490 4ec14e60 2019-08-28 hiltjo if (error)
9491 0ebf8283 2019-07-24 stsp goto done;
9493 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
9494 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
9495 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
9496 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
9499 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
9500 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
9501 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
9502 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
9504 ab20a43a 2020-01-29 stsp if (error)
9505 ab20a43a 2020-01-29 stsp goto done;
9507 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
9508 ab20a43a 2020-01-29 stsp int have_changes = 0;
9510 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
9511 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
9512 ab20a43a 2020-01-29 stsp if (error)
9513 ab20a43a 2020-01-29 stsp goto done;
9514 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
9515 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
9516 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
9517 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
9518 ab20a43a 2020-01-29 stsp if (error) {
9519 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
9520 ab20a43a 2020-01-29 stsp goto done;
9521 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
9522 ab20a43a 2020-01-29 stsp goto done;
9524 ab20a43a 2020-01-29 stsp if (have_changes) {
9525 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
9526 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
9527 ab20a43a 2020-01-29 stsp if (error)
9528 ab20a43a 2020-01-29 stsp goto done;
9530 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
9531 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
9532 ab20a43a 2020-01-29 stsp if (error)
9533 ab20a43a 2020-01-29 stsp goto done;
9534 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
9535 ab20a43a 2020-01-29 stsp hle, NULL);
9536 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
9537 ab20a43a 2020-01-29 stsp commit = NULL;
9538 ab20a43a 2020-01-29 stsp if (error)
9539 ab20a43a 2020-01-29 stsp goto done;
9545 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
9546 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9547 0ebf8283 2019-07-24 stsp if (error)
9548 0ebf8283 2019-07-24 stsp goto done;
9552 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
9553 0ebf8283 2019-07-24 stsp hle->commit_id);
9554 0ebf8283 2019-07-24 stsp if (error)
9555 0ebf8283 2019-07-24 stsp goto done;
9556 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9557 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
9559 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
9560 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
9561 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9562 0ebf8283 2019-07-24 stsp if (error)
9563 0ebf8283 2019-07-24 stsp goto done;
9564 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9565 0ebf8283 2019-07-24 stsp commit = NULL;
9567 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9568 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
9569 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
9571 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9572 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
9574 a0ea4fc0 2020-02-28 stsp if (error)
9575 a0ea4fc0 2020-02-28 stsp goto done;
9576 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9580 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
9581 0ebf8283 2019-07-24 stsp char *id_str;
9582 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
9583 0ebf8283 2019-07-24 stsp if (error)
9584 0ebf8283 2019-07-24 stsp goto done;
9585 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
9587 0ebf8283 2019-07-24 stsp free(id_str);
9588 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9589 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
9590 3e3a69f1 2019-07-25 stsp fileindex);
9591 0ebf8283 2019-07-24 stsp goto done;
9594 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
9595 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
9596 0ebf8283 2019-07-24 stsp if (error)
9597 0ebf8283 2019-07-24 stsp goto done;
9601 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
9602 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
9603 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9604 0ebf8283 2019-07-24 stsp if (error)
9605 0ebf8283 2019-07-24 stsp goto done;
9608 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
9609 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
9610 0ebf8283 2019-07-24 stsp if (error)
9611 0ebf8283 2019-07-24 stsp goto done;
9612 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9613 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
9615 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
9616 3e3a69f1 2019-07-25 stsp branch, repo);
9618 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
9619 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
9620 0ebf8283 2019-07-24 stsp free(head_commit_id);
9621 0ebf8283 2019-07-24 stsp free(base_commit_id);
9622 0ebf8283 2019-07-24 stsp free(resume_commit_id);
9623 0ebf8283 2019-07-24 stsp if (commit)
9624 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9625 0ebf8283 2019-07-24 stsp if (branch)
9626 0ebf8283 2019-07-24 stsp got_ref_close(branch);
9627 0ebf8283 2019-07-24 stsp if (tmp_branch)
9628 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
9629 0ebf8283 2019-07-24 stsp if (worktree)
9630 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
9632 2822a352 2019-10-15 stsp got_repo_close(repo);
9633 2822a352 2019-10-15 stsp return error;
9636 2822a352 2019-10-15 stsp __dead static void
9637 2822a352 2019-10-15 stsp usage_integrate(void)
9639 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9643 2822a352 2019-10-15 stsp static const struct got_error *
9644 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9646 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9647 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9648 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9649 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9650 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9651 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9652 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9653 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9655 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9657 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9658 2822a352 2019-10-15 stsp switch (ch) {
9660 2822a352 2019-10-15 stsp usage_integrate();
9661 2822a352 2019-10-15 stsp /* NOTREACHED */
9665 2822a352 2019-10-15 stsp argc -= optind;
9666 2822a352 2019-10-15 stsp argv += optind;
9668 2822a352 2019-10-15 stsp if (argc != 1)
9669 2822a352 2019-10-15 stsp usage_integrate();
9670 2822a352 2019-10-15 stsp branch_arg = argv[0];
9671 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9672 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9673 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9674 2822a352 2019-10-15 stsp err(1, "pledge");
9676 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9677 2822a352 2019-10-15 stsp if (cwd == NULL) {
9678 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9679 2822a352 2019-10-15 stsp goto done;
9682 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9683 fa51e947 2020-03-27 stsp if (error) {
9684 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9685 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9687 2822a352 2019-10-15 stsp goto done;
9690 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9691 2822a352 2019-10-15 stsp if (error)
9692 2822a352 2019-10-15 stsp goto done;
9694 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9696 2822a352 2019-10-15 stsp if (error != NULL)
9697 2822a352 2019-10-15 stsp goto done;
9699 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9700 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9701 2822a352 2019-10-15 stsp if (error)
9702 2822a352 2019-10-15 stsp goto done;
9704 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9705 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9706 2822a352 2019-10-15 stsp goto done;
9709 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9710 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9711 2822a352 2019-10-15 stsp if (error)
9712 2822a352 2019-10-15 stsp goto done;
9714 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9715 2822a352 2019-10-15 stsp if (refname == NULL) {
9716 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9717 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9718 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9719 2822a352 2019-10-15 stsp goto done;
9721 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9722 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9723 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9724 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9725 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9726 2822a352 2019-10-15 stsp goto done;
9729 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9730 2822a352 2019-10-15 stsp if (error)
9731 2822a352 2019-10-15 stsp goto done;
9733 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9734 2822a352 2019-10-15 stsp if (error)
9735 2822a352 2019-10-15 stsp goto done;
9737 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9738 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9739 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9740 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9741 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9742 2822a352 2019-10-15 stsp goto done;
9745 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9746 2822a352 2019-10-15 stsp if (error) {
9747 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9748 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9749 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9750 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9751 2822a352 2019-10-15 stsp goto done;
9754 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9755 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9756 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9757 2822a352 2019-10-15 stsp check_cancelled, NULL);
9758 2822a352 2019-10-15 stsp if (error)
9759 2822a352 2019-10-15 stsp goto done;
9761 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9762 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9765 715dc77e 2019-08-03 stsp got_repo_close(repo);
9766 2822a352 2019-10-15 stsp if (worktree)
9767 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9768 2822a352 2019-10-15 stsp free(cwd);
9769 2822a352 2019-10-15 stsp free(base_commit_id);
9770 2822a352 2019-10-15 stsp free(commit_id);
9771 2822a352 2019-10-15 stsp free(refname);
9772 2822a352 2019-10-15 stsp free(base_refname);
9773 715dc77e 2019-08-03 stsp return error;
9776 715dc77e 2019-08-03 stsp __dead static void
9777 715dc77e 2019-08-03 stsp usage_stage(void)
9779 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9780 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9781 715dc77e 2019-08-03 stsp getprogname());
9785 715dc77e 2019-08-03 stsp static const struct got_error *
9786 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9787 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9788 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9789 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9791 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9792 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9794 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9795 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9796 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9797 408b4ebc 2019-08-03 stsp return NULL;
9799 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9800 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9801 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9803 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9805 408b4ebc 2019-08-03 stsp return err;
9807 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9808 408b4ebc 2019-08-03 stsp free(id_str);
9809 dc424a06 2019-08-07 stsp return NULL;
9812 dc424a06 2019-08-07 stsp static const struct got_error *
9813 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9815 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9816 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9817 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9818 715dc77e 2019-08-03 stsp char *cwd = NULL;
9819 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9820 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9821 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9822 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9823 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9824 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9826 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9828 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9829 715dc77e 2019-08-03 stsp switch (ch) {
9831 408b4ebc 2019-08-03 stsp list_stage = 1;
9834 dc424a06 2019-08-07 stsp pflag = 1;
9837 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9840 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9843 715dc77e 2019-08-03 stsp usage_stage();
9844 715dc77e 2019-08-03 stsp /* NOTREACHED */
9848 715dc77e 2019-08-03 stsp argc -= optind;
9849 715dc77e 2019-08-03 stsp argv += optind;
9851 715dc77e 2019-08-03 stsp #ifndef PROFILE
9852 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9853 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9854 715dc77e 2019-08-03 stsp err(1, "pledge");
9856 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9857 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9858 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9859 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9861 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9862 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9863 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9864 715dc77e 2019-08-03 stsp goto done;
9867 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9868 fa51e947 2020-03-27 stsp if (error) {
9869 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9870 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9871 715dc77e 2019-08-03 stsp goto done;
9874 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9876 715dc77e 2019-08-03 stsp if (error != NULL)
9877 715dc77e 2019-08-03 stsp goto done;
9879 dc424a06 2019-08-07 stsp if (patch_script_path) {
9880 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9881 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9882 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9883 dc424a06 2019-08-07 stsp patch_script_path);
9884 dc424a06 2019-08-07 stsp goto done;
9887 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9888 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9889 715dc77e 2019-08-03 stsp if (error)
9890 715dc77e 2019-08-03 stsp goto done;
9892 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9893 6d022e97 2019-08-04 stsp if (error)
9894 6d022e97 2019-08-04 stsp goto done;
9896 6d022e97 2019-08-04 stsp if (list_stage)
9897 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9898 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9900 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9901 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9902 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9903 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9904 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9905 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9908 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9909 2e1f37b0 2019-08-08 stsp error == NULL)
9910 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9912 ad493afc 2019-08-03 stsp got_repo_close(repo);
9913 ad493afc 2019-08-03 stsp if (worktree)
9914 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9915 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9916 ad493afc 2019-08-03 stsp free((char *)pe->path);
9917 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9918 ad493afc 2019-08-03 stsp free(cwd);
9919 ad493afc 2019-08-03 stsp return error;
9922 ad493afc 2019-08-03 stsp __dead static void
9923 ad493afc 2019-08-03 stsp usage_unstage(void)
9925 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9926 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9927 ad493afc 2019-08-03 stsp getprogname());
9932 ad493afc 2019-08-03 stsp static const struct got_error *
9933 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9935 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9936 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9937 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9938 ad493afc 2019-08-03 stsp char *cwd = NULL;
9939 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9940 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9941 9627c110 2020-04-18 stsp int ch, pflag = 0;
9942 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9943 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9944 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9945 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9947 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9949 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9950 ad493afc 2019-08-03 stsp switch (ch) {
9952 2e1f37b0 2019-08-08 stsp pflag = 1;
9955 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9958 ad493afc 2019-08-03 stsp usage_unstage();
9959 ad493afc 2019-08-03 stsp /* NOTREACHED */
9963 ad493afc 2019-08-03 stsp argc -= optind;
9964 ad493afc 2019-08-03 stsp argv += optind;
9966 ad493afc 2019-08-03 stsp #ifndef PROFILE
9967 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9968 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9969 ad493afc 2019-08-03 stsp err(1, "pledge");
9971 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9972 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9974 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9975 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9976 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9977 ad493afc 2019-08-03 stsp goto done;
9980 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9981 fa51e947 2020-03-27 stsp if (error) {
9982 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9983 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9984 ad493afc 2019-08-03 stsp goto done;
9987 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9989 ad493afc 2019-08-03 stsp if (error != NULL)
9990 ad493afc 2019-08-03 stsp goto done;
9992 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9993 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9994 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9995 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9996 2e1f37b0 2019-08-08 stsp patch_script_path);
9997 2e1f37b0 2019-08-08 stsp goto done;
10001 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10002 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10003 ad493afc 2019-08-03 stsp if (error)
10004 ad493afc 2019-08-03 stsp goto done;
10006 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10007 ad493afc 2019-08-03 stsp if (error)
10008 ad493afc 2019-08-03 stsp goto done;
10010 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10011 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10012 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10013 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10014 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10015 9627c110 2020-04-18 stsp if (!error)
10016 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10018 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10019 2e1f37b0 2019-08-08 stsp error == NULL)
10020 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10021 0ebf8283 2019-07-24 stsp if (repo)
10022 0ebf8283 2019-07-24 stsp got_repo_close(repo);
10023 715dc77e 2019-08-03 stsp if (worktree)
10024 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10025 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10026 715dc77e 2019-08-03 stsp free((char *)pe->path);
10027 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10028 715dc77e 2019-08-03 stsp free(cwd);
10029 01073a5d 2019-08-22 stsp return error;
10032 01073a5d 2019-08-22 stsp __dead static void
10033 01073a5d 2019-08-22 stsp usage_cat(void)
10035 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10036 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10040 01073a5d 2019-08-22 stsp static const struct got_error *
10041 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10043 01073a5d 2019-08-22 stsp const struct got_error *err;
10044 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10046 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10048 01073a5d 2019-08-22 stsp return err;
10050 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10051 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10052 01073a5d 2019-08-22 stsp return err;
10055 01073a5d 2019-08-22 stsp static const struct got_error *
10056 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10058 01073a5d 2019-08-22 stsp const struct got_error *err;
10059 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10060 56e0773d 2019-11-28 stsp int nentries, i;
10062 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10064 01073a5d 2019-08-22 stsp return err;
10066 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10067 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10068 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10069 01073a5d 2019-08-22 stsp char *id_str;
10070 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10072 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10073 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10076 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10077 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10078 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10079 01073a5d 2019-08-22 stsp free(id_str);
10082 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10083 01073a5d 2019-08-22 stsp return err;
10086 01073a5d 2019-08-22 stsp static const struct got_error *
10087 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10089 01073a5d 2019-08-22 stsp const struct got_error *err;
10090 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10091 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10092 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10093 01073a5d 2019-08-22 stsp char *id_str = NULL;
10094 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10096 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10098 01073a5d 2019-08-22 stsp return err;
10100 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10102 01073a5d 2019-08-22 stsp goto done;
10104 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10105 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10106 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10107 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10108 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
10109 01073a5d 2019-08-22 stsp char *pid_str;
10110 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10112 01073a5d 2019-08-22 stsp goto done;
10113 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10114 01073a5d 2019-08-22 stsp free(pid_str);
10116 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10117 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10118 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10120 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10121 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10122 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10124 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10125 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10126 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10128 01073a5d 2019-08-22 stsp free(id_str);
10129 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10130 01073a5d 2019-08-22 stsp return err;
10133 01073a5d 2019-08-22 stsp static const struct got_error *
10134 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10136 01073a5d 2019-08-22 stsp const struct got_error *err;
10137 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10138 01073a5d 2019-08-22 stsp char *id_str = NULL;
10139 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10141 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10143 01073a5d 2019-08-22 stsp return err;
10145 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10147 01073a5d 2019-08-22 stsp goto done;
10149 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10151 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10152 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10153 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10154 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10156 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10157 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10158 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10160 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10161 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10162 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10164 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10165 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10166 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10172 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10173 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10175 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10176 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10177 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10179 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10180 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10181 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10183 01073a5d 2019-08-22 stsp free(id_str);
10184 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10185 01073a5d 2019-08-22 stsp return err;
10188 01073a5d 2019-08-22 stsp static const struct got_error *
10189 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10191 01073a5d 2019-08-22 stsp const struct got_error *error;
10192 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10193 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10194 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10195 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10196 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10197 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10198 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10200 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10202 01073a5d 2019-08-22 stsp #ifndef PROFILE
10203 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10204 01073a5d 2019-08-22 stsp NULL) == -1)
10205 01073a5d 2019-08-22 stsp err(1, "pledge");
10208 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10209 01073a5d 2019-08-22 stsp switch (ch) {
10210 896e9b6f 2019-08-26 stsp case 'c':
10211 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10213 01073a5d 2019-08-22 stsp case 'r':
10214 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10215 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10216 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10218 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10220 896e9b6f 2019-08-26 stsp case 'P':
10221 896e9b6f 2019-08-26 stsp force_path = 1;
10224 01073a5d 2019-08-22 stsp usage_cat();
10225 01073a5d 2019-08-22 stsp /* NOTREACHED */
10229 01073a5d 2019-08-22 stsp argc -= optind;
10230 01073a5d 2019-08-22 stsp argv += optind;
10232 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10233 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10234 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10235 01073a5d 2019-08-22 stsp goto done;
10237 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10238 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10239 01073a5d 2019-08-22 stsp goto done;
10240 01073a5d 2019-08-22 stsp if (worktree) {
10241 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10242 01073a5d 2019-08-22 stsp repo_path = strdup(
10243 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10244 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10245 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10246 01073a5d 2019-08-22 stsp goto done;
10251 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10252 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10253 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10254 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10257 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10258 01073a5d 2019-08-22 stsp free(repo_path);
10259 01073a5d 2019-08-22 stsp if (error != NULL)
10260 01073a5d 2019-08-22 stsp goto done;
10262 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10263 896e9b6f 2019-08-26 stsp if (error)
10264 896e9b6f 2019-08-26 stsp goto done;
10266 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10267 84de9106 2020-12-26 stsp if (error)
10268 84de9106 2020-12-26 stsp goto done;
10270 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10271 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10272 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10273 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10274 01073a5d 2019-08-22 stsp if (error)
10275 01073a5d 2019-08-22 stsp goto done;
10277 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10278 896e9b6f 2019-08-26 stsp if (force_path) {
10279 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10280 896e9b6f 2019-08-26 stsp argv[i]);
10281 896e9b6f 2019-08-26 stsp if (error)
10284 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10285 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10287 896e9b6f 2019-08-26 stsp if (error) {
10288 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10289 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10291 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10292 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10293 896e9b6f 2019-08-26 stsp if (error)
10298 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10299 01073a5d 2019-08-22 stsp if (error)
10302 01073a5d 2019-08-22 stsp switch (obj_type) {
10303 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10304 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10306 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10307 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10309 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10310 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
10312 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10313 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
10316 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
10319 01073a5d 2019-08-22 stsp if (error)
10321 01073a5d 2019-08-22 stsp free(label);
10322 01073a5d 2019-08-22 stsp label = NULL;
10323 01073a5d 2019-08-22 stsp free(id);
10324 01073a5d 2019-08-22 stsp id = NULL;
10327 01073a5d 2019-08-22 stsp free(label);
10328 01073a5d 2019-08-22 stsp free(id);
10329 896e9b6f 2019-08-26 stsp free(commit_id);
10330 01073a5d 2019-08-22 stsp if (worktree)
10331 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
10332 01073a5d 2019-08-22 stsp if (repo) {
10333 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
10334 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
10335 01073a5d 2019-08-22 stsp if (error == NULL)
10336 01073a5d 2019-08-22 stsp error = repo_error;
10338 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
10339 b2118c49 2020-07-28 stsp return error;
10342 b2118c49 2020-07-28 stsp __dead static void
10343 b2118c49 2020-07-28 stsp usage_info(void)
10345 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
10346 b2118c49 2020-07-28 stsp getprogname());
10350 b2118c49 2020-07-28 stsp static const struct got_error *
10351 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
10352 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10353 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
10355 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
10356 b2118c49 2020-07-28 stsp char *id_str = NULL;
10357 b2118c49 2020-07-28 stsp char datebuf[128];
10358 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
10359 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
10360 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10363 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
10364 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
10366 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
10367 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
10368 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
10369 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
10370 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
10373 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
10374 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
10375 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
10376 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
10377 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
10378 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
10379 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
10380 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
10382 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
10384 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
10385 b2118c49 2020-07-28 stsp if (tm == NULL)
10386 b2118c49 2020-07-28 stsp return NULL;
10387 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
10388 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
10389 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
10391 b2118c49 2020-07-28 stsp if (blob_id) {
10392 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
10394 b2118c49 2020-07-28 stsp return err;
10395 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
10396 b2118c49 2020-07-28 stsp free(id_str);
10399 b2118c49 2020-07-28 stsp if (staged_blob_id) {
10400 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
10402 b2118c49 2020-07-28 stsp return err;
10403 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
10404 b2118c49 2020-07-28 stsp free(id_str);
10407 b2118c49 2020-07-28 stsp if (commit_id) {
10408 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
10410 b2118c49 2020-07-28 stsp return err;
10411 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
10412 b2118c49 2020-07-28 stsp free(id_str);
10415 b2118c49 2020-07-28 stsp return NULL;
10418 b2118c49 2020-07-28 stsp static const struct got_error *
10419 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
10421 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
10422 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
10423 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
10424 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
10425 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10426 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
10427 b2118c49 2020-07-28 stsp int ch, show_files = 0;
10429 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
10431 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10432 b2118c49 2020-07-28 stsp switch (ch) {
10434 b2118c49 2020-07-28 stsp usage_info();
10435 b2118c49 2020-07-28 stsp /* NOTREACHED */
10439 b2118c49 2020-07-28 stsp argc -= optind;
10440 b2118c49 2020-07-28 stsp argv += optind;
10442 b2118c49 2020-07-28 stsp #ifndef PROFILE
10443 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
10444 b2118c49 2020-07-28 stsp NULL) == -1)
10445 b2118c49 2020-07-28 stsp err(1, "pledge");
10447 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
10448 b2118c49 2020-07-28 stsp if (cwd == NULL) {
10449 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
10450 b2118c49 2020-07-28 stsp goto done;
10453 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
10454 b2118c49 2020-07-28 stsp if (error) {
10455 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10456 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
10457 b2118c49 2020-07-28 stsp goto done;
10460 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
10461 b2118c49 2020-07-28 stsp if (error)
10462 b2118c49 2020-07-28 stsp goto done;
10464 b2118c49 2020-07-28 stsp if (argc >= 1) {
10465 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
10466 b2118c49 2020-07-28 stsp worktree);
10467 b2118c49 2020-07-28 stsp if (error)
10468 b2118c49 2020-07-28 stsp goto done;
10469 b2118c49 2020-07-28 stsp show_files = 1;
10472 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
10473 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
10474 b2118c49 2020-07-28 stsp if (error)
10475 b2118c49 2020-07-28 stsp goto done;
10477 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
10478 b2118c49 2020-07-28 stsp if (error)
10479 b2118c49 2020-07-28 stsp goto done;
10481 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
10482 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
10483 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
10484 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
10485 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
10486 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
10487 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
10488 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
10490 b2118c49 2020-07-28 stsp if (show_files) {
10491 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
10492 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10493 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
10494 b2118c49 2020-07-28 stsp continue;
10496 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
10497 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
10499 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
10500 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
10502 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
10503 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
10504 b2118c49 2020-07-28 stsp if (error)
10505 b2118c49 2020-07-28 stsp goto done;
10506 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
10507 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
10508 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
10514 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
10515 b2118c49 2020-07-28 stsp free((char *)pe->path);
10516 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
10517 b2118c49 2020-07-28 stsp free(cwd);
10518 b2118c49 2020-07-28 stsp free(id_str);
10519 b2118c49 2020-07-28 stsp free(uuidstr);
10520 0ebf8283 2019-07-24 stsp return error;