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 5c860e29 2018-03-12 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 ce5b7c56 2019-07-09 stsp __dead static void usage(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 ce5b7c56 2019-07-09 stsp list_commands(void)
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "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 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
266 ce5b7c56 2019-07-09 stsp list_commands();
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
276 8920fa04 2019-08-18 stsp *abspath = NULL;
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
285 0ee7065d 2019-05-13 stsp return err;
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
294 e2ba3d07 2019-05-13 stsp return NULL;
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
301 163ce85a 2019-05-13 stsp const struct got_error *err;
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
323 0266afb7 2019-01-04 stsp return NULL;
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
400 3ce1b845 2019-07-15 stsp goto doneediting;
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
467 3ce1b845 2019-07-15 stsp fclose(fp);
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
482 3ce1b845 2019-07-15 stsp return err;
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
516 59f86c76 2020-09-11 stsp free(*logmsg_path);
517 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
519 3ce1b845 2019-07-15 stsp return err;
522 3ce1b845 2019-07-15 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
525 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
526 3ce1b845 2019-07-15 stsp return NULL;
529 3ce1b845 2019-07-15 stsp static const struct got_error *
530 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
531 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
533 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
534 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
535 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
537 84792843 2019-08-09 stsp *author = NULL;
539 50b0790e 2020-09-11 stsp if (worktree)
540 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
544 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
545 50b0790e 2020-09-11 stsp * significant to least significant:
546 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
547 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
548 50b0790e 2020-09-11 stsp * 3) repository's git config file
549 50b0790e 2020-09-11 stsp * 4) environment variables
550 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
553 50b0790e 2020-09-11 stsp if (worktree_conf)
554 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 50b0790e 2020-09-11 stsp if (got_author == NULL)
556 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
557 84792843 2019-08-09 stsp if (got_author == NULL) {
558 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
559 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
560 c9956ddf 2019-09-08 stsp if (name && email) {
561 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
563 c9956ddf 2019-09-08 stsp return NULL;
566 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
567 257add31 2020-09-09 stsp if (got_author == NULL) {
568 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
571 257add31 2020-09-09 stsp if (name && email) {
572 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
574 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
575 257add31 2020-09-09 stsp return NULL;
577 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
578 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
582 aba9c984 2019-09-08 stsp *author = strdup(got_author);
583 aba9c984 2019-09-08 stsp if (*author == NULL)
584 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
587 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
588 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
590 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
591 84792843 2019-08-09 stsp got_author++;
592 aba9c984 2019-09-08 stsp if (*got_author != '<') {
593 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
596 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
597 84792843 2019-08-09 stsp got_author++;
598 aba9c984 2019-09-08 stsp if (*got_author != '@') {
599 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
602 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
603 84792843 2019-08-09 stsp got_author++;
604 84792843 2019-08-09 stsp if (*got_author != '>')
605 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
608 aba9c984 2019-09-08 stsp free(*author);
609 aba9c984 2019-09-08 stsp *author = NULL;
611 aba9c984 2019-09-08 stsp return err;
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
617 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
619 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (homedir) {
621 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
625 c9956ddf 2019-09-08 stsp return NULL;
628 84792843 2019-08-09 stsp static const struct got_error *
629 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
631 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
632 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
635 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
637 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
640 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
641 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
642 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
644 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
646 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 3ce1b845 2019-07-15 stsp switch (ch) {
649 3ce1b845 2019-07-15 stsp branch_name = optarg;
652 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
653 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
654 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
659 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
660 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
661 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
667 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
669 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
675 b2b65d18 2019-08-22 stsp usage_import();
676 3ce1b845 2019-07-15 stsp /* NOTREACHED */
680 3ce1b845 2019-07-15 stsp argc -= optind;
681 3ce1b845 2019-07-15 stsp argv += optind;
683 3ce1b845 2019-07-15 stsp #ifndef PROFILE
684 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
686 3ce1b845 2019-07-15 stsp NULL) == -1)
687 3ce1b845 2019-07-15 stsp err(1, "pledge");
689 3ce1b845 2019-07-15 stsp if (argc != 1)
690 3ce1b845 2019-07-15 stsp usage_import();
692 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
693 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
694 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
695 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
697 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
698 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
705 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
707 aba9c984 2019-09-08 stsp return error;
710 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
711 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
712 e560b7e0 2019-11-28 stsp * an unintended typo.
714 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
715 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
717 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
722 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 3ce1b845 2019-07-15 stsp if (error) {
724 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
727 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 3ce1b845 2019-07-15 stsp "import target branch already exists");
732 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
733 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
734 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
737 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
740 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
743 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
747 8e158b01 2019-09-22 stsp free(logmsg);
748 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 ef293bdd 2019-10-21 stsp path_dir, refname);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
758 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
759 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
772 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
774 ef293bdd 2019-10-21 stsp if (error) {
775 ef293bdd 2019-10-21 stsp if (logmsg_path)
776 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
780 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 ef293bdd 2019-10-21 stsp if (error) {
782 ef293bdd 2019-10-21 stsp if (logmsg_path)
783 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
787 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
788 ef293bdd 2019-10-21 stsp if (error) {
789 ef293bdd 2019-10-21 stsp if (logmsg_path)
790 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
794 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
795 ef293bdd 2019-10-21 stsp if (error) {
796 ef293bdd 2019-10-21 stsp if (logmsg_path)
797 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
804 ef293bdd 2019-10-21 stsp if (logmsg_path)
805 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
809 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 3ce1b845 2019-07-15 stsp branch_ref);
811 ef293bdd 2019-10-21 stsp if (error) {
812 ef293bdd 2019-10-21 stsp if (logmsg_path)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
817 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
818 ef293bdd 2019-10-21 stsp if (error) {
819 ef293bdd 2019-10-21 stsp if (logmsg_path)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
825 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
826 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
828 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
829 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
831 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 8e158b01 2019-09-22 stsp free(logmsg);
834 ef293bdd 2019-10-21 stsp free(logmsg_path);
835 2c7829a4 2019-06-17 stsp free(repo_path);
836 3ce1b845 2019-07-15 stsp free(editor);
837 3ce1b845 2019-07-15 stsp free(refname);
838 3ce1b845 2019-07-15 stsp free(new_commit_id);
839 3ce1b845 2019-07-15 stsp free(id_str);
840 aba9c984 2019-09-08 stsp free(author);
841 c9956ddf 2019-09-08 stsp free(gitconfig_path);
842 3ce1b845 2019-07-15 stsp if (branch_ref)
843 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
844 3ce1b845 2019-07-15 stsp if (head_ref)
845 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
846 2c7829a4 2019-06-17 stsp return error;
849 93658fb9 2020-03-18 stsp __dead static void
850 93658fb9 2020-03-18 stsp usage_clone(void)
852 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
857 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
858 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 892ac3b6 2020-03-18 stsp int last_p_indexed;
860 892ac3b6 2020-03-18 stsp int last_p_resolved;
861 68999b92 2020-03-18 stsp int verbosity;
864 93658fb9 2020-03-18 stsp static const struct got_error *
865 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
866 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
868 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
869 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
870 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
871 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
873 68999b92 2020-03-18 stsp if (a->verbosity < 0)
874 68999b92 2020-03-18 stsp return NULL;
876 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
877 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
878 892ac3b6 2020-03-18 stsp fflush(stdout);
879 12d1281e 2020-03-19 stsp return NULL;
882 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
883 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
885 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
886 892ac3b6 2020-03-18 stsp print_size = 1;
887 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
888 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
891 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
892 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
893 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
894 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
895 892ac3b6 2020-03-18 stsp print_indexed = 1;
896 892ac3b6 2020-03-18 stsp print_size = 1;
899 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
900 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
901 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
902 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
903 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
904 892ac3b6 2020-03-18 stsp print_resolved = 1;
905 892ac3b6 2020-03-18 stsp print_indexed = 1;
906 892ac3b6 2020-03-18 stsp print_size = 1;
911 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
912 892ac3b6 2020-03-18 stsp printf("\r");
913 892ac3b6 2020-03-18 stsp if (print_size)
914 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
915 d715f13e 2020-03-19 stsp if (print_indexed)
916 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
917 d715f13e 2020-03-19 stsp if (print_resolved)
918 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
919 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
920 892ac3b6 2020-03-18 stsp fflush(stdout);
922 531c3985 2020-03-18 stsp return NULL;
925 531c3985 2020-03-18 stsp static const struct got_error *
926 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
927 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
929 4ba14133 2020-03-20 stsp const struct got_error *err;
930 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
932 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
934 4ba14133 2020-03-20 stsp return err;
936 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
937 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
938 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
939 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
940 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
942 4ba14133 2020-03-20 stsp return err;
945 4ba14133 2020-03-20 stsp static const struct got_error *
946 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
947 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
949 41b0de12 2020-03-21 stsp const struct got_error *err;
950 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
952 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
953 41b0de12 2020-03-21 stsp const char *refname = pe->path;
954 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
956 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
959 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
960 41b0de12 2020-03-21 stsp const char *refname = pe->path;
961 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
962 41b0de12 2020-03-21 stsp char *id_str;
964 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
966 41b0de12 2020-03-21 stsp return err;
967 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
968 41b0de12 2020-03-21 stsp free(id_str);
971 41b0de12 2020-03-21 stsp return NULL;
974 6338a6a1 2020-03-21 stsp static const struct got_error *
975 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
976 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
978 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
979 6338a6a1 2020-03-21 stsp struct got_reference *ref;
980 6338a6a1 2020-03-21 stsp char *id_str;
982 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
984 6338a6a1 2020-03-21 stsp return err;
986 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
990 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
991 6338a6a1 2020-03-21 stsp got_ref_close(ref);
993 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
994 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
996 6338a6a1 2020-03-21 stsp free(id_str);
997 6338a6a1 2020-03-21 stsp return err;
1000 0e4002ca 2020-03-21 stsp static int
1001 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1003 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1005 0e4002ca 2020-03-21 stsp refname += 5;
1008 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1009 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1011 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1013 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1016 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1017 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1019 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1020 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1023 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1024 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1027 0e4002ca 2020-03-21 stsp static int
1028 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1032 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1033 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1040 41b0de12 2020-03-21 stsp static const struct got_error *
1041 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1042 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1044 0e4002ca 2020-03-21 stsp const struct got_error *err;
1045 0e4002ca 2020-03-21 stsp char *remote_refname;
1047 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1048 0e4002ca 2020-03-21 stsp refname += 5;
1050 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1051 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1052 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1054 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1055 0e4002ca 2020-03-21 stsp free(remote_refname);
1056 0e4002ca 2020-03-21 stsp return err;
1059 0e4002ca 2020-03-21 stsp static const struct got_error *
1060 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1062 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1063 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1064 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1065 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1066 bb64b798 2020-03-18 stsp const char *repo_path;
1067 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1068 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1069 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1070 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1071 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1072 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1073 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1074 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1075 257add31 2020-09-09 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1076 257add31 2020-09-09 stsp char *gitconfig = NULL, *gotconfig = NULL;
1077 257add31 2020-09-09 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1078 b46f3e71 2020-03-18 stsp ssize_t n;
1079 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1080 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1081 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1083 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1084 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1085 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1086 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1088 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1089 93658fb9 2020-03-18 stsp switch (ch) {
1091 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1094 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1095 4ba14133 2020-03-20 stsp optarg, NULL);
1096 4ba14133 2020-03-20 stsp if (error)
1097 4ba14133 2020-03-20 stsp return error;
1100 41b0de12 2020-03-21 stsp list_refs_only = 1;
1103 469dd726 2020-03-20 stsp mirror_references = 1;
1106 68999b92 2020-03-18 stsp if (verbosity < 0)
1107 68999b92 2020-03-18 stsp verbosity = 0;
1108 68999b92 2020-03-18 stsp else if (verbosity < 3)
1109 68999b92 2020-03-18 stsp verbosity++;
1112 68999b92 2020-03-18 stsp verbosity = -1;
1115 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1116 0e4002ca 2020-03-21 stsp optarg, NULL);
1117 0e4002ca 2020-03-21 stsp if (error)
1118 0e4002ca 2020-03-21 stsp return error;
1121 93658fb9 2020-03-18 stsp usage_clone();
1125 93658fb9 2020-03-18 stsp argc -= optind;
1126 93658fb9 2020-03-18 stsp argv += optind;
1128 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1129 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1130 41b0de12 2020-03-21 stsp if (list_refs_only) {
1131 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1132 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1133 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1134 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1135 41b0de12 2020-03-21 stsp if (mirror_references)
1136 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1137 41b0de12 2020-03-21 stsp if (verbosity == -1)
1138 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1139 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1140 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1143 93658fb9 2020-03-18 stsp uri = argv[0];
1145 9df6f38b 2020-03-18 stsp if (argc == 1)
1146 93658fb9 2020-03-18 stsp dirname = NULL;
1147 9df6f38b 2020-03-18 stsp else if (argc == 2)
1148 93658fb9 2020-03-18 stsp dirname = argv[1];
1150 93658fb9 2020-03-18 stsp usage_clone();
1152 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1154 39c64a6a 2020-03-18 stsp if (error)
1155 09f63084 2020-03-20 stsp goto done;
1157 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1158 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1159 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1160 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1161 09838ffc 2020-03-18 stsp goto done;
1164 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1165 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1166 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1167 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1168 39c64a6a 2020-03-18 stsp err(1, "pledge");
1170 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1171 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1172 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1173 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1174 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1175 39c64a6a 2020-03-18 stsp err(1, "pledge");
1177 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1178 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1179 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1180 39c64a6a 2020-03-18 stsp goto done;
1182 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1183 39c64a6a 2020-03-18 stsp goto done;
1185 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1186 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1187 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1188 bb64b798 2020-03-18 stsp goto done;
1190 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1192 bb64b798 2020-03-18 stsp repo_path = dirname;
1194 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1195 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1196 41b0de12 2020-03-21 stsp if (error)
1197 41b0de12 2020-03-21 stsp goto done;
1199 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1200 41b0de12 2020-03-21 stsp if (error)
1201 41b0de12 2020-03-21 stsp goto done;
1202 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1203 41b0de12 2020-03-21 stsp if (error)
1204 41b0de12 2020-03-21 stsp goto done;
1207 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1208 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1209 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1210 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1211 ee448f5f 2020-03-18 stsp goto done;
1214 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 ee448f5f 2020-03-18 stsp if (error)
1216 ee448f5f 2020-03-18 stsp goto done;
1218 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1219 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1220 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1222 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1223 9c52365f 2020-03-21 stsp server_path, verbosity);
1224 39c64a6a 2020-03-18 stsp if (error)
1225 bb64b798 2020-03-18 stsp goto done;
1227 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1228 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1229 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1230 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1231 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1232 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1233 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1234 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1235 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1236 39c64a6a 2020-03-18 stsp if (error)
1237 d9b4d0c0 2020-03-18 stsp goto done;
1239 41b0de12 2020-03-21 stsp if (list_refs_only) {
1240 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1241 41b0de12 2020-03-21 stsp goto done;
1244 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1245 39c64a6a 2020-03-18 stsp if (error)
1246 d9b4d0c0 2020-03-18 stsp goto done;
1247 68999b92 2020-03-18 stsp if (verbosity >= 0)
1248 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1249 d9b4d0c0 2020-03-18 stsp free(id_str);
1251 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1253 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1254 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1255 7ebc0570 2020-03-18 stsp char *remote_refname;
1257 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1258 0e4002ca 2020-03-21 stsp !mirror_references) {
1259 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1260 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1261 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1262 0e4002ca 2020-03-21 stsp if (error)
1263 0e4002ca 2020-03-21 stsp goto done;
1267 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1268 39c64a6a 2020-03-18 stsp if (error)
1269 d9b4d0c0 2020-03-18 stsp goto done;
1271 469dd726 2020-03-20 stsp if (mirror_references)
1274 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1277 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1278 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1279 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1280 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1281 7ebc0570 2020-03-18 stsp goto done;
1283 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1284 f298ae0f 2020-03-25 stsp free(remote_refname);
1285 39c64a6a 2020-03-18 stsp if (error)
1286 d9b4d0c0 2020-03-18 stsp goto done;
1289 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1290 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1291 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1292 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1293 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1294 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1296 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1299 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1300 39c64a6a 2020-03-18 stsp if (error) {
1301 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1302 55330abe 2020-03-20 stsp error = NULL;
1305 d9b4d0c0 2020-03-18 stsp goto done;
1308 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1309 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1310 f298ae0f 2020-03-25 stsp if (error)
1311 f298ae0f 2020-03-25 stsp goto done;
1313 f298ae0f 2020-03-25 stsp if (mirror_references)
1316 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1319 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1320 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1321 f298ae0f 2020-03-25 stsp refname) == -1) {
1322 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1323 f298ae0f 2020-03-25 stsp goto done;
1325 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1326 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1327 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1328 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1329 f298ae0f 2020-03-25 stsp free(remote_refname);
1330 f298ae0f 2020-03-25 stsp goto done;
1332 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1333 f298ae0f 2020-03-25 stsp if (error) {
1334 f298ae0f 2020-03-25 stsp free(remote_refname);
1335 f298ae0f 2020-03-25 stsp free(remote_target);
1336 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1337 f298ae0f 2020-03-25 stsp error = NULL;
1340 f298ae0f 2020-03-25 stsp goto done;
1342 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1343 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1344 f298ae0f 2020-03-25 stsp free(remote_refname);
1345 f298ae0f 2020-03-25 stsp free(remote_target);
1346 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1347 39c64a6a 2020-03-18 stsp if (error)
1348 d9b4d0c0 2020-03-18 stsp goto done;
1350 4ba14133 2020-03-20 stsp if (pe == NULL) {
1352 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1353 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1354 4ba14133 2020-03-20 stsp * which could be fetched instead.
1356 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1357 4ba14133 2020-03-20 stsp const char *target = pe->path;
1358 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1360 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1361 4ba14133 2020-03-20 stsp if (error) {
1362 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1363 4ba14133 2020-03-20 stsp error = NULL;
1366 4ba14133 2020-03-20 stsp goto done;
1369 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1370 f298ae0f 2020-03-25 stsp verbosity, repo);
1371 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1372 4ba14133 2020-03-20 stsp if (error)
1373 4ba14133 2020-03-20 stsp goto done;
1378 257add31 2020-09-09 stsp /* Create got.conf(5). */
1379 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1380 257add31 2020-09-09 stsp if (gotconfig_path == NULL) {
1381 257add31 2020-09-09 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1382 257add31 2020-09-09 stsp goto done;
1384 257add31 2020-09-09 stsp gotconfig_file = fopen(gotconfig_path, "a");
1385 257add31 2020-09-09 stsp if (gotconfig_file == NULL) {
1386 257add31 2020-09-09 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1387 257add31 2020-09-09 stsp goto done;
1389 257add31 2020-09-09 stsp got_path_strip_trailing_slashes(server_path);
1390 257add31 2020-09-09 stsp if (asprintf(&gotconfig,
1391 257add31 2020-09-09 stsp "remote \"%s\" {\n"
1392 257add31 2020-09-09 stsp "\tserver %s\n"
1393 257add31 2020-09-09 stsp "\tprotocol %s\n"
1395 257add31 2020-09-09 stsp "\trepository \"%s\"\n"
1398 257add31 2020-09-09 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1399 257add31 2020-09-09 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1400 257add31 2020-09-09 stsp server_path,
1401 257add31 2020-09-09 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1402 257add31 2020-09-09 stsp error = got_error_from_errno("asprintf");
1403 257add31 2020-09-09 stsp goto done;
1405 257add31 2020-09-09 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1406 257add31 2020-09-09 stsp if (n != strlen(gotconfig)) {
1407 257add31 2020-09-09 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1408 257add31 2020-09-09 stsp goto done;
1411 257add31 2020-09-09 stsp /* Create a config file Git can understand. */
1412 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1413 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1414 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1415 659e7fbd 2020-03-20 stsp goto done;
1417 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1418 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1419 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1420 659e7fbd 2020-03-20 stsp goto done;
1422 659e7fbd 2020-03-20 stsp if (mirror_references) {
1423 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1424 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1425 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1426 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1427 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1428 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1429 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1430 659e7fbd 2020-03-20 stsp goto done;
1432 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1433 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1434 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1435 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1436 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1437 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1438 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1439 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1440 659e7fbd 2020-03-20 stsp goto done;
1443 659e7fbd 2020-03-20 stsp const char *branchname;
1446 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1447 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1449 659e7fbd 2020-03-20 stsp if (head_symref) {
1450 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1451 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1452 659e7fbd 2020-03-20 stsp branchname += 11;
1454 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1455 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1456 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1457 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1458 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1459 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1460 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1461 659e7fbd 2020-03-20 stsp branchname) == -1) {
1462 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1463 659e7fbd 2020-03-20 stsp goto done;
1466 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1467 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1468 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 659e7fbd 2020-03-20 stsp goto done;
1472 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1473 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1474 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1476 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1477 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1478 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1479 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1480 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1482 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1483 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1484 257add31 2020-09-09 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1485 257add31 2020-09-09 stsp error = got_error_from_errno("fclose");
1486 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1487 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1489 bb64b798 2020-03-18 stsp got_repo_close(repo);
1490 659e7fbd 2020-03-20 stsp if (head_symref)
1491 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1492 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1493 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1494 d9b4d0c0 2020-03-18 stsp free(pe->data);
1496 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1497 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1498 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1499 d9b4d0c0 2020-03-18 stsp free(pe->data);
1501 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1502 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1503 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1504 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1505 09838ffc 2020-03-18 stsp free(proto);
1506 09838ffc 2020-03-18 stsp free(host);
1507 09838ffc 2020-03-18 stsp free(port);
1508 09838ffc 2020-03-18 stsp free(server_path);
1509 09838ffc 2020-03-18 stsp free(repo_name);
1510 bb64b798 2020-03-18 stsp free(default_destdir);
1511 257add31 2020-09-09 stsp free(gotconfig);
1512 257add31 2020-09-09 stsp free(gitconfig);
1513 257add31 2020-09-09 stsp free(gotconfig_path);
1514 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1515 b46f3e71 2020-03-18 stsp free(git_url);
1516 39c64a6a 2020-03-18 stsp return error;
1519 7848a0e1 2020-03-19 stsp static const struct got_error *
1520 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1521 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1523 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1524 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1525 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1527 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1529 7848a0e1 2020-03-19 stsp goto done;
1531 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1532 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1533 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1535 88609724 2020-03-21 stsp goto done;
1536 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1537 88609724 2020-03-21 stsp goto done;
1538 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1539 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1540 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1542 db6d8ad8 2020-03-21 stsp goto done;
1545 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1546 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1547 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1548 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1549 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1551 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1553 7848a0e1 2020-03-19 stsp goto done;
1554 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1556 e8a967e0 2020-03-21 stsp goto done;
1558 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1560 7848a0e1 2020-03-19 stsp goto done;
1561 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1562 6338a6a1 2020-03-21 stsp goto done;
1564 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1566 6338a6a1 2020-03-21 stsp goto done;
1567 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1569 6338a6a1 2020-03-21 stsp goto done;
1572 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1573 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1574 6338a6a1 2020-03-21 stsp new_id_str);
1576 7848a0e1 2020-03-19 stsp free(old_id);
1577 7848a0e1 2020-03-19 stsp free(new_id_str);
1578 7848a0e1 2020-03-19 stsp return err;
1581 f1bcca34 2020-03-25 stsp static const struct got_error *
1582 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1583 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1585 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1586 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1587 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1589 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1590 bcf34b0e 2020-03-26 stsp if (err) {
1591 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1592 bcf34b0e 2020-03-26 stsp return err;
1593 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1595 bcf34b0e 2020-03-26 stsp goto done;
1597 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1599 bcf34b0e 2020-03-26 stsp goto done;
1601 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1602 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1603 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1604 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1606 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1608 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1609 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1610 bcf34b0e 2020-03-26 stsp goto done;
1612 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1613 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1615 bcf34b0e 2020-03-26 stsp goto done;
1617 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1619 bcf34b0e 2020-03-26 stsp goto done;
1621 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1622 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1623 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1627 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1628 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1629 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1630 bcf34b0e 2020-03-26 stsp err = unlock_err;
1632 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1633 f1bcca34 2020-03-25 stsp return err;
1636 2ab43947 2020-03-18 stsp __dead static void
1637 7848a0e1 2020-03-19 stsp usage_fetch(void)
1639 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1640 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1641 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1642 13f12b09 2020-03-21 stsp getprogname());
1646 7848a0e1 2020-03-19 stsp static const struct got_error *
1647 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1648 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1650 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1651 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1652 3789fd73 2020-03-26 stsp char *id_str = NULL;
1654 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1655 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1657 3789fd73 2020-03-26 stsp return err;
1658 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1659 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1660 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1661 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1664 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1666 3789fd73 2020-03-26 stsp return err;
1667 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1669 3789fd73 2020-03-26 stsp goto done;
1671 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1673 3789fd73 2020-03-26 stsp goto done;
1674 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1675 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1676 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1681 3789fd73 2020-03-26 stsp free(id_str);
1682 3789fd73 2020-03-26 stsp return NULL;
1685 3789fd73 2020-03-26 stsp static const struct got_error *
1686 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1687 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1688 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1689 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1691 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1692 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1693 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1694 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1695 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1696 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1698 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1700 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1702 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1704 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1706 3789fd73 2020-03-26 stsp goto done;
1708 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1709 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1711 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1712 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1713 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1714 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1715 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1717 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1718 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1719 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1720 3789fd73 2020-03-26 stsp goto done;
1722 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1726 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1727 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1730 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1733 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1734 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1737 3789fd73 2020-03-26 stsp if (pe != NULL)
1740 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1744 3789fd73 2020-03-26 stsp if (local_refname) {
1745 3789fd73 2020-03-26 stsp struct got_reference *ref;
1746 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1747 3789fd73 2020-03-26 stsp if (err) {
1748 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1750 3789fd73 2020-03-26 stsp free(local_refname);
1751 3789fd73 2020-03-26 stsp local_refname = NULL;
1754 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1757 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1758 3789fd73 2020-03-26 stsp got_ref_close(ref);
1759 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1760 3789fd73 2020-03-26 stsp err = unlock_err;
1764 3789fd73 2020-03-26 stsp free(local_refname);
1765 3789fd73 2020-03-26 stsp local_refname = NULL;
1769 3789fd73 2020-03-26 stsp free(remote_namespace);
1770 3789fd73 2020-03-26 stsp free(local_refname);
1771 0e4002ca 2020-03-21 stsp return err;
1774 0e4002ca 2020-03-21 stsp static const struct got_error *
1775 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1776 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1778 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1779 0e4002ca 2020-03-21 stsp char *remote_refname;
1780 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1782 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1783 0e4002ca 2020-03-21 stsp refname += 5;
1785 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1786 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1787 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1789 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1790 0e4002ca 2020-03-21 stsp if (err) {
1791 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1792 0e4002ca 2020-03-21 stsp goto done;
1793 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1795 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1796 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1797 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1798 9f142382 2020-03-21 stsp err = unlock_err;
1799 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1802 0e4002ca 2020-03-21 stsp free(remote_refname);
1803 f21ec2f0 2020-03-21 stsp return err;
1806 f21ec2f0 2020-03-21 stsp static const struct got_error *
1807 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1809 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1810 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1811 7848a0e1 2020-03-19 stsp const char *remote_name;
1812 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1813 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1815 7848a0e1 2020-03-19 stsp int nremotes;
1816 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1817 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1818 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1819 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1820 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1821 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1822 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1823 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1824 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1825 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1826 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1827 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1829 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1830 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1831 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1832 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1834 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1835 7848a0e1 2020-03-19 stsp switch (ch) {
1837 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1840 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1841 4ba14133 2020-03-20 stsp optarg, NULL);
1842 4ba14133 2020-03-20 stsp if (error)
1843 4ba14133 2020-03-20 stsp return error;
1846 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1849 41b0de12 2020-03-21 stsp list_refs_only = 1;
1852 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1853 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1854 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1856 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1859 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1862 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1863 7848a0e1 2020-03-19 stsp verbosity = 0;
1864 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1865 7848a0e1 2020-03-19 stsp verbosity++;
1868 7848a0e1 2020-03-19 stsp verbosity = -1;
1871 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1872 0e4002ca 2020-03-21 stsp optarg, NULL);
1873 0e4002ca 2020-03-21 stsp if (error)
1874 0e4002ca 2020-03-21 stsp return error;
1877 7848a0e1 2020-03-19 stsp usage_fetch();
1881 7848a0e1 2020-03-19 stsp argc -= optind;
1882 7848a0e1 2020-03-19 stsp argv += optind;
1884 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1885 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1886 41b0de12 2020-03-21 stsp if (list_refs_only) {
1887 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1888 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1889 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1890 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1891 f21ec2f0 2020-03-21 stsp if (delete_refs)
1892 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1893 41b0de12 2020-03-21 stsp if (verbosity == -1)
1894 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1897 7848a0e1 2020-03-19 stsp if (argc == 0)
1898 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1899 7848a0e1 2020-03-19 stsp else if (argc == 1)
1900 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1902 7848a0e1 2020-03-19 stsp usage_fetch();
1904 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1905 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1906 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1907 7848a0e1 2020-03-19 stsp goto done;
1910 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1911 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1912 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 7848a0e1 2020-03-19 stsp goto done;
1915 7848a0e1 2020-03-19 stsp error = NULL;
1916 7848a0e1 2020-03-19 stsp if (worktree) {
1917 7848a0e1 2020-03-19 stsp repo_path =
1918 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1919 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1920 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1921 7848a0e1 2020-03-19 stsp if (error)
1922 7848a0e1 2020-03-19 stsp goto done;
1924 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1925 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1926 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1927 7848a0e1 2020-03-19 stsp goto done;
1932 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1933 7848a0e1 2020-03-19 stsp if (error)
1934 7848a0e1 2020-03-19 stsp goto done;
1936 50b0790e 2020-09-11 stsp if (worktree) {
1937 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1938 50b0790e 2020-09-11 stsp if (worktree_conf) {
1939 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1940 50b0790e 2020-09-11 stsp worktree_conf);
1941 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1942 50b0790e 2020-09-11 stsp remote = &remotes[i];
1943 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1948 50b0790e 2020-09-11 stsp if (remote == NULL) {
1949 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
1950 50b0790e 2020-09-11 stsp if (repo_conf) {
1951 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1952 50b0790e 2020-09-11 stsp repo_conf);
1953 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
1954 50b0790e 2020-09-11 stsp remote = &remotes[i];
1955 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
1960 50b0790e 2020-09-11 stsp if (remote == NULL) {
1961 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1962 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
1963 257add31 2020-09-09 stsp remote = &remotes[i];
1964 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
1968 50b0790e 2020-09-11 stsp if (remote == NULL) {
1969 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1970 50b0790e 2020-09-11 stsp goto done;
1973 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1974 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1975 7848a0e1 2020-03-19 stsp if (error)
1976 7848a0e1 2020-03-19 stsp goto done;
1978 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1979 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1980 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1981 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1982 7848a0e1 2020-03-19 stsp err(1, "pledge");
1984 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1985 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1986 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1987 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1988 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1989 7848a0e1 2020-03-19 stsp err(1, "pledge");
1991 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1992 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1993 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1994 7848a0e1 2020-03-19 stsp goto done;
1996 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1997 7848a0e1 2020-03-19 stsp goto done;
2000 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2001 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2002 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2003 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2004 7848a0e1 2020-03-19 stsp goto done;
2007 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 7848a0e1 2020-03-19 stsp if (error)
2009 7848a0e1 2020-03-19 stsp goto done;
2011 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2012 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2013 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2015 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2016 9c52365f 2020-03-21 stsp server_path, verbosity);
2017 7848a0e1 2020-03-19 stsp if (error)
2018 7848a0e1 2020-03-19 stsp goto done;
2020 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2021 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2022 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2023 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2024 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2025 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2026 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2027 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2028 7848a0e1 2020-03-19 stsp if (error)
2029 7848a0e1 2020-03-19 stsp goto done;
2031 41b0de12 2020-03-21 stsp if (list_refs_only) {
2032 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2033 41b0de12 2020-03-21 stsp goto done;
2036 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2037 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2038 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2039 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2040 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2041 984065c8 2020-03-19 stsp if (error)
2042 984065c8 2020-03-19 stsp goto done;
2043 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2044 984065c8 2020-03-19 stsp free(id_str);
2045 984065c8 2020-03-19 stsp id_str = NULL;
2048 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2049 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2050 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2051 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2052 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2053 7848a0e1 2020-03-19 stsp char *remote_refname;
2055 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2056 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2057 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2058 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2059 0e4002ca 2020-03-21 stsp if (error)
2060 0e4002ca 2020-03-21 stsp goto done;
2064 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2065 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2066 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2067 7848a0e1 2020-03-19 stsp if (error) {
2068 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2069 7848a0e1 2020-03-19 stsp goto done;
2070 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2072 7848a0e1 2020-03-19 stsp if (error)
2073 7848a0e1 2020-03-19 stsp goto done;
2075 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2076 db6d8ad8 2020-03-21 stsp verbosity, repo);
2077 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2078 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2079 9f142382 2020-03-21 stsp error = unlock_err;
2080 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2081 7848a0e1 2020-03-19 stsp if (error)
2082 7848a0e1 2020-03-19 stsp goto done;
2084 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2085 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2086 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2087 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2088 7848a0e1 2020-03-19 stsp goto done;
2091 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2092 7848a0e1 2020-03-19 stsp if (error) {
2093 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2094 7848a0e1 2020-03-19 stsp goto done;
2095 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2096 688f11b3 2020-03-21 stsp verbosity, repo);
2097 7848a0e1 2020-03-19 stsp if (error)
2098 7848a0e1 2020-03-19 stsp goto done;
2100 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2101 db6d8ad8 2020-03-21 stsp verbosity, repo);
2102 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2103 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2104 9f142382 2020-03-21 stsp error = unlock_err;
2105 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2106 7848a0e1 2020-03-19 stsp if (error)
2107 7848a0e1 2020-03-19 stsp goto done;
2110 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2111 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2112 2ec30c80 2020-03-20 stsp if (error) {
2113 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2114 2ec30c80 2020-03-20 stsp goto done;
2115 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2117 2ec30c80 2020-03-20 stsp if (error)
2118 2ec30c80 2020-03-20 stsp goto done;
2120 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2121 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2122 9f142382 2020-03-21 stsp error = unlock_err;
2123 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2127 f1bcca34 2020-03-25 stsp if (delete_refs) {
2128 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2129 3789fd73 2020-03-26 stsp verbosity, repo);
2130 f1bcca34 2020-03-25 stsp if (error)
2131 f1bcca34 2020-03-25 stsp goto done;
2134 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2135 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2136 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2137 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2138 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2139 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2140 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2142 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2145 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2148 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2149 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2150 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2151 f1bcca34 2020-03-25 stsp goto done;
2153 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2154 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2155 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2156 f1bcca34 2020-03-25 stsp free(remote_refname);
2157 f1bcca34 2020-03-25 stsp goto done;
2160 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2162 f1bcca34 2020-03-25 stsp if (error) {
2163 f1bcca34 2020-03-25 stsp free(remote_refname);
2164 f1bcca34 2020-03-25 stsp free(remote_target);
2165 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2166 f1bcca34 2020-03-25 stsp error = NULL;
2169 f1bcca34 2020-03-25 stsp goto done;
2171 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2172 f1bcca34 2020-03-25 stsp verbosity, repo);
2173 f1bcca34 2020-03-25 stsp free(remote_refname);
2174 f1bcca34 2020-03-25 stsp free(remote_target);
2175 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2176 f1bcca34 2020-03-25 stsp if (error)
2177 f1bcca34 2020-03-25 stsp goto done;
2181 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2182 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2183 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2184 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2185 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2187 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2188 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2190 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2191 7848a0e1 2020-03-19 stsp if (worktree)
2192 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2193 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2194 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2195 7848a0e1 2020-03-19 stsp free(pe->data);
2197 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2198 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2199 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2200 7848a0e1 2020-03-19 stsp free(pe->data);
2202 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2203 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2204 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2205 7848a0e1 2020-03-19 stsp free(id_str);
2206 7848a0e1 2020-03-19 stsp free(cwd);
2207 7848a0e1 2020-03-19 stsp free(repo_path);
2208 7848a0e1 2020-03-19 stsp free(pack_hash);
2209 7848a0e1 2020-03-19 stsp free(proto);
2210 7848a0e1 2020-03-19 stsp free(host);
2211 7848a0e1 2020-03-19 stsp free(port);
2212 7848a0e1 2020-03-19 stsp free(server_path);
2213 7848a0e1 2020-03-19 stsp free(repo_name);
2214 7848a0e1 2020-03-19 stsp return error;
2218 7848a0e1 2020-03-19 stsp __dead static void
2219 2ab43947 2020-03-18 stsp usage_checkout(void)
2221 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2222 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2226 2ab43947 2020-03-18 stsp static void
2227 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2229 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2230 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2231 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2232 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2233 2ab43947 2020-03-18 stsp getprogname());
2236 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2237 2ab43947 2020-03-18 stsp const char *worktree_path;
2238 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2241 2ab43947 2020-03-18 stsp static const struct got_error *
2242 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2244 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2246 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2247 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2248 2ab43947 2020-03-18 stsp return NULL;
2250 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2251 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2252 2ab43947 2020-03-18 stsp return NULL;
2255 2ab43947 2020-03-18 stsp while (path[0] == '/')
2258 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2259 2ab43947 2020-03-18 stsp return NULL;
2262 2ab43947 2020-03-18 stsp static const struct got_error *
2263 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2265 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2266 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2267 2ab43947 2020-03-18 stsp return NULL;
2270 2ab43947 2020-03-18 stsp static const struct got_error *
2271 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2272 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2273 2ab43947 2020-03-18 stsp struct got_repository *repo)
2275 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2276 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2278 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2279 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2281 2ab43947 2020-03-18 stsp return err;
2283 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2284 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2287 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2288 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2290 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2292 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2300 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2301 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2302 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2304 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2305 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2306 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2307 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2308 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2311 2ab43947 2020-03-18 stsp free(yca_id);
2312 2ab43947 2020-03-18 stsp return NULL;
2315 2ab43947 2020-03-18 stsp static const struct got_error *
2316 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2317 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2318 2ab43947 2020-03-18 stsp struct got_repository *repo)
2320 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2321 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2322 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2323 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2325 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2327 2ab43947 2020-03-18 stsp goto done;
2329 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2330 2ab43947 2020-03-18 stsp is_same_branch = 1;
2331 2ab43947 2020-03-18 stsp goto done;
2333 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 2ab43947 2020-03-18 stsp is_same_branch = 1;
2335 2ab43947 2020-03-18 stsp goto done;
2338 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2340 2ab43947 2020-03-18 stsp goto done;
2342 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2343 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2345 2ab43947 2020-03-18 stsp goto done;
2347 2ab43947 2020-03-18 stsp for (;;) {
2348 2ab43947 2020-03-18 stsp struct got_object_id *id;
2349 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2350 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2351 2ab43947 2020-03-18 stsp if (err) {
2352 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2353 2ab43947 2020-03-18 stsp err = NULL;
2358 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2360 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2361 2ab43947 2020-03-18 stsp is_same_branch = 1;
2367 2ab43947 2020-03-18 stsp if (graph)
2368 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2369 2ab43947 2020-03-18 stsp free(head_commit_id);
2370 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2371 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2372 2ab43947 2020-03-18 stsp return err;
2375 4ed7e80c 2018-05-20 stsp static const struct got_error *
2376 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2378 4b6c9460 2020-03-05 stsp static char msg[512];
2379 4b6c9460 2020-03-05 stsp const char *branch_name;
2381 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2382 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2384 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2386 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2387 4b6c9460 2020-03-05 stsp branch_name += 11;
2389 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2390 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2391 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2392 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2393 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2394 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2396 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2399 4b6c9460 2020-03-05 stsp static const struct got_error *
2400 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2402 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2403 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2404 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2405 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2406 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2407 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2408 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2409 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2410 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2411 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2412 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2413 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2414 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2416 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2418 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2419 0bb8a95e 2018-03-12 stsp switch (ch) {
2421 08573d5b 2019-05-14 stsp branch_name = optarg;
2424 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2425 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2426 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2429 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2432 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2435 2deda0b9 2019-03-07 stsp usage_checkout();
2436 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2440 0bb8a95e 2018-03-12 stsp argc -= optind;
2441 0bb8a95e 2018-03-12 stsp argv += optind;
2443 6715a751 2018-03-16 stsp #ifndef PROFILE
2444 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2445 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2446 c09a553d 2018-03-12 stsp err(1, "pledge");
2448 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2449 c47340da 2020-09-20 stsp char *base, *dotgit;
2450 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2451 76089277 2018-04-01 stsp if (repo_path == NULL)
2452 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2453 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2454 76089277 2018-04-01 stsp if (cwd == NULL) {
2455 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2456 76089277 2018-04-01 stsp goto done;
2458 c47340da 2020-09-20 stsp if (path_prefix[0])
2459 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2461 c47340da 2020-09-20 stsp path = strdup(repo_path);
2462 c47340da 2020-09-20 stsp if (path == NULL) {
2463 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2464 c47340da 2020-09-20 stsp goto done;
2466 c47340da 2020-09-20 stsp base = basename(path);
2467 c47340da 2020-09-20 stsp if (base == NULL) {
2468 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2469 c47340da 2020-09-20 stsp goto done;
2471 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2472 c09a553d 2018-03-12 stsp if (dotgit)
2473 c09a553d 2018-03-12 stsp *dotgit = '\0';
2474 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2475 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2476 76089277 2018-04-01 stsp goto done;
2478 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2479 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2480 76089277 2018-04-01 stsp if (repo_path == NULL) {
2481 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2482 76089277 2018-04-01 stsp goto done;
2484 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2485 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2486 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2487 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2489 b4b3a7dd 2019-07-22 stsp goto done;
2491 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2492 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2493 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2494 b4b3a7dd 2019-07-22 stsp goto done;
2498 c09a553d 2018-03-12 stsp usage_checkout();
2500 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2501 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2503 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2504 c09a553d 2018-03-12 stsp if (error != NULL)
2505 c02c541e 2019-03-29 stsp goto done;
2507 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2508 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2509 c530dc23 2019-07-23 stsp if (error) {
2510 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2511 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2512 c530dc23 2019-07-23 stsp goto done;
2513 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2514 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2515 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2516 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2517 c530dc23 2019-07-23 stsp goto done;
2521 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2522 c02c541e 2019-03-29 stsp if (error)
2523 c09a553d 2018-03-12 stsp goto done;
2525 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2526 c09a553d 2018-03-12 stsp if (error != NULL)
2527 c09a553d 2018-03-12 stsp goto done;
2529 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2530 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2531 c09a553d 2018-03-12 stsp goto done;
2533 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2534 c09a553d 2018-03-12 stsp if (error != NULL)
2535 c09a553d 2018-03-12 stsp goto done;
2537 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2538 e5dc7198 2018-12-29 stsp path_prefix);
2539 e5dc7198 2018-12-29 stsp if (error != NULL)
2540 e5dc7198 2018-12-29 stsp goto done;
2541 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2542 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2543 49520a32 2018-12-29 stsp goto done;
2546 8069f636 2019-01-12 stsp if (commit_id_str) {
2547 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2548 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2549 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2550 30837e32 2019-07-25 stsp if (error)
2551 8069f636 2019-01-12 stsp goto done;
2552 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2553 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2554 8069f636 2019-01-12 stsp if (error != NULL) {
2555 8069f636 2019-01-12 stsp free(commit_id);
2556 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2557 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2558 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2560 8069f636 2019-01-12 stsp goto done;
2562 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2563 4b6c9460 2020-03-05 stsp if (error) {
2564 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2565 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2566 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2568 45d344f6 2019-05-14 stsp goto done;
2570 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2571 8069f636 2019-01-12 stsp commit_id);
2572 8069f636 2019-01-12 stsp free(commit_id);
2573 8069f636 2019-01-12 stsp if (error)
2574 8069f636 2019-01-12 stsp goto done;
2577 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2578 f2ea84fa 2019-07-27 stsp if (error)
2579 f2ea84fa 2019-07-27 stsp goto done;
2580 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2581 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2582 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2583 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2584 c09a553d 2018-03-12 stsp if (error != NULL)
2585 c09a553d 2018-03-12 stsp goto done;
2587 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2588 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2589 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2591 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2592 8069f636 2019-01-12 stsp free(commit_id_str);
2593 76089277 2018-04-01 stsp free(repo_path);
2594 507dc3bb 2018-12-29 stsp free(worktree_path);
2595 c47340da 2020-09-20 stsp free(cwd);
2596 c47340da 2020-09-20 stsp free(path);
2597 507dc3bb 2018-12-29 stsp return error;
2600 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2601 9627c110 2020-04-18 stsp int did_something;
2602 9627c110 2020-04-18 stsp int conflicts;
2603 9627c110 2020-04-18 stsp int obstructed;
2604 9627c110 2020-04-18 stsp int not_updated;
2608 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2610 9627c110 2020-04-18 stsp if (!upa->did_something)
2613 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2614 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2615 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2616 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2617 9627c110 2020-04-18 stsp upa->obstructed);
2618 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2619 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2620 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2623 507dc3bb 2018-12-29 stsp __dead static void
2624 507dc3bb 2018-12-29 stsp usage_update(void)
2626 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2627 507dc3bb 2018-12-29 stsp getprogname());
2631 1ee397ad 2019-07-12 stsp static const struct got_error *
2632 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2634 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2636 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2637 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2638 1ee397ad 2019-07-12 stsp return NULL;
2640 9627c110 2020-04-18 stsp upa->did_something = 1;
2642 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2643 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2644 1ee397ad 2019-07-12 stsp return NULL;
2646 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2647 9627c110 2020-04-18 stsp upa->conflicts++;
2648 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2649 9627c110 2020-04-18 stsp upa->obstructed++;
2650 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2651 9627c110 2020-04-18 stsp upa->not_updated++;
2653 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2655 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2656 1ee397ad 2019-07-12 stsp return NULL;
2659 be7061eb 2018-12-30 stsp static const struct got_error *
2660 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2661 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2662 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2664 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2665 a1fb16d8 2019-05-24 stsp char *base_id_str;
2666 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2668 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2669 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2670 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2671 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2672 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2673 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2674 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2677 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2678 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2679 a1fb16d8 2019-05-24 stsp if (err) {
2680 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2681 a1fb16d8 2019-05-24 stsp return err;
2682 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2684 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2685 a1fb16d8 2019-05-24 stsp return NULL;
2687 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2688 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2689 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2691 a1fb16d8 2019-05-24 stsp return err;
2692 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2693 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2694 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2695 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2696 0ebf8283 2019-07-24 stsp return NULL;
2699 0ebf8283 2019-07-24 stsp static const struct got_error *
2700 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2702 0ebf8283 2019-07-24 stsp const struct got_error *err;
2703 0ebf8283 2019-07-24 stsp int in_progress;
2705 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2707 0ebf8283 2019-07-24 stsp return err;
2708 0ebf8283 2019-07-24 stsp if (in_progress)
2709 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2711 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2713 0ebf8283 2019-07-24 stsp return err;
2714 0ebf8283 2019-07-24 stsp if (in_progress)
2715 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2717 a1fb16d8 2019-05-24 stsp return NULL;
2720 a5edda0a 2019-07-27 stsp static const struct got_error *
2721 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2722 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2724 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2725 a5edda0a 2019-07-27 stsp char *path;
2728 a5edda0a 2019-07-27 stsp if (argc == 0) {
2729 a5edda0a 2019-07-27 stsp path = strdup("");
2730 a5edda0a 2019-07-27 stsp if (path == NULL)
2731 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2732 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2735 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2736 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2739 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2740 a5edda0a 2019-07-27 stsp if (err) {
2741 a5edda0a 2019-07-27 stsp free(path);
2746 a5edda0a 2019-07-27 stsp return err;
2749 a1fb16d8 2019-05-24 stsp static const struct got_error *
2750 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2751 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2753 fa51e947 2020-03-27 stsp const struct got_error *err;
2754 fa51e947 2020-03-27 stsp struct got_repository *repo;
2755 fa51e947 2020-03-27 stsp static char msg[512];
2757 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2759 fa51e947 2020-03-27 stsp return orig_err;
2761 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2762 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2763 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2764 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2765 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2766 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2767 fa51e947 2020-03-27 stsp got_repo_close(repo);
2768 fa51e947 2020-03-27 stsp return err;
2771 fa51e947 2020-03-27 stsp static const struct got_error *
2772 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2774 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2775 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2776 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2777 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2778 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2779 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2780 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2781 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2782 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2783 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2785 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2787 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2789 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2790 507dc3bb 2018-12-29 stsp switch (ch) {
2792 024e9686 2019-05-14 stsp branch_name = optarg;
2795 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2796 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2797 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2800 2deda0b9 2019-03-07 stsp usage_update();
2801 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2805 507dc3bb 2018-12-29 stsp argc -= optind;
2806 507dc3bb 2018-12-29 stsp argv += optind;
2808 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2809 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2810 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2811 507dc3bb 2018-12-29 stsp err(1, "pledge");
2813 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2814 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2815 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2816 c4cdcb68 2019-04-03 stsp goto done;
2818 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2819 fa51e947 2020-03-27 stsp if (error) {
2820 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2821 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2822 fa51e947 2020-03-27 stsp worktree_path);
2823 7d5807f4 2019-07-11 stsp goto done;
2826 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2827 f2ea84fa 2019-07-27 stsp if (error)
2828 f2ea84fa 2019-07-27 stsp goto done;
2830 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2832 507dc3bb 2018-12-29 stsp if (error != NULL)
2833 507dc3bb 2018-12-29 stsp goto done;
2835 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2836 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2837 087fb88c 2019-08-04 stsp if (error)
2838 087fb88c 2019-08-04 stsp goto done;
2840 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2841 0266afb7 2019-01-04 stsp if (error)
2842 0266afb7 2019-01-04 stsp goto done;
2844 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2845 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2846 024e9686 2019-05-14 stsp if (error != NULL)
2847 024e9686 2019-05-14 stsp goto done;
2848 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2849 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2850 9c4b8182 2019-01-02 stsp if (error != NULL)
2851 9c4b8182 2019-01-02 stsp goto done;
2852 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2853 507dc3bb 2018-12-29 stsp if (error != NULL)
2854 507dc3bb 2018-12-29 stsp goto done;
2856 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2857 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2858 04f57cb3 2019-07-25 stsp free(commit_id_str);
2859 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2860 30837e32 2019-07-25 stsp if (error)
2861 a297e751 2019-07-11 stsp goto done;
2862 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2863 a297e751 2019-07-11 stsp if (error)
2864 507dc3bb 2018-12-29 stsp goto done;
2867 a1fb16d8 2019-05-24 stsp if (branch_name) {
2868 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2869 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2870 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2872 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2873 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2874 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2875 024e9686 2019-05-14 stsp goto done;
2877 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2878 024e9686 2019-05-14 stsp if (error)
2879 024e9686 2019-05-14 stsp goto done;
2880 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2882 a367ff0f 2019-05-14 stsp free(head_commit_id);
2883 024e9686 2019-05-14 stsp if (error != NULL)
2884 024e9686 2019-05-14 stsp goto done;
2885 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2886 a367ff0f 2019-05-14 stsp if (error)
2887 a367ff0f 2019-05-14 stsp goto done;
2888 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2889 024e9686 2019-05-14 stsp if (error)
2890 024e9686 2019-05-14 stsp goto done;
2892 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2893 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2894 a367ff0f 2019-05-14 stsp if (error != NULL) {
2895 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2896 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2897 024e9686 2019-05-14 stsp goto done;
2899 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2900 a367ff0f 2019-05-14 stsp if (error)
2901 a367ff0f 2019-05-14 stsp goto done;
2904 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2905 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2906 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2907 507dc3bb 2018-12-29 stsp commit_id);
2908 507dc3bb 2018-12-29 stsp if (error)
2909 507dc3bb 2018-12-29 stsp goto done;
2912 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2913 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2914 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2915 507dc3bb 2018-12-29 stsp if (error != NULL)
2916 507dc3bb 2018-12-29 stsp goto done;
2918 9627c110 2020-04-18 stsp if (upa.did_something)
2919 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2921 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2922 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2924 c09a553d 2018-03-12 stsp free(worktree_path);
2925 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2926 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2927 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2928 507dc3bb 2018-12-29 stsp free(commit_id);
2929 9c4b8182 2019-01-02 stsp free(commit_id_str);
2930 c09a553d 2018-03-12 stsp return error;
2933 f42b1b34 2018-03-12 stsp static const struct got_error *
2934 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2935 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2936 63035f9f 2019-10-06 stsp struct got_repository *repo)
2938 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2939 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2941 44392932 2019-08-25 stsp if (blob_id1) {
2942 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2944 44392932 2019-08-25 stsp goto done;
2947 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2949 44392932 2019-08-25 stsp goto done;
2951 44392932 2019-08-25 stsp while (path[0] == '/')
2953 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2954 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2956 44392932 2019-08-25 stsp if (blob1)
2957 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2958 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2959 44392932 2019-08-25 stsp return err;
2962 44392932 2019-08-25 stsp static const struct got_error *
2963 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2964 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2965 63035f9f 2019-10-06 stsp struct got_repository *repo)
2967 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2968 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2969 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2971 44392932 2019-08-25 stsp if (tree_id1) {
2972 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2974 44392932 2019-08-25 stsp goto done;
2977 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2979 0f2b3dca 2018-12-22 stsp goto done;
2981 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2982 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2983 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2984 44392932 2019-08-25 stsp while (path[0] == '/')
2986 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2987 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2989 79109fed 2018-03-27 stsp if (tree1)
2990 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2991 366e0a5f 2019-10-10 stsp if (tree2)
2992 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2993 44392932 2019-08-25 stsp return err;
2996 44392932 2019-08-25 stsp static const struct got_error *
2997 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2998 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3000 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3001 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3002 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3003 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3005 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3006 0208f208 2020-05-05 stsp if (qid != NULL) {
3007 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3008 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3011 0208f208 2020-05-05 stsp return err;
3013 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3014 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3018 0208f208 2020-05-05 stsp if (tree_id1) {
3019 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3021 0208f208 2020-05-05 stsp goto done;
3024 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3025 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3027 0208f208 2020-05-05 stsp goto done;
3029 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3030 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3032 0208f208 2020-05-05 stsp if (tree1)
3033 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3034 0208f208 2020-05-05 stsp if (tree2)
3035 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3036 0208f208 2020-05-05 stsp return err;
3039 0208f208 2020-05-05 stsp static const struct got_error *
3040 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3041 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3043 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3044 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3045 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3046 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3047 44392932 2019-08-25 stsp struct got_object_qid *qid;
3049 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3050 44392932 2019-08-25 stsp if (qid != NULL) {
3051 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3054 44392932 2019-08-25 stsp return err;
3057 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3058 44392932 2019-08-25 stsp int obj_type;
3059 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3061 44392932 2019-08-25 stsp goto done;
3062 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3063 44392932 2019-08-25 stsp if (err) {
3064 44392932 2019-08-25 stsp free(obj_id2);
3065 44392932 2019-08-25 stsp goto done;
3067 44392932 2019-08-25 stsp if (pcommit) {
3068 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3069 44392932 2019-08-25 stsp qid->id, path);
3070 44392932 2019-08-25 stsp if (err) {
3071 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3072 2e8c69d1 2020-05-04 stsp free(obj_id2);
3073 2e8c69d1 2020-05-04 stsp goto done;
3076 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3077 2e8c69d1 2020-05-04 stsp if (err) {
3078 2e8c69d1 2020-05-04 stsp free(obj_id2);
3079 2e8c69d1 2020-05-04 stsp goto done;
3083 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3084 44392932 2019-08-25 stsp if (err) {
3085 44392932 2019-08-25 stsp free(obj_id2);
3086 44392932 2019-08-25 stsp goto done;
3088 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3089 44392932 2019-08-25 stsp switch (obj_type) {
3090 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3091 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3094 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3095 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3099 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3102 44392932 2019-08-25 stsp free(obj_id1);
3103 44392932 2019-08-25 stsp free(obj_id2);
3105 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3106 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3108 44392932 2019-08-25 stsp goto done;
3109 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3110 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3112 44392932 2019-08-25 stsp goto done;
3113 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3114 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3117 0f2b3dca 2018-12-22 stsp free(id_str1);
3118 0f2b3dca 2018-12-22 stsp free(id_str2);
3119 44392932 2019-08-25 stsp if (pcommit)
3120 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3121 79109fed 2018-03-27 stsp return err;
3124 4bb494d5 2018-06-16 stsp static char *
3125 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3127 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3128 09867e48 2019-08-13 stsp char *p, *s;
3130 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3131 09867e48 2019-08-13 stsp if (tm == NULL)
3132 09867e48 2019-08-13 stsp return NULL;
3133 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3134 09867e48 2019-08-13 stsp if (s == NULL)
3135 09867e48 2019-08-13 stsp return NULL;
3136 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3138 6c281f94 2018-06-11 stsp *p = '\0';
3142 6841bf13 2019-11-29 kn static const struct got_error *
3143 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3144 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3146 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3147 6841bf13 2019-11-29 kn regmatch_t regmatch;
3148 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3150 6841bf13 2019-11-29 kn *have_match = 0;
3152 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3156 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3160 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3161 6841bf13 2019-11-29 kn *have_match = 1;
3163 6841bf13 2019-11-29 kn free(id_str);
3164 6841bf13 2019-11-29 kn free(logmsg);
3168 0208f208 2020-05-05 stsp static void
3169 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3170 0208f208 2020-05-05 stsp regex_t *regex)
3172 0208f208 2020-05-05 stsp regmatch_t regmatch;
3173 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3175 0208f208 2020-05-05 stsp *have_match = 0;
3177 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3178 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3179 0208f208 2020-05-05 stsp *have_match = 1;
3185 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3187 79109fed 2018-03-27 stsp static const struct got_error *
3188 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3189 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3190 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3191 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3193 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3194 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3195 6c281f94 2018-06-11 stsp char datebuf[26];
3196 45d799e2 2018-12-23 stsp time_t committer_time;
3197 45d799e2 2018-12-23 stsp const char *author, *committer;
3198 199a4027 2019-02-02 stsp char *refs_str = NULL;
3199 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3201 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3203 199a4027 2019-02-02 stsp const char *name;
3204 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3207 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3208 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3210 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3211 199a4027 2019-02-02 stsp name += 5;
3212 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3214 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3215 e34f9ed6 2019-02-02 stsp name += 6;
3216 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3217 141c2bff 2019-02-04 stsp name += 8;
3218 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3219 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3222 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3223 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3224 5d844a1e 2019-08-13 stsp if (err) {
3225 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3226 5d844a1e 2019-08-13 stsp return err;
3227 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3228 5d844a1e 2019-08-13 stsp err = NULL;
3229 5d844a1e 2019-08-13 stsp tag = NULL;
3232 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3233 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3235 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3236 a436ad14 2019-08-13 stsp if (cmp != 0)
3238 199a4027 2019-02-02 stsp s = refs_str;
3239 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3240 199a4027 2019-02-02 stsp name) == -1) {
3241 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3243 34ca4898 2019-08-13 stsp return err;
3247 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3249 8bf5b3c9 2018-03-17 stsp return err;
3251 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3252 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3253 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3254 832c249c 2018-06-10 stsp free(id_str);
3255 d3d493d7 2019-02-21 stsp id_str = NULL;
3256 d3d493d7 2019-02-21 stsp free(refs_str);
3257 d3d493d7 2019-02-21 stsp refs_str = NULL;
3258 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3259 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3260 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3261 09867e48 2019-08-13 stsp if (datestr)
3262 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3263 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3264 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3265 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3266 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3267 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3268 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3269 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3270 3fe1abad 2018-06-10 stsp int n = 1;
3271 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3272 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3273 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3275 a0603db2 2018-06-10 stsp return err;
3276 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3277 a0603db2 2018-06-10 stsp free(id_str);
3281 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3283 5943eee2 2019-08-13 stsp return err;
3285 621015ac 2018-07-23 stsp logmsg = logmsg0;
3287 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3289 832c249c 2018-06-10 stsp printf(" %s\n", line);
3290 832c249c 2018-06-10 stsp } while (line);
3291 621015ac 2018-07-23 stsp free(logmsg0);
3293 0208f208 2020-05-05 stsp if (changed_paths) {
3294 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3295 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3296 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3297 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3299 0208f208 2020-05-05 stsp printf("\n");
3301 971751ac 2018-03-27 stsp if (show_patch) {
3302 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3303 971751ac 2018-03-27 stsp if (err == 0)
3304 971751ac 2018-03-27 stsp printf("\n");
3307 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3308 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3309 07862c20 2018-09-15 stsp return err;
3312 f42b1b34 2018-03-12 stsp static const struct got_error *
3313 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3314 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3315 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3316 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3318 f42b1b34 2018-03-12 stsp const struct got_error *err;
3319 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3320 6841bf13 2019-11-29 kn regex_t regex;
3321 6841bf13 2019-11-29 kn int have_match;
3322 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3323 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3324 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3325 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3326 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3328 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3329 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3331 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3332 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3333 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3335 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3337 f42b1b34 2018-03-12 stsp return err;
3338 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3339 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3341 fcc85cad 2018-07-22 stsp goto done;
3343 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3345 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3348 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3349 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3350 372ccdbb 2018-06-10 stsp if (err) {
3351 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3352 9ba79e04 2018-06-11 stsp err = NULL;
3355 b43fbaa0 2018-06-11 stsp if (id == NULL)
3358 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3362 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3363 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3368 6841bf13 2019-11-29 kn if (search_pattern) {
3369 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3371 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3374 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3375 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3376 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3377 6841bf13 2019-11-29 kn if (have_match == 0) {
3378 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3379 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3380 0208f208 2020-05-05 stsp free((char *)pe->path);
3381 0208f208 2020-05-05 stsp free(pe->data);
3383 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3388 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3389 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3392 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3393 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3395 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3396 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3397 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3398 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3402 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3403 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3406 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3407 0208f208 2020-05-05 stsp free((char *)pe->path);
3408 0208f208 2020-05-05 stsp free(pe->data);
3410 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3412 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3413 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3414 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3417 502b9684 2020-07-31 stsp if (show_changed_paths) {
3418 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3419 502b9684 2020-07-31 stsp commit, repo);
3423 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3424 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3425 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3426 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3429 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3430 502b9684 2020-07-31 stsp free((char *)pe->path);
3431 502b9684 2020-07-31 stsp free(pe->data);
3433 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3437 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3438 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3439 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3440 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3442 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3443 0208f208 2020-05-05 stsp free((char *)pe->path);
3444 0208f208 2020-05-05 stsp free(pe->data);
3446 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3447 6841bf13 2019-11-29 kn if (search_pattern)
3448 6841bf13 2019-11-29 kn regfree(®ex);
3449 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3450 f42b1b34 2018-03-12 stsp return err;
3453 4ed7e80c 2018-05-20 stsp __dead static void
3454 6f3d1eb0 2018-03-12 stsp usage_log(void)
3456 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3457 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3458 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3462 b1ebc001 2019-08-13 stsp static int
3463 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3465 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3466 b1ebc001 2019-08-13 stsp long long n;
3467 b1ebc001 2019-08-13 stsp const char *errstr;
3469 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3470 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3472 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3473 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3478 d1fe46f9 2020-04-18 stsp static const struct got_error *
3479 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3480 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3482 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3483 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3485 d1fe46f9 2020-04-18 stsp *id = NULL;
3487 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3488 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3489 d1fe46f9 2020-04-18 stsp int obj_type;
3490 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3491 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3493 d1fe46f9 2020-04-18 stsp return err;
3494 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3496 d1fe46f9 2020-04-18 stsp return err;
3497 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3498 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3499 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3501 d1fe46f9 2020-04-18 stsp return err;
3502 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3503 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3504 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3505 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3507 d1fe46f9 2020-04-18 stsp free(*id);
3508 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3509 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3510 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3511 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3512 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3513 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3515 d1fe46f9 2020-04-18 stsp return err;
3516 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3517 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3519 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3520 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3523 d1fe46f9 2020-04-18 stsp return err;
3526 4ed7e80c 2018-05-20 stsp static const struct got_error *
3527 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3529 f42b1b34 2018-03-12 stsp const struct got_error *error;
3530 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3531 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3532 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3533 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3534 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3535 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3536 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3537 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3538 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3539 64a96a6d 2018-04-01 stsp const char *errstr;
3540 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3542 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3544 6715a751 2018-03-16 stsp #ifndef PROFILE
3545 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3548 f42b1b34 2018-03-12 stsp err(1, "pledge");
3551 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3553 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3554 79109fed 2018-03-27 stsp switch (ch) {
3556 79109fed 2018-03-27 stsp show_patch = 1;
3559 0208f208 2020-05-05 stsp show_changed_paths = 1;
3562 d142fc45 2018-04-01 stsp start_commit = optarg;
3565 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3567 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3568 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3571 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3572 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3573 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3576 48c8c60d 2020-01-27 stsp log_branches = 1;
3579 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3580 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3581 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3583 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3586 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3589 6841bf13 2019-11-29 kn search_pattern = optarg;
3592 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3595 2deda0b9 2019-03-07 stsp usage_log();
3596 79109fed 2018-03-27 stsp /* NOTREACHED */
3600 79109fed 2018-03-27 stsp argc -= optind;
3601 79109fed 2018-03-27 stsp argv += optind;
3603 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3604 dc1edbfa 2019-11-29 kn diff_context = 3;
3605 dc1edbfa 2019-11-29 kn else if (!show_patch)
3606 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3608 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3609 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3611 04ca23f4 2018-07-16 stsp goto done;
3614 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3615 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3616 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3617 50f2fada 2020-04-24 stsp goto done;
3618 50f2fada 2020-04-24 stsp error = NULL;
3621 e7301579 2019-03-18 stsp if (argc == 0) {
3622 cbd1af7a 2019-03-18 stsp path = strdup("");
3623 e7301579 2019-03-18 stsp if (path == NULL) {
3624 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3625 cbd1af7a 2019-03-18 stsp goto done;
3627 e7301579 2019-03-18 stsp } else if (argc == 1) {
3628 e7301579 2019-03-18 stsp if (worktree) {
3629 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3631 e7301579 2019-03-18 stsp if (error)
3632 e7301579 2019-03-18 stsp goto done;
3634 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3635 e7301579 2019-03-18 stsp if (path == NULL) {
3636 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3637 e7301579 2019-03-18 stsp goto done;
3641 cbd1af7a 2019-03-18 stsp usage_log();
3643 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3644 5486daa2 2019-05-11 stsp repo_path = worktree ?
3645 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3647 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3648 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3649 cffc0aa4 2019-02-05 stsp goto done;
3652 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3653 d7d4f210 2018-03-12 stsp if (error != NULL)
3654 04ca23f4 2018-07-16 stsp goto done;
3656 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3657 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3658 c02c541e 2019-03-29 stsp if (error)
3659 c02c541e 2019-03-29 stsp goto done;
3661 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3662 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3663 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3664 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3665 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3666 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3667 3235492e 2018-04-01 stsp if (error != NULL)
3668 d1fe46f9 2020-04-18 stsp goto done;
3669 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3670 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3671 3235492e 2018-04-01 stsp if (error != NULL)
3672 d1fe46f9 2020-04-18 stsp goto done;
3673 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3674 d1fe46f9 2020-04-18 stsp start_id);
3675 d1fe46f9 2020-04-18 stsp if (error != NULL)
3676 d1fe46f9 2020-04-18 stsp goto done;
3677 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3679 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3680 d1fe46f9 2020-04-18 stsp if (error != NULL)
3681 d1fe46f9 2020-04-18 stsp goto done;
3683 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3684 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3685 d1fe46f9 2020-04-18 stsp if (error != NULL)
3686 d1fe46f9 2020-04-18 stsp goto done;
3689 deeabeae 2019-08-27 stsp if (worktree) {
3690 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3692 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3693 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3694 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3695 deeabeae 2019-08-27 stsp goto done;
3697 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3700 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3701 04ca23f4 2018-07-16 stsp if (error != NULL)
3702 04ca23f4 2018-07-16 stsp goto done;
3703 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3704 04ca23f4 2018-07-16 stsp free(path);
3705 04ca23f4 2018-07-16 stsp path = in_repo_path;
3708 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3709 199a4027 2019-02-02 stsp if (error)
3710 199a4027 2019-02-02 stsp goto done;
3712 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3713 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3714 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3716 04ca23f4 2018-07-16 stsp free(path);
3717 04ca23f4 2018-07-16 stsp free(repo_path);
3718 04ca23f4 2018-07-16 stsp free(cwd);
3719 cffc0aa4 2019-02-05 stsp if (worktree)
3720 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3721 ad242220 2018-09-08 stsp if (repo) {
3722 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3723 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3724 ad242220 2018-09-08 stsp if (error == NULL)
3725 ad242220 2018-09-08 stsp error = repo_error;
3727 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3728 8bf5b3c9 2018-03-17 stsp return error;
3731 4ed7e80c 2018-05-20 stsp __dead static void
3732 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3734 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3735 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3739 b72f483a 2019-02-05 stsp struct print_diff_arg {
3740 b72f483a 2019-02-05 stsp struct got_repository *repo;
3741 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3742 b72f483a 2019-02-05 stsp int diff_context;
3743 3fc0c068 2019-02-10 stsp const char *id_str;
3744 3fc0c068 2019-02-10 stsp int header_shown;
3745 98eaaa12 2019-08-03 stsp int diff_staged;
3746 63035f9f 2019-10-06 stsp int ignore_whitespace;
3750 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3751 39449a05 2020-07-23 stsp * it as content to the diff engine.
3753 39449a05 2020-07-23 stsp static const struct got_error *
3754 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3755 39449a05 2020-07-23 stsp const char *abspath)
3757 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3758 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3759 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3763 39449a05 2020-07-23 stsp if (dirfd != -1) {
3764 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3765 39449a05 2020-07-23 stsp if (target_len == -1)
3766 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3768 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3769 39449a05 2020-07-23 stsp if (target_len == -1)
3770 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3773 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3774 39449a05 2020-07-23 stsp if (*fd == -1)
3775 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3777 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3778 39449a05 2020-07-23 stsp if (outlen == -1) {
3779 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3780 39449a05 2020-07-23 stsp goto done;
3783 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3784 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3785 39449a05 2020-07-23 stsp goto done;
3788 39449a05 2020-07-23 stsp if (err) {
3789 39449a05 2020-07-23 stsp close(*fd);
3792 39449a05 2020-07-23 stsp return err;
3795 4ed7e80c 2018-05-20 stsp static const struct got_error *
3796 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3797 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3798 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3799 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3801 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3802 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3803 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3804 12463d8b 2019-12-13 stsp int fd = -1;
3805 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3806 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3807 b72f483a 2019-02-05 stsp struct stat sb;
3809 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3810 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3811 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3812 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3813 98eaaa12 2019-08-03 stsp return NULL;
3815 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3816 98eaaa12 2019-08-03 stsp return NULL;
3817 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3818 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3819 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3820 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3821 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3822 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3823 98eaaa12 2019-08-03 stsp return NULL;
3826 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3827 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3828 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3829 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3830 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3833 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3834 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3835 98eaaa12 2019-08-03 stsp switch (staged_status) {
3836 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3837 98eaaa12 2019-08-03 stsp label1 = path;
3838 98eaaa12 2019-08-03 stsp label2 = path;
3840 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3841 98eaaa12 2019-08-03 stsp label2 = path;
3843 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3844 98eaaa12 2019-08-03 stsp label1 = path;
3847 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3849 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3850 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3851 63035f9f 2019-10-06 stsp a->repo, stdout);
3854 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3855 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3856 4ce46740 2019-08-08 stsp char *id_str;
3857 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3860 4ce46740 2019-08-08 stsp goto done;
3861 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3863 4ce46740 2019-08-08 stsp goto done;
3864 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3865 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3866 4ce46740 2019-08-08 stsp free(id_str);
3867 4ce46740 2019-08-08 stsp goto done;
3869 4ce46740 2019-08-08 stsp free(id_str);
3870 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3871 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3873 4ce46740 2019-08-08 stsp goto done;
3876 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3877 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3878 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3879 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3880 2ec1f75b 2019-03-26 stsp goto done;
3883 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3884 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3885 12463d8b 2019-12-13 stsp if (fd == -1) {
3886 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3887 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3889 39449a05 2020-07-23 stsp goto done;
3891 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3892 39449a05 2020-07-23 stsp de_name, abspath);
3894 39449a05 2020-07-23 stsp goto done;
3897 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3898 12463d8b 2019-12-13 stsp if (fd == -1) {
3899 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3900 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3902 39449a05 2020-07-23 stsp goto done;
3904 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3905 39449a05 2020-07-23 stsp de_name, abspath);
3907 39449a05 2020-07-23 stsp goto done;
3910 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3911 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3912 2ec1f75b 2019-03-26 stsp goto done;
3914 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3915 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3916 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3917 2ec1f75b 2019-03-26 stsp goto done;
3921 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3923 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3924 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3926 b72f483a 2019-02-05 stsp if (blob1)
3927 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3928 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3929 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3930 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3931 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3932 b72f483a 2019-02-05 stsp free(abspath);
3933 927df6b7 2019-02-10 stsp return err;
3936 927df6b7 2019-02-10 stsp static const struct got_error *
3937 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3939 b00d56cd 2018-04-01 stsp const struct got_error *error;
3940 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3941 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3942 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3943 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3944 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3945 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3946 15a94983 2018-12-23 stsp int type1, type2;
3947 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3948 c0cc5c62 2018-10-18 stsp const char *errstr;
3949 927df6b7 2019-02-10 stsp char *path = NULL;
3951 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3952 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3953 25eccc22 2019-01-04 stsp NULL) == -1)
3954 b00d56cd 2018-04-01 stsp err(1, "pledge");
3957 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3958 b00d56cd 2018-04-01 stsp switch (ch) {
3960 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3962 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3963 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3966 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3967 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3968 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3970 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3973 98eaaa12 2019-08-03 stsp diff_staged = 1;
3976 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3979 2deda0b9 2019-03-07 stsp usage_diff();
3980 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3984 b00d56cd 2018-04-01 stsp argc -= optind;
3985 b00d56cd 2018-04-01 stsp argv += optind;
3987 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3988 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3989 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3990 b72f483a 2019-02-05 stsp goto done;
3992 927df6b7 2019-02-10 stsp if (argc <= 1) {
3993 b72f483a 2019-02-05 stsp if (repo_path)
3995 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3996 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3997 fa51e947 2020-03-27 stsp if (error) {
3998 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3999 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4001 1f03b8da 2020-03-20 stsp goto done;
4003 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4004 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4005 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4006 927df6b7 2019-02-10 stsp goto done;
4008 927df6b7 2019-02-10 stsp if (argc == 1) {
4009 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4011 927df6b7 2019-02-10 stsp if (error)
4012 927df6b7 2019-02-10 stsp goto done;
4014 927df6b7 2019-02-10 stsp path = strdup("");
4015 927df6b7 2019-02-10 stsp if (path == NULL) {
4016 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4017 927df6b7 2019-02-10 stsp goto done;
4020 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4021 98eaaa12 2019-08-03 stsp if (diff_staged)
4022 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4023 98eaaa12 2019-08-03 stsp "objects in repository");
4024 15a94983 2018-12-23 stsp id_str1 = argv[0];
4025 15a94983 2018-12-23 stsp id_str2 = argv[1];
4026 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4027 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4028 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4029 30db809c 2019-06-05 stsp goto done;
4030 1f03b8da 2020-03-20 stsp if (worktree) {
4031 1f03b8da 2020-03-20 stsp repo_path = strdup(
4032 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4033 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4034 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4035 1f03b8da 2020-03-20 stsp goto done;
4038 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4039 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4040 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4041 1f03b8da 2020-03-20 stsp goto done;
4046 b00d56cd 2018-04-01 stsp usage_diff();
4048 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4049 76089277 2018-04-01 stsp free(repo_path);
4050 b00d56cd 2018-04-01 stsp if (error != NULL)
4051 b00d56cd 2018-04-01 stsp goto done;
4053 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4054 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4055 c02c541e 2019-03-29 stsp if (error)
4056 c02c541e 2019-03-29 stsp goto done;
4058 30db809c 2019-06-05 stsp if (argc <= 1) {
4059 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4060 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4061 b72f483a 2019-02-05 stsp char *id_str;
4063 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4065 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4066 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4067 b72f483a 2019-02-05 stsp if (error)
4068 b72f483a 2019-02-05 stsp goto done;
4069 b72f483a 2019-02-05 stsp arg.repo = repo;
4070 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4071 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4072 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4073 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4074 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4075 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4077 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4078 72ea6654 2019-07-27 stsp if (error)
4079 72ea6654 2019-07-27 stsp goto done;
4081 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4082 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4083 b72f483a 2019-02-05 stsp free(id_str);
4084 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4085 b72f483a 2019-02-05 stsp goto done;
4088 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4089 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4090 0adb7196 2019-08-11 stsp if (error)
4091 0adb7196 2019-08-11 stsp goto done;
4093 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4094 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4095 0adb7196 2019-08-11 stsp if (error)
4096 0adb7196 2019-08-11 stsp goto done;
4098 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4099 15a94983 2018-12-23 stsp if (error)
4100 15a94983 2018-12-23 stsp goto done;
4102 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4103 15a94983 2018-12-23 stsp if (error)
4104 15a94983 2018-12-23 stsp goto done;
4106 15a94983 2018-12-23 stsp if (type1 != type2) {
4107 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4108 b00d56cd 2018-04-01 stsp goto done;
4111 15a94983 2018-12-23 stsp switch (type1) {
4112 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4113 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4114 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4116 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4117 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4118 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4120 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4121 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4122 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4123 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4126 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4129 5e70831e 2019-05-28 stsp free(label1);
4130 5e70831e 2019-05-28 stsp free(label2);
4131 15a94983 2018-12-23 stsp free(id1);
4132 15a94983 2018-12-23 stsp free(id2);
4133 927df6b7 2019-02-10 stsp free(path);
4134 b72f483a 2019-02-05 stsp if (worktree)
4135 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4136 ad242220 2018-09-08 stsp if (repo) {
4137 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4138 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4139 ad242220 2018-09-08 stsp if (error == NULL)
4140 ad242220 2018-09-08 stsp error = repo_error;
4142 b00d56cd 2018-04-01 stsp return error;
4145 404c43c4 2018-06-21 stsp __dead static void
4146 404c43c4 2018-06-21 stsp usage_blame(void)
4148 9270e621 2019-02-05 stsp fprintf(stderr,
4149 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4150 404c43c4 2018-06-21 stsp getprogname());
4154 28315671 2019-08-14 stsp struct blame_line {
4155 28315671 2019-08-14 stsp int annotated;
4156 28315671 2019-08-14 stsp char *id_str;
4157 82f6abb8 2019-08-14 stsp char *committer;
4158 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4161 28315671 2019-08-14 stsp struct blame_cb_args {
4162 28315671 2019-08-14 stsp struct blame_line *lines;
4163 28315671 2019-08-14 stsp int nlines;
4164 7ef28ff8 2019-08-14 stsp int nlines_prec;
4165 28315671 2019-08-14 stsp int lineno_cur;
4166 28315671 2019-08-14 stsp off_t *line_offsets;
4168 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4171 404c43c4 2018-06-21 stsp static const struct got_error *
4172 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4174 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4175 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4176 28315671 2019-08-14 stsp struct blame_line *bline;
4177 82f6abb8 2019-08-14 stsp char *line = NULL;
4178 28315671 2019-08-14 stsp size_t linesize = 0;
4179 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4180 28315671 2019-08-14 stsp off_t offset;
4181 bcb49d15 2019-08-14 stsp struct tm tm;
4182 bcb49d15 2019-08-14 stsp time_t committer_time;
4184 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4185 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4186 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4188 28315671 2019-08-14 stsp if (sigint_received)
4189 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4191 2839f8b9 2019-08-18 stsp if (lineno == -1)
4192 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4194 28315671 2019-08-14 stsp /* Annotate this line. */
4195 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4196 28315671 2019-08-14 stsp if (bline->annotated)
4197 28315671 2019-08-14 stsp return NULL;
4198 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4200 28315671 2019-08-14 stsp return err;
4202 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4204 82f6abb8 2019-08-14 stsp goto done;
4206 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4207 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4208 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4209 bcb49d15 2019-08-14 stsp goto done;
4212 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4213 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4214 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4215 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4216 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4217 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4218 82f6abb8 2019-08-14 stsp goto done;
4220 28315671 2019-08-14 stsp bline->annotated = 1;
4222 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4223 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4224 28315671 2019-08-14 stsp if (!bline->annotated)
4225 82f6abb8 2019-08-14 stsp goto done;
4227 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4228 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4229 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4230 82f6abb8 2019-08-14 stsp goto done;
4233 28315671 2019-08-14 stsp while (bline->annotated) {
4234 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4235 82f6abb8 2019-08-14 stsp size_t len;
4237 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4238 28315671 2019-08-14 stsp if (ferror(a->f))
4239 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4243 82f6abb8 2019-08-14 stsp committer = bline->committer;
4244 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4245 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4246 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4247 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4249 82f6abb8 2019-08-14 stsp *at = '\0';
4250 82f6abb8 2019-08-14 stsp len = strlen(committer);
4251 82f6abb8 2019-08-14 stsp if (len >= 9)
4252 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4254 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4256 28315671 2019-08-14 stsp *nl = '\0';
4257 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4258 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4260 28315671 2019-08-14 stsp a->lineno_cur++;
4261 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4264 82f6abb8 2019-08-14 stsp if (commit)
4265 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4266 28315671 2019-08-14 stsp free(line);
4267 28315671 2019-08-14 stsp return err;
4270 28315671 2019-08-14 stsp static const struct got_error *
4271 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4273 404c43c4 2018-06-21 stsp const struct got_error *error;
4274 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4275 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4276 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4277 0587e10c 2020-07-23 stsp char *link_target = NULL;
4278 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4279 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4280 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4281 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4282 28315671 2019-08-14 stsp struct blame_cb_args bca;
4283 28315671 2019-08-14 stsp int ch, obj_type, i;
4284 b02560ec 2019-08-19 stsp size_t filesize;
4286 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4288 404c43c4 2018-06-21 stsp #ifndef PROFILE
4289 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4290 36e2fb66 2019-01-04 stsp NULL) == -1)
4291 404c43c4 2018-06-21 stsp err(1, "pledge");
4294 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4295 404c43c4 2018-06-21 stsp switch (ch) {
4297 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4300 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4301 66bea077 2018-08-02 stsp if (repo_path == NULL)
4302 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4304 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4307 2deda0b9 2019-03-07 stsp usage_blame();
4308 404c43c4 2018-06-21 stsp /* NOTREACHED */
4312 404c43c4 2018-06-21 stsp argc -= optind;
4313 404c43c4 2018-06-21 stsp argv += optind;
4315 a39318fd 2018-08-02 stsp if (argc == 1)
4316 404c43c4 2018-06-21 stsp path = argv[0];
4318 404c43c4 2018-06-21 stsp usage_blame();
4320 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4321 66bea077 2018-08-02 stsp if (cwd == NULL) {
4322 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4323 66bea077 2018-08-02 stsp goto done;
4325 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4326 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4327 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4328 66bea077 2018-08-02 stsp goto done;
4330 0c06baac 2019-02-05 stsp error = NULL;
4331 0c06baac 2019-02-05 stsp if (worktree) {
4332 0c06baac 2019-02-05 stsp repo_path =
4333 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4334 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4335 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4336 1f03b8da 2020-03-20 stsp if (error)
4337 1f03b8da 2020-03-20 stsp goto done;
4340 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4341 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4342 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4343 0c06baac 2019-02-05 stsp goto done;
4348 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4349 c02c541e 2019-03-29 stsp if (error != NULL)
4350 36e2fb66 2019-01-04 stsp goto done;
4352 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4353 c02c541e 2019-03-29 stsp if (error)
4354 404c43c4 2018-06-21 stsp goto done;
4356 0c06baac 2019-02-05 stsp if (worktree) {
4357 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4358 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4359 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4360 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4361 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4362 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4363 6efaaa2d 2019-02-05 stsp path) == -1) {
4364 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4365 0c06baac 2019-02-05 stsp goto done;
4367 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4370 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4372 0c06baac 2019-02-05 stsp if (error)
4373 66bea077 2018-08-02 stsp goto done;
4375 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4376 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4377 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4378 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4379 404c43c4 2018-06-21 stsp if (error != NULL)
4380 66bea077 2018-08-02 stsp goto done;
4381 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4382 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4383 404c43c4 2018-06-21 stsp if (error != NULL)
4384 66bea077 2018-08-02 stsp goto done;
4386 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4387 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4388 30837e32 2019-07-25 stsp if (error)
4389 66bea077 2018-08-02 stsp goto done;
4392 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4393 0587e10c 2020-07-23 stsp commit_id, repo);
4394 0587e10c 2020-07-23 stsp if (error)
4395 0587e10c 2020-07-23 stsp goto done;
4397 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4398 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4399 28315671 2019-08-14 stsp if (error)
4400 28315671 2019-08-14 stsp goto done;
4402 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4403 28315671 2019-08-14 stsp if (error)
4404 28315671 2019-08-14 stsp goto done;
4406 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4407 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4408 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4409 28315671 2019-08-14 stsp goto done;
4412 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4413 28315671 2019-08-14 stsp if (error)
4414 28315671 2019-08-14 stsp goto done;
4415 28315671 2019-08-14 stsp bca.f = got_opentemp();
4416 28315671 2019-08-14 stsp if (bca.f == NULL) {
4417 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4418 28315671 2019-08-14 stsp goto done;
4420 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4421 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4422 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4423 28315671 2019-08-14 stsp goto done;
4425 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4426 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4427 b02560ec 2019-08-19 stsp bca.nlines--;
4429 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4430 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4431 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4432 28315671 2019-08-14 stsp goto done;
4434 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4435 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4436 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4437 7ef28ff8 2019-08-14 stsp while (i > 0) {
4439 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4441 82f6abb8 2019-08-14 stsp bca.repo = repo;
4443 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4444 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4446 66bea077 2018-08-02 stsp free(in_repo_path);
4447 0587e10c 2020-07-23 stsp free(link_target);
4448 66bea077 2018-08-02 stsp free(repo_path);
4449 66bea077 2018-08-02 stsp free(cwd);
4450 404c43c4 2018-06-21 stsp free(commit_id);
4451 28315671 2019-08-14 stsp free(obj_id);
4453 28315671 2019-08-14 stsp got_object_blob_close(blob);
4454 0c06baac 2019-02-05 stsp if (worktree)
4455 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4456 ad242220 2018-09-08 stsp if (repo) {
4457 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4458 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4459 ad242220 2018-09-08 stsp if (error == NULL)
4460 ad242220 2018-09-08 stsp error = repo_error;
4462 3affba96 2019-09-22 stsp if (bca.lines) {
4463 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4464 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4465 3affba96 2019-09-22 stsp free(bline->id_str);
4466 3affba96 2019-09-22 stsp free(bline->committer);
4468 3affba96 2019-09-22 stsp free(bca.lines);
4470 28315671 2019-08-14 stsp free(bca.line_offsets);
4471 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4472 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4473 404c43c4 2018-06-21 stsp return error;
4476 5de5890b 2018-10-18 stsp __dead static void
4477 5de5890b 2018-10-18 stsp usage_tree(void)
4479 c1669e2e 2019-01-09 stsp fprintf(stderr,
4480 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4481 5de5890b 2018-10-18 stsp getprogname());
4485 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4486 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4487 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4489 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4490 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4491 848d6979 2019-08-12 stsp const char *modestr = "";
4492 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4493 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4495 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4496 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4499 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4500 63c5ca5d 2019-08-24 stsp modestr = "$";
4501 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4504 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4506 0d6c6ee3 2020-05-20 stsp return err;
4507 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4508 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4509 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4512 848d6979 2019-08-12 stsp modestr = "@";
4514 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4515 848d6979 2019-08-12 stsp modestr = "/";
4516 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4517 848d6979 2019-08-12 stsp modestr = "*";
4519 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4520 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4521 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4523 0d6c6ee3 2020-05-20 stsp free(link_target);
4524 0d6c6ee3 2020-05-20 stsp return NULL;
4527 5de5890b 2018-10-18 stsp static const struct got_error *
4528 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4529 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4530 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4532 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4533 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4534 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4535 56e0773d 2019-11-28 stsp int nentries, i;
4537 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4539 5de5890b 2018-10-18 stsp goto done;
4541 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4543 5de5890b 2018-10-18 stsp goto done;
4544 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4545 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4546 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4547 5de5890b 2018-10-18 stsp char *id = NULL;
4549 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4552 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4553 5de5890b 2018-10-18 stsp if (show_ids) {
4554 5de5890b 2018-10-18 stsp char *id_str;
4555 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4556 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4558 5de5890b 2018-10-18 stsp goto done;
4559 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4561 5de5890b 2018-10-18 stsp free(id_str);
4562 5de5890b 2018-10-18 stsp goto done;
4564 5de5890b 2018-10-18 stsp free(id_str);
4566 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4569 0d6c6ee3 2020-05-20 stsp goto done;
4571 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4572 c1669e2e 2019-01-09 stsp char *child_path;
4573 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4574 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4575 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4577 c1669e2e 2019-01-09 stsp goto done;
4579 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4580 c1669e2e 2019-01-09 stsp root_path, repo);
4581 c1669e2e 2019-01-09 stsp free(child_path);
4583 c1669e2e 2019-01-09 stsp goto done;
4588 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4589 5de5890b 2018-10-18 stsp free(tree_id);
4590 5de5890b 2018-10-18 stsp return err;
4593 5de5890b 2018-10-18 stsp static const struct got_error *
4594 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4596 5de5890b 2018-10-18 stsp const struct got_error *error;
4597 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4598 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4599 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4600 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4601 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4602 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4603 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4606 5de5890b 2018-10-18 stsp #ifndef PROFILE
4607 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4608 0f8d269b 2019-01-04 stsp NULL) == -1)
4609 5de5890b 2018-10-18 stsp err(1, "pledge");
4612 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4613 5de5890b 2018-10-18 stsp switch (ch) {
4615 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4618 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4619 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4620 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4622 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4625 5de5890b 2018-10-18 stsp show_ids = 1;
4628 c1669e2e 2019-01-09 stsp recurse = 1;
4631 2deda0b9 2019-03-07 stsp usage_tree();
4632 5de5890b 2018-10-18 stsp /* NOTREACHED */
4636 5de5890b 2018-10-18 stsp argc -= optind;
4637 5de5890b 2018-10-18 stsp argv += optind;
4639 5de5890b 2018-10-18 stsp if (argc == 1)
4640 5de5890b 2018-10-18 stsp path = argv[0];
4641 5de5890b 2018-10-18 stsp else if (argc > 1)
4642 5de5890b 2018-10-18 stsp usage_tree();
4644 7a2c19d6 2019-02-05 stsp path = NULL;
4646 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4647 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4648 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4649 9bf7a39b 2019-02-05 stsp goto done;
4651 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4652 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4653 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4654 7a2c19d6 2019-02-05 stsp goto done;
4656 7a2c19d6 2019-02-05 stsp error = NULL;
4657 7a2c19d6 2019-02-05 stsp if (worktree) {
4658 7a2c19d6 2019-02-05 stsp repo_path =
4659 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4660 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4661 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4662 7a2c19d6 2019-02-05 stsp if (error)
4663 7a2c19d6 2019-02-05 stsp goto done;
4665 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4666 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4667 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4668 7a2c19d6 2019-02-05 stsp goto done;
4673 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4674 5de5890b 2018-10-18 stsp if (error != NULL)
4675 c02c541e 2019-03-29 stsp goto done;
4677 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4678 c02c541e 2019-03-29 stsp if (error)
4679 5de5890b 2018-10-18 stsp goto done;
4681 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4682 9bf7a39b 2019-02-05 stsp if (worktree) {
4683 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4684 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4685 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4686 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4687 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4688 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4689 9bf7a39b 2019-02-05 stsp goto done;
4691 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4693 9bf7a39b 2019-02-05 stsp if (error)
4694 9bf7a39b 2019-02-05 stsp goto done;
4696 9bf7a39b 2019-02-05 stsp path = "/";
4698 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4699 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4700 9bf7a39b 2019-02-05 stsp if (error != NULL)
4701 9bf7a39b 2019-02-05 stsp goto done;
4704 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4705 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4706 4e0a20a4 2020-03-23 tracey if (worktree)
4707 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4709 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4710 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4711 5de5890b 2018-10-18 stsp if (error != NULL)
4712 5de5890b 2018-10-18 stsp goto done;
4713 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4714 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4715 5de5890b 2018-10-18 stsp if (error != NULL)
4716 5de5890b 2018-10-18 stsp goto done;
4718 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4719 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4720 30837e32 2019-07-25 stsp if (error)
4721 5de5890b 2018-10-18 stsp goto done;
4724 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4725 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4727 5de5890b 2018-10-18 stsp free(in_repo_path);
4728 5de5890b 2018-10-18 stsp free(repo_path);
4729 5de5890b 2018-10-18 stsp free(cwd);
4730 5de5890b 2018-10-18 stsp free(commit_id);
4731 7a2c19d6 2019-02-05 stsp if (worktree)
4732 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4733 5de5890b 2018-10-18 stsp if (repo) {
4734 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4735 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4736 5de5890b 2018-10-18 stsp if (error == NULL)
4737 5de5890b 2018-10-18 stsp error = repo_error;
4739 5de5890b 2018-10-18 stsp return error;
4742 6bad629b 2019-02-04 stsp __dead static void
4743 6bad629b 2019-02-04 stsp usage_status(void)
4745 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4746 081470ac 2020-08-13 stsp getprogname());
4750 b72f483a 2019-02-05 stsp static const struct got_error *
4751 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4752 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4753 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4754 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4756 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4757 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4758 081470ac 2020-08-13 stsp if (arg) {
4759 081470ac 2020-08-13 stsp char *status_codes = arg;
4760 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4762 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4763 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4764 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4767 081470ac 2020-08-13 stsp if (i == ncodes)
4768 081470ac 2020-08-13 stsp return NULL;
4770 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4771 b72f483a 2019-02-05 stsp return NULL;
4774 6bad629b 2019-02-04 stsp static const struct got_error *
4775 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4777 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4778 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4779 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4780 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4781 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4782 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4783 081470ac 2020-08-13 stsp int ch, i;
4785 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4787 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4788 6bad629b 2019-02-04 stsp switch (ch) {
4790 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4791 081470ac 2020-08-13 stsp switch (optarg[i]) {
4792 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4793 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4794 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4795 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4796 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4797 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4798 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4799 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4800 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4803 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4804 081470ac 2020-08-13 stsp optarg[i]);
4807 081470ac 2020-08-13 stsp status_codes = optarg;
4810 2deda0b9 2019-03-07 stsp usage_status();
4811 6bad629b 2019-02-04 stsp /* NOTREACHED */
4815 6bad629b 2019-02-04 stsp argc -= optind;
4816 6bad629b 2019-02-04 stsp argv += optind;
4818 6bad629b 2019-02-04 stsp #ifndef PROFILE
4819 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4820 6bad629b 2019-02-04 stsp NULL) == -1)
4821 6bad629b 2019-02-04 stsp err(1, "pledge");
4823 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4824 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4825 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4826 927df6b7 2019-02-10 stsp goto done;
4829 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4830 fa51e947 2020-03-27 stsp if (error) {
4831 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4832 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4833 a5edda0a 2019-07-27 stsp goto done;
4836 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4838 6bad629b 2019-02-04 stsp if (error != NULL)
4839 6bad629b 2019-02-04 stsp goto done;
4841 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4842 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4843 087fb88c 2019-08-04 stsp if (error)
4844 087fb88c 2019-08-04 stsp goto done;
4846 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4847 6bad629b 2019-02-04 stsp if (error)
4848 6bad629b 2019-02-04 stsp goto done;
4850 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4851 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4853 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4854 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4855 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4856 927df6b7 2019-02-10 stsp free(cwd);
4857 d0eebce4 2019-03-11 stsp return error;
4860 d0eebce4 2019-03-11 stsp __dead static void
4861 d0eebce4 2019-03-11 stsp usage_ref(void)
4863 d0eebce4 2019-03-11 stsp fprintf(stderr,
4864 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4865 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4866 d0eebce4 2019-03-11 stsp getprogname());
4870 d0eebce4 2019-03-11 stsp static const struct got_error *
4871 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4873 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4874 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4875 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4877 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4878 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4880 d0eebce4 2019-03-11 stsp return err;
4882 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4883 d0eebce4 2019-03-11 stsp char *refstr;
4884 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4885 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4886 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4887 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4888 d0eebce4 2019-03-11 stsp free(refstr);
4891 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4892 d0eebce4 2019-03-11 stsp return NULL;
4895 d0eebce4 2019-03-11 stsp static const struct got_error *
4896 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4898 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4899 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4901 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4903 d0eebce4 2019-03-11 stsp return err;
4905 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4906 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4907 d0eebce4 2019-03-11 stsp return err;
4910 d0eebce4 2019-03-11 stsp static const struct got_error *
4911 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4913 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4914 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4915 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4918 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4919 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4920 d1644381 2019-07-14 stsp * an unintended typo.
4922 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4923 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4925 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4927 d83d9d5c 2019-05-13 stsp if (err) {
4928 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4930 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4931 d83d9d5c 2019-05-13 stsp return err;
4932 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4934 d83d9d5c 2019-05-13 stsp return err;
4935 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4936 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4938 d83d9d5c 2019-05-13 stsp return err;
4941 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4943 d0eebce4 2019-03-11 stsp goto done;
4945 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4948 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4950 d1c1ae5f 2019-08-12 stsp return err;
4953 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4954 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4956 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4957 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4958 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4961 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4962 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4963 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4965 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4966 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4968 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4970 d1c1ae5f 2019-08-12 stsp return err;
4972 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4974 d1c1ae5f 2019-08-12 stsp goto done;
4976 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4978 d1c1ae5f 2019-08-12 stsp if (target_ref)
4979 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4981 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4982 d0eebce4 2019-03-11 stsp return err;
4985 d0eebce4 2019-03-11 stsp static const struct got_error *
4986 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4988 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4989 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4990 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4991 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4992 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4993 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4994 b2070a3f 2020-03-22 stsp char *refname = NULL;
4996 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4997 d0eebce4 2019-03-11 stsp switch (ch) {
4999 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5002 e31abbf2 2020-03-22 stsp do_delete = 1;
5005 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5006 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5007 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5009 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5012 d0eebce4 2019-03-11 stsp do_list = 1;
5015 e31abbf2 2020-03-22 stsp symref_target = optarg;
5018 d0eebce4 2019-03-11 stsp usage_ref();
5019 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5023 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5024 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5025 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5026 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5027 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5028 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5029 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5030 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5031 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5032 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5033 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5034 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5036 d0eebce4 2019-03-11 stsp argc -= optind;
5037 d0eebce4 2019-03-11 stsp argv += optind;
5039 e31abbf2 2020-03-22 stsp if (do_list) {
5040 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5041 d0eebce4 2019-03-11 stsp usage_ref();
5042 b2070a3f 2020-03-22 stsp if (argc == 1) {
5043 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5044 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5045 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5046 b2070a3f 2020-03-22 stsp goto done;
5050 e31abbf2 2020-03-22 stsp if (argc != 1)
5051 e31abbf2 2020-03-22 stsp usage_ref();
5052 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5053 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5054 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5055 b2070a3f 2020-03-22 stsp goto done;
5059 b2070a3f 2020-03-22 stsp if (refname)
5060 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5062 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5063 e0b57350 2019-03-12 stsp if (do_list) {
5064 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5065 e0b57350 2019-03-12 stsp NULL) == -1)
5066 e0b57350 2019-03-12 stsp err(1, "pledge");
5068 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5069 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5070 e0b57350 2019-03-12 stsp err(1, "pledge");
5073 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5074 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5075 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5076 d0eebce4 2019-03-11 stsp goto done;
5079 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5080 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5081 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5082 d0eebce4 2019-03-11 stsp goto done;
5084 d0eebce4 2019-03-11 stsp error = NULL;
5085 d0eebce4 2019-03-11 stsp if (worktree) {
5086 d0eebce4 2019-03-11 stsp repo_path =
5087 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5088 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5089 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5090 d0eebce4 2019-03-11 stsp if (error)
5091 d0eebce4 2019-03-11 stsp goto done;
5093 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5094 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5095 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5096 d0eebce4 2019-03-11 stsp goto done;
5101 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5102 d0eebce4 2019-03-11 stsp if (error != NULL)
5103 d0eebce4 2019-03-11 stsp goto done;
5105 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5106 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5107 c02c541e 2019-03-29 stsp if (error)
5108 c02c541e 2019-03-29 stsp goto done;
5110 d0eebce4 2019-03-11 stsp if (do_list)
5111 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5112 e31abbf2 2020-03-22 stsp else if (do_delete)
5113 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5114 e31abbf2 2020-03-22 stsp else if (symref_target)
5115 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5117 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5118 e31abbf2 2020-03-22 stsp usage_ref();
5119 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5122 b2070a3f 2020-03-22 stsp free(refname);
5124 4e759de4 2019-06-26 stsp got_repo_close(repo);
5125 4e759de4 2019-06-26 stsp if (worktree)
5126 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5127 4e759de4 2019-06-26 stsp free(cwd);
5128 4e759de4 2019-06-26 stsp free(repo_path);
5129 4e759de4 2019-06-26 stsp return error;
5132 4e759de4 2019-06-26 stsp __dead static void
5133 4e759de4 2019-06-26 stsp usage_branch(void)
5135 4e759de4 2019-06-26 stsp fprintf(stderr,
5136 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5137 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5141 4e759de4 2019-06-26 stsp static const struct got_error *
5142 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5143 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5145 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5146 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5147 4e99b47e 2019-10-04 stsp char *refstr;
5149 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5150 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5151 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5152 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5154 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5156 4e99b47e 2019-10-04 stsp return err;
5157 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5158 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5159 4e99b47e 2019-10-04 stsp marker = "* ";
5161 4e99b47e 2019-10-04 stsp marker = "~ ";
5165 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5166 4e99b47e 2019-10-04 stsp refname += 11;
5167 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5168 4e99b47e 2019-10-04 stsp refname += 18;
5170 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5171 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5172 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5174 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5175 4e99b47e 2019-10-04 stsp free(refstr);
5176 4e99b47e 2019-10-04 stsp return NULL;
5179 4e99b47e 2019-10-04 stsp static const struct got_error *
5180 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5182 ad89fa31 2019-10-04 stsp const char *refname;
5184 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5185 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5187 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5189 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5190 ad89fa31 2019-10-04 stsp refname += 11;
5191 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5192 ad89fa31 2019-10-04 stsp refname += 18;
5194 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5196 ad89fa31 2019-10-04 stsp return NULL;
5199 ad89fa31 2019-10-04 stsp static const struct got_error *
5200 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5202 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5203 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5204 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5205 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5206 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5208 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5210 4e99b47e 2019-10-04 stsp if (worktree) {
5211 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5212 4e99b47e 2019-10-04 stsp worktree);
5214 4e99b47e 2019-10-04 stsp return err;
5216 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5217 4e99b47e 2019-10-04 stsp worktree);
5219 4e99b47e 2019-10-04 stsp return err;
5221 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5222 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5223 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5225 4e99b47e 2019-10-04 stsp return err;
5226 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5227 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5231 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5232 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5234 4e759de4 2019-06-26 stsp return err;
5236 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5237 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5239 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5240 4e759de4 2019-06-26 stsp return NULL;
5243 4e759de4 2019-06-26 stsp static const struct got_error *
5244 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5245 45cd4e47 2019-08-25 stsp const char *branch_name)
5247 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5248 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5249 4e759de4 2019-06-26 stsp char *refname;
5251 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5252 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5254 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5256 4e759de4 2019-06-26 stsp goto done;
5258 45cd4e47 2019-08-25 stsp if (worktree &&
5259 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5260 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5261 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5262 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5263 45cd4e47 2019-08-25 stsp goto done;
5266 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5269 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5270 4e759de4 2019-06-26 stsp free(refname);
5271 4e759de4 2019-06-26 stsp return err;
5274 4e759de4 2019-06-26 stsp static const struct got_error *
5275 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5276 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5278 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5279 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5280 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5283 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5284 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5285 d3f84d51 2019-07-11 stsp * an unintended typo.
5287 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5288 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5290 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5291 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5292 4e759de4 2019-06-26 stsp goto done;
5295 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5296 4e759de4 2019-06-26 stsp if (err == NULL) {
5297 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5298 4e759de4 2019-06-26 stsp goto done;
5299 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5300 4e759de4 2019-06-26 stsp goto done;
5302 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5304 4e759de4 2019-06-26 stsp goto done;
5306 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5309 4e759de4 2019-06-26 stsp got_ref_close(ref);
5310 4e759de4 2019-06-26 stsp free(base_refname);
5311 4e759de4 2019-06-26 stsp free(refname);
5312 4e759de4 2019-06-26 stsp return err;
5315 4e759de4 2019-06-26 stsp static const struct got_error *
5316 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5318 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5319 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5320 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5321 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5322 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5323 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5324 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5325 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5326 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5327 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5328 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5330 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5332 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5333 4e759de4 2019-06-26 stsp switch (ch) {
5335 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5338 4e759de4 2019-06-26 stsp delref = optarg;
5341 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5342 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5343 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5345 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5348 4e759de4 2019-06-26 stsp do_list = 1;
5351 da76fce2 2020-02-24 stsp do_update = 0;
5354 4e759de4 2019-06-26 stsp usage_branch();
5355 4e759de4 2019-06-26 stsp /* NOTREACHED */
5359 4e759de4 2019-06-26 stsp if (do_list && delref)
5360 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5362 4e759de4 2019-06-26 stsp argc -= optind;
5363 4e759de4 2019-06-26 stsp argv += optind;
5365 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5366 ad89fa31 2019-10-04 stsp do_show = 1;
5368 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5369 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5371 4e759de4 2019-06-26 stsp if (do_list || delref) {
5372 4e759de4 2019-06-26 stsp if (argc > 0)
5373 4e759de4 2019-06-26 stsp usage_branch();
5374 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5375 4e759de4 2019-06-26 stsp usage_branch();
5377 4e759de4 2019-06-26 stsp #ifndef PROFILE
5378 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5379 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5380 4e759de4 2019-06-26 stsp NULL) == -1)
5381 4e759de4 2019-06-26 stsp err(1, "pledge");
5383 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5384 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5385 4e759de4 2019-06-26 stsp err(1, "pledge");
5388 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5389 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5390 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5391 4e759de4 2019-06-26 stsp goto done;
5394 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5395 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5396 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5397 4e759de4 2019-06-26 stsp goto done;
5399 4e759de4 2019-06-26 stsp error = NULL;
5400 4e759de4 2019-06-26 stsp if (worktree) {
5401 4e759de4 2019-06-26 stsp repo_path =
5402 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5403 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5404 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5405 4e759de4 2019-06-26 stsp if (error)
5406 4e759de4 2019-06-26 stsp goto done;
5408 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5409 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5410 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5411 4e759de4 2019-06-26 stsp goto done;
5416 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5417 4e759de4 2019-06-26 stsp if (error != NULL)
5418 4e759de4 2019-06-26 stsp goto done;
5420 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5421 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5422 4e759de4 2019-06-26 stsp if (error)
5423 4e759de4 2019-06-26 stsp goto done;
5425 ad89fa31 2019-10-04 stsp if (do_show)
5426 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5427 ad89fa31 2019-10-04 stsp else if (do_list)
5428 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5429 4e759de4 2019-06-26 stsp else if (delref)
5430 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5432 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5433 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5434 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5435 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5436 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5437 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5438 a74f7e83 2019-11-10 stsp if (error)
5439 a74f7e83 2019-11-10 stsp goto done;
5440 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5441 da76fce2 2020-02-24 stsp if (error)
5442 da76fce2 2020-02-24 stsp goto done;
5443 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5444 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5445 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5447 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5448 da76fce2 2020-02-24 stsp if (error)
5449 da76fce2 2020-02-24 stsp goto done;
5450 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5451 da76fce2 2020-02-24 stsp worktree);
5452 da76fce2 2020-02-24 stsp if (error)
5453 da76fce2 2020-02-24 stsp goto done;
5454 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5456 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5457 da76fce2 2020-02-24 stsp goto done;
5459 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5460 da76fce2 2020-02-24 stsp free(branch_refname);
5461 da76fce2 2020-02-24 stsp if (error)
5462 da76fce2 2020-02-24 stsp goto done;
5463 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5465 da76fce2 2020-02-24 stsp if (error)
5466 da76fce2 2020-02-24 stsp goto done;
5467 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5468 da76fce2 2020-02-24 stsp commit_id);
5469 da76fce2 2020-02-24 stsp if (error)
5470 da76fce2 2020-02-24 stsp goto done;
5471 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5472 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5473 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5475 da76fce2 2020-02-24 stsp if (error)
5476 da76fce2 2020-02-24 stsp goto done;
5477 9627c110 2020-04-18 stsp if (upa.did_something)
5478 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5479 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5484 da76fce2 2020-02-24 stsp got_ref_close(ref);
5486 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5487 d0eebce4 2019-03-11 stsp if (worktree)
5488 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5489 d0eebce4 2019-03-11 stsp free(cwd);
5490 d0eebce4 2019-03-11 stsp free(repo_path);
5491 da76fce2 2020-02-24 stsp free(commit_id);
5492 da76fce2 2020-02-24 stsp free(commit_id_str);
5493 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5494 da76fce2 2020-02-24 stsp free((char *)pe->path);
5495 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5496 d00136be 2019-03-26 stsp return error;
5500 d00136be 2019-03-26 stsp __dead static void
5501 8e7bd50a 2019-08-22 stsp usage_tag(void)
5503 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5504 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5505 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5510 b8bad2ba 2019-08-23 stsp static const struct got_error *
5511 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5513 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5514 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5515 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5516 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5517 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5519 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5520 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5521 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5522 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5524 b8bad2ba 2019-08-23 stsp return err;
5525 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5528 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5531 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5532 b8bad2ba 2019-08-23 stsp free(re_id);
5535 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5536 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5539 b8bad2ba 2019-08-23 stsp while (se) {
5540 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5543 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5544 b8bad2ba 2019-08-23 stsp free(se_id);
5547 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5548 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5550 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5551 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5553 b8bad2ba 2019-08-23 stsp return err;
5554 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5557 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5562 b8bad2ba 2019-08-23 stsp return err;
5566 b8bad2ba 2019-08-23 stsp static const struct got_error *
5567 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5569 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5570 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5571 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5573 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5575 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5577 8e7bd50a 2019-08-22 stsp return err;
5579 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5580 8e7bd50a 2019-08-22 stsp const char *refname;
5581 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5582 8e7bd50a 2019-08-22 stsp char datebuf[26];
5583 d4efa91b 2020-01-14 stsp const char *tagger;
5584 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5585 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5586 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5587 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5589 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5590 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5592 8e7bd50a 2019-08-22 stsp refname += 10;
5593 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5594 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5595 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5598 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5599 8e7bd50a 2019-08-22 stsp free(refstr);
5601 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5604 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5605 d4efa91b 2020-01-14 stsp if (err) {
5606 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5610 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5611 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5612 d4efa91b 2020-01-14 stsp if (err) {
5616 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5617 d4efa91b 2020-01-14 stsp tagger_time =
5618 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5619 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5625 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5626 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5627 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5628 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5632 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5633 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5634 2417344c 2019-08-23 stsp if (datestr)
5635 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5636 d4efa91b 2020-01-14 stsp if (commit)
5637 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5639 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5640 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5641 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5644 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5645 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5648 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5649 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5652 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5653 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5660 8e7bd50a 2019-08-22 stsp free(id_str);
5661 d4efa91b 2020-01-14 stsp if (commit) {
5662 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5665 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5667 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5668 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5669 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5670 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5675 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5677 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5679 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5680 8e7bd50a 2019-08-22 stsp } while (line);
5681 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5684 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5685 8e7bd50a 2019-08-22 stsp return NULL;
5688 8e7bd50a 2019-08-22 stsp static const struct got_error *
5689 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5690 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5692 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5693 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5694 f372d5cd 2019-10-21 stsp char *editor = NULL;
5695 1601cb9f 2020-09-11 naddy int initial_content_len;
5696 8e7bd50a 2019-08-22 stsp int fd = -1;
5698 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5699 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5700 8e7bd50a 2019-08-22 stsp goto done;
5703 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5704 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5705 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5706 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5707 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5708 8e7bd50a 2019-08-22 stsp goto done;
5711 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5713 8e7bd50a 2019-08-22 stsp goto done;
5715 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5716 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5717 97972933 2020-09-11 stsp goto done;
5720 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5722 8e7bd50a 2019-08-22 stsp goto done;
5723 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5725 8e7bd50a 2019-08-22 stsp free(initial_content);
5726 8e7bd50a 2019-08-22 stsp free(template);
5727 8e7bd50a 2019-08-22 stsp free(editor);
5729 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5730 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5732 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5733 59f86c76 2020-09-11 stsp if (err == NULL)
5734 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5735 59f86c76 2020-09-11 stsp if (err) {
5736 59f86c76 2020-09-11 stsp free(*tagmsg);
5737 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5739 8e7bd50a 2019-08-22 stsp return err;
5742 8e7bd50a 2019-08-22 stsp static const struct got_error *
5743 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5744 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5746 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5747 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5748 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5749 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5750 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5751 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5752 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5755 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5756 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5757 8e7bd50a 2019-08-22 stsp * an unintended typo.
5759 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5760 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5762 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5764 8e7bd50a 2019-08-22 stsp return err;
5766 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5767 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5769 8e7bd50a 2019-08-22 stsp goto done;
5771 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5773 8e7bd50a 2019-08-22 stsp goto done;
5775 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5776 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5777 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5778 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5779 8e7bd50a 2019-08-22 stsp goto done;
5781 8e7bd50a 2019-08-22 stsp tag_name += 10;
5782 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5783 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5784 8e7bd50a 2019-08-22 stsp goto done;
5787 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5788 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5789 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5790 8e7bd50a 2019-08-22 stsp goto done;
5791 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5792 8e7bd50a 2019-08-22 stsp goto done;
5794 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5795 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5796 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5797 f372d5cd 2019-10-21 stsp if (err) {
5798 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5799 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5800 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5801 8e7bd50a 2019-08-22 stsp goto done;
5805 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5806 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5807 f372d5cd 2019-10-21 stsp if (err) {
5808 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5809 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5810 8e7bd50a 2019-08-22 stsp goto done;
5813 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5814 f372d5cd 2019-10-21 stsp if (err) {
5815 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5816 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5817 8e7bd50a 2019-08-22 stsp goto done;
5820 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5821 f372d5cd 2019-10-21 stsp if (err) {
5822 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5823 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5824 f372d5cd 2019-10-21 stsp goto done;
5827 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5828 f372d5cd 2019-10-21 stsp if (err) {
5829 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5830 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5831 f372d5cd 2019-10-21 stsp goto done;
5833 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5835 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5836 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5837 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5838 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5839 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5840 f372d5cd 2019-10-21 stsp free(tag_id_str);
5842 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5843 8e7bd50a 2019-08-22 stsp free(commit_id);
5844 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5845 8e7bd50a 2019-08-22 stsp free(refname);
5846 8e7bd50a 2019-08-22 stsp free(tagmsg);
5847 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5848 aba9c984 2019-09-08 stsp free(tagger);
5849 8e7bd50a 2019-08-22 stsp return err;
5852 8e7bd50a 2019-08-22 stsp static const struct got_error *
5853 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5855 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5856 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5857 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5858 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5859 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5860 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5861 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5863 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5864 8e7bd50a 2019-08-22 stsp switch (ch) {
5866 80106605 2020-02-24 stsp commit_id_arg = optarg;
5869 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5872 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5873 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5874 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5876 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5879 8e7bd50a 2019-08-22 stsp do_list = 1;
5882 8e7bd50a 2019-08-22 stsp usage_tag();
5883 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5887 8e7bd50a 2019-08-22 stsp argc -= optind;
5888 8e7bd50a 2019-08-22 stsp argv += optind;
5890 8e7bd50a 2019-08-22 stsp if (do_list) {
5891 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5893 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5894 8e7bd50a 2019-08-22 stsp if (tagmsg)
5895 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5896 8e7bd50a 2019-08-22 stsp if (argc > 0)
5897 8e7bd50a 2019-08-22 stsp usage_tag();
5898 80106605 2020-02-24 stsp } else if (argc != 1)
5899 8e7bd50a 2019-08-22 stsp usage_tag();
5901 a2887370 2019-08-23 stsp tag_name = argv[0];
5903 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5904 8e7bd50a 2019-08-22 stsp if (do_list) {
5905 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5906 8e7bd50a 2019-08-22 stsp NULL) == -1)
5907 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5909 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5910 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5911 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5914 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5915 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5916 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5917 8e7bd50a 2019-08-22 stsp goto done;
5920 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5921 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5922 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5923 8e7bd50a 2019-08-22 stsp goto done;
5925 8e7bd50a 2019-08-22 stsp error = NULL;
5926 8e7bd50a 2019-08-22 stsp if (worktree) {
5927 8e7bd50a 2019-08-22 stsp repo_path =
5928 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5929 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5930 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5931 8e7bd50a 2019-08-22 stsp if (error)
5932 8e7bd50a 2019-08-22 stsp goto done;
5934 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5935 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5936 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5937 8e7bd50a 2019-08-22 stsp goto done;
5942 8e7bd50a 2019-08-22 stsp if (do_list) {
5943 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5944 c9956ddf 2019-09-08 stsp if (error != NULL)
5945 c9956ddf 2019-09-08 stsp goto done;
5946 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5947 8e7bd50a 2019-08-22 stsp if (error)
5948 8e7bd50a 2019-08-22 stsp goto done;
5949 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5951 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5952 c9956ddf 2019-09-08 stsp if (error)
5953 c9956ddf 2019-09-08 stsp goto done;
5954 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5955 c9956ddf 2019-09-08 stsp if (error != NULL)
5956 c9956ddf 2019-09-08 stsp goto done;
5958 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5959 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5960 8e7bd50a 2019-08-22 stsp if (error)
5961 8e7bd50a 2019-08-22 stsp goto done;
5964 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5965 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5966 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5967 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5968 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5969 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5970 8e7bd50a 2019-08-22 stsp if (error)
5971 8e7bd50a 2019-08-22 stsp goto done;
5972 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5973 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5974 8e7bd50a 2019-08-22 stsp if (error)
5975 8e7bd50a 2019-08-22 stsp goto done;
5976 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5977 8e7bd50a 2019-08-22 stsp free(commit_id);
5978 8e7bd50a 2019-08-22 stsp if (error)
5979 8e7bd50a 2019-08-22 stsp goto done;
5982 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
5983 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5987 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5988 8e7bd50a 2019-08-22 stsp if (worktree)
5989 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5990 8e7bd50a 2019-08-22 stsp free(cwd);
5991 8e7bd50a 2019-08-22 stsp free(repo_path);
5992 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5993 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5994 8e7bd50a 2019-08-22 stsp return error;
5997 8e7bd50a 2019-08-22 stsp __dead static void
5998 d00136be 2019-03-26 stsp usage_add(void)
6000 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6001 022fae89 2019-12-06 tracey getprogname());
6005 d00136be 2019-03-26 stsp static const struct got_error *
6006 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6008 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6010 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6011 4e68cba3 2019-11-23 stsp return NULL;
6014 4e68cba3 2019-11-23 stsp static const struct got_error *
6015 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6017 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6018 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6019 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6020 1dd54920 2019-05-11 stsp char *cwd = NULL;
6021 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6022 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6023 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6025 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6027 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6028 d00136be 2019-03-26 stsp switch (ch) {
6029 022fae89 2019-12-06 tracey case 'I':
6030 022fae89 2019-12-06 tracey no_ignores = 1;
6033 4e68cba3 2019-11-23 stsp can_recurse = 1;
6036 d00136be 2019-03-26 stsp usage_add();
6037 d00136be 2019-03-26 stsp /* NOTREACHED */
6041 d00136be 2019-03-26 stsp argc -= optind;
6042 d00136be 2019-03-26 stsp argv += optind;
6044 43012d58 2019-07-14 stsp #ifndef PROFILE
6045 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6046 43012d58 2019-07-14 stsp NULL) == -1)
6047 43012d58 2019-07-14 stsp err(1, "pledge");
6049 723c305c 2019-05-11 jcs if (argc < 1)
6050 d00136be 2019-03-26 stsp usage_add();
6052 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6053 d00136be 2019-03-26 stsp if (cwd == NULL) {
6054 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6055 d00136be 2019-03-26 stsp goto done;
6058 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6059 fa51e947 2020-03-27 stsp if (error) {
6060 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6061 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6062 d00136be 2019-03-26 stsp goto done;
6065 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6067 031a5338 2019-03-26 stsp if (error != NULL)
6068 031a5338 2019-03-26 stsp goto done;
6070 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6071 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6072 d00136be 2019-03-26 stsp if (error)
6073 d00136be 2019-03-26 stsp goto done;
6075 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6076 6d022e97 2019-08-04 stsp if (error)
6077 022fae89 2019-12-06 tracey goto done;
6079 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6080 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6081 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6082 6d022e97 2019-08-04 stsp goto done;
6086 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6087 4e68cba3 2019-11-23 stsp char *ondisk_path;
6088 4e68cba3 2019-11-23 stsp struct stat sb;
6089 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6090 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6091 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6092 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6093 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6094 4e68cba3 2019-11-23 stsp goto done;
6096 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6097 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6098 4e68cba3 2019-11-23 stsp free(ondisk_path);
6101 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6102 4e68cba3 2019-11-23 stsp ondisk_path);
6103 4e68cba3 2019-11-23 stsp free(ondisk_path);
6104 4e68cba3 2019-11-23 stsp goto done;
6106 4e68cba3 2019-11-23 stsp free(ondisk_path);
6107 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6108 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6109 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6110 4e68cba3 2019-11-23 stsp goto done;
6115 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6116 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6119 031a5338 2019-03-26 stsp got_repo_close(repo);
6120 d00136be 2019-03-26 stsp if (worktree)
6121 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6122 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6123 1dd54920 2019-05-11 stsp free((char *)pe->path);
6124 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6125 2ec1f75b 2019-03-26 stsp free(cwd);
6126 2ec1f75b 2019-03-26 stsp return error;
6129 2ec1f75b 2019-03-26 stsp __dead static void
6130 648e4ef7 2019-07-09 stsp usage_remove(void)
6132 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6133 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6137 2a06fe5f 2019-08-24 stsp static const struct got_error *
6138 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6139 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6141 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6143 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6144 2a06fe5f 2019-08-24 stsp return NULL;
6145 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6146 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6147 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6148 2a06fe5f 2019-08-24 stsp return NULL;
6151 2ec1f75b 2019-03-26 stsp static const struct got_error *
6152 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6154 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6155 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6156 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6157 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6158 17ed4618 2019-06-02 stsp char *cwd = NULL;
6159 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6160 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6161 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6163 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6165 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6166 2ec1f75b 2019-03-26 stsp switch (ch) {
6168 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6170 70e3e7f5 2019-12-13 tracey case 'k':
6171 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6173 f2a9dc41 2019-12-13 tracey case 'R':
6174 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6177 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6178 766841c2 2020-08-13 stsp switch (optarg[i]) {
6179 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6180 766841c2 2020-08-13 stsp delete_local_mods = 1;
6182 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6185 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6186 766841c2 2020-08-13 stsp optarg[i]);
6189 766841c2 2020-08-13 stsp status_codes = optarg;
6192 f2a9dc41 2019-12-13 tracey usage_remove();
6193 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6197 2ec1f75b 2019-03-26 stsp argc -= optind;
6198 2ec1f75b 2019-03-26 stsp argv += optind;
6200 43012d58 2019-07-14 stsp #ifndef PROFILE
6201 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6202 43012d58 2019-07-14 stsp NULL) == -1)
6203 43012d58 2019-07-14 stsp err(1, "pledge");
6205 17ed4618 2019-06-02 stsp if (argc < 1)
6206 648e4ef7 2019-07-09 stsp usage_remove();
6208 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6209 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6210 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6211 2ec1f75b 2019-03-26 stsp goto done;
6213 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6214 fa51e947 2020-03-27 stsp if (error) {
6215 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6216 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6217 2ec1f75b 2019-03-26 stsp goto done;
6220 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6223 2ec1f75b 2019-03-26 stsp goto done;
6225 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6227 2ec1f75b 2019-03-26 stsp if (error)
6228 2ec1f75b 2019-03-26 stsp goto done;
6230 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6231 6d022e97 2019-08-04 stsp if (error)
6232 6d022e97 2019-08-04 stsp goto done;
6234 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6235 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6236 f2a9dc41 2019-12-13 tracey struct stat sb;
6237 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6238 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6239 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6240 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6241 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6242 f2a9dc41 2019-12-13 tracey goto done;
6244 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6245 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6246 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6247 f2a9dc41 2019-12-13 tracey continue;
6249 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6250 f2a9dc41 2019-12-13 tracey ondisk_path);
6251 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6252 f2a9dc41 2019-12-13 tracey goto done;
6254 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6255 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6256 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6257 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6258 f2a9dc41 2019-12-13 tracey goto done;
6263 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6264 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6265 766841c2 2020-08-13 stsp repo, keep_on_disk);
6268 a129376b 2019-03-28 stsp got_repo_close(repo);
6269 a129376b 2019-03-28 stsp if (worktree)
6270 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6271 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6272 17ed4618 2019-06-02 stsp free((char *)pe->path);
6273 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6274 a129376b 2019-03-28 stsp free(cwd);
6275 a129376b 2019-03-28 stsp return error;
6278 a129376b 2019-03-28 stsp __dead static void
6279 a129376b 2019-03-28 stsp usage_revert(void)
6281 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6282 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6286 1ee397ad 2019-07-12 stsp static const struct got_error *
6287 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6289 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6290 fb9704af 2020-01-27 stsp return NULL;
6292 a129376b 2019-03-28 stsp while (path[0] == '/')
6294 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6295 33aa809d 2019-08-08 stsp return NULL;
6298 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6299 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6300 33aa809d 2019-08-08 stsp const char *action;
6303 33aa809d 2019-08-08 stsp static const struct got_error *
6304 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6305 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6307 33aa809d 2019-08-08 stsp char *line = NULL;
6308 33aa809d 2019-08-08 stsp size_t linesize = 0;
6309 33aa809d 2019-08-08 stsp ssize_t linelen;
6311 33aa809d 2019-08-08 stsp switch (status) {
6312 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6313 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6315 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6316 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6318 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6319 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6320 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6321 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6322 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6323 33aa809d 2019-08-08 stsp printf("%s", line);
6324 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6325 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6326 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6327 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6328 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6331 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6334 33aa809d 2019-08-08 stsp return NULL;
6337 33aa809d 2019-08-08 stsp static const struct got_error *
6338 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6339 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6341 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6342 33aa809d 2019-08-08 stsp char *line = NULL;
6343 33aa809d 2019-08-08 stsp size_t linesize = 0;
6344 33aa809d 2019-08-08 stsp ssize_t linelen;
6345 33aa809d 2019-08-08 stsp int resp = ' ';
6346 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6348 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6350 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6352 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6353 33aa809d 2019-08-08 stsp a->action);
6355 33aa809d 2019-08-08 stsp return err;
6356 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6357 33aa809d 2019-08-08 stsp if (linelen == -1) {
6358 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6359 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6360 33aa809d 2019-08-08 stsp return NULL;
6362 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6364 33aa809d 2019-08-08 stsp *nl = '\0';
6365 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6366 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6367 33aa809d 2019-08-08 stsp printf("y\n");
6368 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6369 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6370 33aa809d 2019-08-08 stsp printf("n\n");
6371 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6372 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6373 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6374 33aa809d 2019-08-08 stsp printf("q\n");
6376 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6377 33aa809d 2019-08-08 stsp free(line);
6378 33aa809d 2019-08-08 stsp return NULL;
6381 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6382 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6383 33aa809d 2019-08-08 stsp a->action);
6385 33aa809d 2019-08-08 stsp return err;
6386 33aa809d 2019-08-08 stsp resp = getchar();
6387 33aa809d 2019-08-08 stsp if (resp == '\n')
6388 33aa809d 2019-08-08 stsp resp = getchar();
6389 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6390 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6391 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6392 33aa809d 2019-08-08 stsp resp = ' ';
6394 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6395 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6396 33aa809d 2019-08-08 stsp resp = ' ';
6400 33aa809d 2019-08-08 stsp if (resp == 'y')
6401 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6402 33aa809d 2019-08-08 stsp else if (resp == 'n')
6403 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6404 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6405 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6407 1ee397ad 2019-07-12 stsp return NULL;
6411 a129376b 2019-03-28 stsp static const struct got_error *
6412 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6414 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6415 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6416 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6417 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6418 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6419 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6420 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6421 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6422 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6423 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6425 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6427 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6428 a129376b 2019-03-28 stsp switch (ch) {
6430 33aa809d 2019-08-08 stsp pflag = 1;
6433 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6436 0f6d7415 2019-08-08 stsp can_recurse = 1;
6439 a129376b 2019-03-28 stsp usage_revert();
6440 a129376b 2019-03-28 stsp /* NOTREACHED */
6444 a129376b 2019-03-28 stsp argc -= optind;
6445 a129376b 2019-03-28 stsp argv += optind;
6447 43012d58 2019-07-14 stsp #ifndef PROFILE
6448 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6449 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6450 43012d58 2019-07-14 stsp err(1, "pledge");
6452 e20a8b6f 2019-06-04 stsp if (argc < 1)
6453 a129376b 2019-03-28 stsp usage_revert();
6454 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6455 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6457 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6458 a129376b 2019-03-28 stsp if (cwd == NULL) {
6459 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6460 a129376b 2019-03-28 stsp goto done;
6462 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6463 fa51e947 2020-03-27 stsp if (error) {
6464 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6465 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6466 2ec1f75b 2019-03-26 stsp goto done;
6469 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6471 a129376b 2019-03-28 stsp if (error != NULL)
6472 a129376b 2019-03-28 stsp goto done;
6474 33aa809d 2019-08-08 stsp if (patch_script_path) {
6475 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6476 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6477 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6478 33aa809d 2019-08-08 stsp patch_script_path);
6479 33aa809d 2019-08-08 stsp goto done;
6482 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6483 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6484 a129376b 2019-03-28 stsp if (error)
6485 a129376b 2019-03-28 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 6d022e97 2019-08-04 stsp goto done;
6491 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6492 0f6d7415 2019-08-08 stsp char *ondisk_path;
6493 0f6d7415 2019-08-08 stsp struct stat sb;
6494 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6495 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6496 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6497 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6498 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6499 0f6d7415 2019-08-08 stsp goto done;
6501 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6502 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6503 0f6d7415 2019-08-08 stsp free(ondisk_path);
6506 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6507 0f6d7415 2019-08-08 stsp ondisk_path);
6508 0f6d7415 2019-08-08 stsp free(ondisk_path);
6509 0f6d7415 2019-08-08 stsp goto done;
6511 0f6d7415 2019-08-08 stsp free(ondisk_path);
6512 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6513 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6514 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6515 0f6d7415 2019-08-08 stsp goto done;
6520 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6521 33aa809d 2019-08-08 stsp cpa.action = "revert";
6522 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6523 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6525 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6526 33aa809d 2019-08-08 stsp error == NULL)
6527 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6529 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6530 2ec1f75b 2019-03-26 stsp if (worktree)
6531 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6532 2ec1f75b 2019-03-26 stsp free(path);
6533 d00136be 2019-03-26 stsp free(cwd);
6534 6bad629b 2019-02-04 stsp return error;
6537 c4296144 2019-05-09 stsp __dead static void
6538 c4296144 2019-05-09 stsp usage_commit(void)
6540 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6541 5c1e53bc 2019-07-28 stsp getprogname());
6545 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6546 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6547 e2ba3d07 2019-05-13 stsp const char *editor;
6548 e0870e44 2019-05-13 stsp const char *worktree_path;
6549 76d98825 2019-06-03 stsp const char *branch_name;
6550 314a6357 2019-05-13 stsp const char *repo_path;
6551 e0870e44 2019-05-13 stsp char *logmsg_path;
6555 33ad4cbe 2019-05-12 jcs static const struct got_error *
6556 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6559 76d98825 2019-06-03 stsp char *initial_content = NULL;
6560 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6561 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6562 e0870e44 2019-05-13 stsp char *template = NULL;
6563 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6564 1601cb9f 2020-09-11 naddy int initial_content_len;
6565 97972933 2020-09-11 stsp int fd = -1;
6566 33ad4cbe 2019-05-12 jcs size_t len;
6568 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6569 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6570 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6571 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6572 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6573 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6574 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6575 33ad4cbe 2019-05-12 jcs return NULL;
6578 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6579 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6581 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6582 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6583 1601cb9f 2020-09-11 naddy a->branch_name);
6584 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6585 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6587 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6589 e0870e44 2019-05-13 stsp goto done;
6591 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6592 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6593 97972933 2020-09-11 stsp goto done;
6596 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6597 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6598 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6599 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6600 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6603 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6605 76d98825 2019-06-03 stsp free(initial_content);
6606 e0870e44 2019-05-13 stsp free(template);
6608 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6609 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6611 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6612 59f86c76 2020-09-11 stsp if (err == NULL)
6613 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6614 59f86c76 2020-09-11 stsp if (err) {
6615 59f86c76 2020-09-11 stsp free(*logmsg);
6616 59f86c76 2020-09-11 stsp *logmsg = NULL;
6618 33ad4cbe 2019-05-12 jcs return err;
6621 c4296144 2019-05-09 stsp static const struct got_error *
6622 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6624 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6625 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6626 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6627 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6628 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6629 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6630 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6631 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6632 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6633 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6634 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6636 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6637 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6639 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6640 c4296144 2019-05-09 stsp switch (ch) {
6642 c4296144 2019-05-09 stsp logmsg = optarg;
6645 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6648 c4296144 2019-05-09 stsp usage_commit();
6649 c4296144 2019-05-09 stsp /* NOTREACHED */
6653 c4296144 2019-05-09 stsp argc -= optind;
6654 c4296144 2019-05-09 stsp argv += optind;
6656 43012d58 2019-07-14 stsp #ifndef PROFILE
6657 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6658 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6659 43012d58 2019-07-14 stsp err(1, "pledge");
6661 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6662 c4296144 2019-05-09 stsp if (cwd == NULL) {
6663 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6664 c4296144 2019-05-09 stsp goto done;
6666 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6667 fa51e947 2020-03-27 stsp if (error) {
6668 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6669 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6670 7d5807f4 2019-07-11 stsp goto done;
6673 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6674 c4296144 2019-05-09 stsp if (error)
6675 a698f62e 2019-07-25 stsp goto done;
6676 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6677 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6678 c4296144 2019-05-09 stsp goto done;
6681 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6682 916f288c 2019-07-30 stsp worktree);
6683 5c1e53bc 2019-07-28 stsp if (error)
6684 5c1e53bc 2019-07-28 stsp goto done;
6686 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6687 c9956ddf 2019-09-08 stsp if (error)
6688 c9956ddf 2019-09-08 stsp goto done;
6689 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6690 c9956ddf 2019-09-08 stsp gitconfig_path);
6691 c4296144 2019-05-09 stsp if (error != NULL)
6692 c4296144 2019-05-09 stsp goto done;
6694 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6695 aba9c984 2019-09-08 stsp if (error)
6696 aba9c984 2019-09-08 stsp return error;
6699 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6700 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6702 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6703 314a6357 2019-05-13 stsp error = get_editor(&editor);
6705 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6706 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6707 c4296144 2019-05-09 stsp if (error)
6708 c4296144 2019-05-09 stsp goto done;
6710 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6711 087fb88c 2019-08-04 stsp if (error)
6712 087fb88c 2019-08-04 stsp goto done;
6714 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6715 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6716 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6717 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6718 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6719 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6720 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6721 916f288c 2019-07-30 stsp goto done;
6723 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6725 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6726 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6727 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6728 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6729 e0870e44 2019-05-13 stsp if (error) {
6730 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6731 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6732 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6733 c4296144 2019-05-09 stsp goto done;
6736 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6737 c4296144 2019-05-09 stsp if (error)
6738 c4296144 2019-05-09 stsp goto done;
6739 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6741 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6742 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6743 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6744 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6745 7266f21f 2019-10-21 stsp error == NULL)
6746 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6747 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6749 c4296144 2019-05-09 stsp got_repo_close(repo);
6750 c4296144 2019-05-09 stsp if (worktree)
6751 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6752 c4296144 2019-05-09 stsp free(cwd);
6753 c4296144 2019-05-09 stsp free(id_str);
6754 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6755 e2ba3d07 2019-05-13 stsp free(editor);
6756 aba9c984 2019-09-08 stsp free(author);
6757 234035bc 2019-06-01 stsp return error;
6760 234035bc 2019-06-01 stsp __dead static void
6761 234035bc 2019-06-01 stsp usage_cherrypick(void)
6763 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6767 234035bc 2019-06-01 stsp static const struct got_error *
6768 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6770 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6771 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6772 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6773 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6774 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6775 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6776 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6777 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6779 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6781 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6782 234035bc 2019-06-01 stsp switch (ch) {
6784 234035bc 2019-06-01 stsp usage_cherrypick();
6785 234035bc 2019-06-01 stsp /* NOTREACHED */
6789 234035bc 2019-06-01 stsp argc -= optind;
6790 234035bc 2019-06-01 stsp argv += optind;
6792 43012d58 2019-07-14 stsp #ifndef PROFILE
6793 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6794 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6795 43012d58 2019-07-14 stsp err(1, "pledge");
6797 234035bc 2019-06-01 stsp if (argc != 1)
6798 234035bc 2019-06-01 stsp usage_cherrypick();
6800 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6801 234035bc 2019-06-01 stsp if (cwd == NULL) {
6802 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6803 234035bc 2019-06-01 stsp goto done;
6805 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6806 fa51e947 2020-03-27 stsp if (error) {
6807 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6808 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6810 234035bc 2019-06-01 stsp goto done;
6813 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6815 234035bc 2019-06-01 stsp if (error != NULL)
6816 234035bc 2019-06-01 stsp goto done;
6818 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6819 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6820 234035bc 2019-06-01 stsp if (error)
6821 234035bc 2019-06-01 stsp goto done;
6823 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6824 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6825 234035bc 2019-06-01 stsp if (error != NULL) {
6826 234035bc 2019-06-01 stsp struct got_reference *ref;
6827 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6828 234035bc 2019-06-01 stsp goto done;
6829 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6830 234035bc 2019-06-01 stsp if (error != NULL)
6831 234035bc 2019-06-01 stsp goto done;
6832 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6833 234035bc 2019-06-01 stsp got_ref_close(ref);
6834 234035bc 2019-06-01 stsp if (error != NULL)
6835 234035bc 2019-06-01 stsp goto done;
6837 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6838 234035bc 2019-06-01 stsp if (error)
6839 234035bc 2019-06-01 stsp goto done;
6841 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6842 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6843 234035bc 2019-06-01 stsp if (error != NULL)
6844 234035bc 2019-06-01 stsp goto done;
6846 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6847 234035bc 2019-06-01 stsp if (error) {
6848 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6849 234035bc 2019-06-01 stsp goto done;
6850 234035bc 2019-06-01 stsp error = NULL;
6852 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6853 234035bc 2019-06-01 stsp goto done;
6856 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6857 234035bc 2019-06-01 stsp if (error)
6858 234035bc 2019-06-01 stsp goto done;
6859 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6860 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6861 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6862 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6864 234035bc 2019-06-01 stsp if (error != NULL)
6865 234035bc 2019-06-01 stsp goto done;
6867 9627c110 2020-04-18 stsp if (upa.did_something)
6868 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6869 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6871 234035bc 2019-06-01 stsp if (commit)
6872 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6873 234035bc 2019-06-01 stsp free(commit_id_str);
6874 234035bc 2019-06-01 stsp if (head_ref)
6875 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6876 234035bc 2019-06-01 stsp if (worktree)
6877 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6879 234035bc 2019-06-01 stsp got_repo_close(repo);
6880 c4296144 2019-05-09 stsp return error;
6883 5ef14e63 2019-06-02 stsp __dead static void
6884 5ef14e63 2019-06-02 stsp usage_backout(void)
6886 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6890 5ef14e63 2019-06-02 stsp static const struct got_error *
6891 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6893 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6894 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6895 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6896 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6897 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6898 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6899 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6900 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6902 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6904 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6905 5ef14e63 2019-06-02 stsp switch (ch) {
6907 5ef14e63 2019-06-02 stsp usage_backout();
6908 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6912 5ef14e63 2019-06-02 stsp argc -= optind;
6913 5ef14e63 2019-06-02 stsp argv += optind;
6915 43012d58 2019-07-14 stsp #ifndef PROFILE
6916 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6917 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6918 43012d58 2019-07-14 stsp err(1, "pledge");
6920 5ef14e63 2019-06-02 stsp if (argc != 1)
6921 5ef14e63 2019-06-02 stsp usage_backout();
6923 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6924 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6925 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6926 5ef14e63 2019-06-02 stsp goto done;
6928 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6929 fa51e947 2020-03-27 stsp if (error) {
6930 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6931 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6932 5ef14e63 2019-06-02 stsp goto done;
6935 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6937 5ef14e63 2019-06-02 stsp if (error != NULL)
6938 5ef14e63 2019-06-02 stsp goto done;
6940 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6941 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6942 5ef14e63 2019-06-02 stsp if (error)
6943 5ef14e63 2019-06-02 stsp goto done;
6945 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6946 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6947 5ef14e63 2019-06-02 stsp if (error != NULL) {
6948 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6949 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6950 5ef14e63 2019-06-02 stsp goto done;
6951 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6952 5ef14e63 2019-06-02 stsp if (error != NULL)
6953 5ef14e63 2019-06-02 stsp goto done;
6954 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6955 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6956 5ef14e63 2019-06-02 stsp if (error != NULL)
6957 5ef14e63 2019-06-02 stsp goto done;
6959 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6960 5ef14e63 2019-06-02 stsp if (error)
6961 5ef14e63 2019-06-02 stsp goto done;
6963 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6964 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6965 5ef14e63 2019-06-02 stsp if (error != NULL)
6966 5ef14e63 2019-06-02 stsp goto done;
6968 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6969 5ef14e63 2019-06-02 stsp if (error)
6970 5ef14e63 2019-06-02 stsp goto done;
6972 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6973 5ef14e63 2019-06-02 stsp if (error)
6974 5ef14e63 2019-06-02 stsp goto done;
6975 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6976 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6977 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6978 5ef14e63 2019-06-02 stsp goto done;
6981 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6982 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6983 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6984 5ef14e63 2019-06-02 stsp if (error != NULL)
6985 5ef14e63 2019-06-02 stsp goto done;
6987 9627c110 2020-04-18 stsp if (upa.did_something)
6988 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6989 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6991 5ef14e63 2019-06-02 stsp if (commit)
6992 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6993 5ef14e63 2019-06-02 stsp free(commit_id_str);
6994 5ef14e63 2019-06-02 stsp if (head_ref)
6995 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6996 818c7501 2019-07-11 stsp if (worktree)
6997 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6999 818c7501 2019-07-11 stsp got_repo_close(repo);
7000 818c7501 2019-07-11 stsp return error;
7003 818c7501 2019-07-11 stsp __dead static void
7004 818c7501 2019-07-11 stsp usage_rebase(void)
7006 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7007 af54c8f8 2019-07-11 stsp getprogname());
7012 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7015 0ebf8283 2019-07-24 stsp size_t len;
7017 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7018 0ebf8283 2019-07-24 stsp if (len > limit)
7019 0ebf8283 2019-07-24 stsp len = limit;
7020 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7021 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7023 0ebf8283 2019-07-24 stsp *nl = '\0';
7026 0ebf8283 2019-07-24 stsp static const struct got_error *
7027 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7029 5943eee2 2019-08-13 stsp const struct got_error *err;
7030 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7031 5943eee2 2019-08-13 stsp const char *s;
7033 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7035 5943eee2 2019-08-13 stsp return err;
7037 5943eee2 2019-08-13 stsp s = logmsg0;
7038 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7041 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7042 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7043 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7044 5943eee2 2019-08-13 stsp goto done;
7047 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7049 5943eee2 2019-08-13 stsp free(logmsg0);
7050 a0ea4fc0 2020-02-28 stsp return err;
7053 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7054 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7056 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7057 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7058 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7060 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7062 a0ea4fc0 2020-02-28 stsp return err;
7064 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7066 a0ea4fc0 2020-02-28 stsp goto done;
7068 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7070 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7072 a0ea4fc0 2020-02-28 stsp goto done;
7074 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7076 a0ea4fc0 2020-02-28 stsp free(id_str);
7077 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7078 a0ea4fc0 2020-02-28 stsp free(logmsg);
7079 5943eee2 2019-08-13 stsp return err;
7082 818c7501 2019-07-11 stsp static const struct got_error *
7083 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7084 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7086 818c7501 2019-07-11 stsp const struct got_error *err;
7087 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7089 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7091 818c7501 2019-07-11 stsp goto done;
7093 ff0d2220 2019-07-11 stsp if (new_id) {
7094 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7096 ff0d2220 2019-07-11 stsp goto done;
7099 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7100 ff0d2220 2019-07-11 stsp if (new_id_str)
7101 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7103 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7105 0ebf8283 2019-07-24 stsp goto done;
7107 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7108 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7110 818c7501 2019-07-11 stsp free(old_id_str);
7111 818c7501 2019-07-11 stsp free(new_id_str);
7112 272a1371 2020-02-28 stsp free(logmsg);
7113 818c7501 2019-07-11 stsp return err;
7116 1ee397ad 2019-07-12 stsp static const struct got_error *
7117 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7118 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7119 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7121 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7122 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7123 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7126 818c7501 2019-07-11 stsp static const struct got_error *
7127 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7128 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7129 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7130 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7132 ff0d2220 2019-07-11 stsp const struct got_error *error;
7133 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7134 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7136 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7137 ff0d2220 2019-07-11 stsp if (error)
7138 ff0d2220 2019-07-11 stsp return error;
7140 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7141 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7142 ff0d2220 2019-07-11 stsp if (error) {
7143 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7144 ff0d2220 2019-07-11 stsp goto done;
7145 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7147 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7148 ff0d2220 2019-07-11 stsp free(new_commit_id);
7151 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7152 ff0d2220 2019-07-11 stsp return error;
7155 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7156 64c6d990 2019-07-11 stsp const char *path_prefix;
7157 64c6d990 2019-07-11 stsp size_t len;
7158 8ca9bd68 2019-07-25 stsp int errcode;
7161 64c6d990 2019-07-11 stsp static const struct got_error *
7162 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7163 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7164 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7165 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7167 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7169 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7170 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7171 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7173 64c6d990 2019-07-11 stsp return NULL;
7176 64c6d990 2019-07-11 stsp static const struct got_error *
7177 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7178 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7179 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7181 64c6d990 2019-07-11 stsp const struct got_error *err;
7182 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7183 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7184 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7186 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7187 64c6d990 2019-07-11 stsp return NULL;
7189 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7191 64c6d990 2019-07-11 stsp goto done;
7193 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7195 64c6d990 2019-07-11 stsp goto done;
7197 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7198 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7200 64c6d990 2019-07-11 stsp goto done;
7202 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7203 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7205 64c6d990 2019-07-11 stsp goto done;
7207 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7208 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7209 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7210 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7211 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7212 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7213 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7215 64c6d990 2019-07-11 stsp if (tree1)
7216 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7217 64c6d990 2019-07-11 stsp if (tree2)
7218 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7219 64c6d990 2019-07-11 stsp if (commit)
7220 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7221 64c6d990 2019-07-11 stsp if (parent_commit)
7222 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7223 64c6d990 2019-07-11 stsp return err;
7226 ff0d2220 2019-07-11 stsp static const struct got_error *
7227 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7228 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7229 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7230 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7231 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7233 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7234 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7235 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7236 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7237 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7239 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7241 af61c510 2019-07-19 stsp return err;
7243 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7244 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7246 af61c510 2019-07-19 stsp goto done;
7247 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7248 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7249 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7250 af61c510 2019-07-19 stsp if (err) {
7251 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7252 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7253 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7254 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7255 af61c510 2019-07-19 stsp "been reached?!?");
7257 ee780d5c 2020-01-04 stsp goto done;
7259 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7260 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7262 af61c510 2019-07-19 stsp goto done;
7264 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7266 af61c510 2019-07-19 stsp goto done;
7267 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7268 af61c510 2019-07-19 stsp commit_id = parent_id;
7272 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7273 af61c510 2019-07-19 stsp return err;
7276 af61c510 2019-07-19 stsp static const struct got_error *
7277 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7279 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7280 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7281 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7282 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7283 818c7501 2019-07-11 stsp char *cwd = NULL;
7284 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7285 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7286 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7287 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7288 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7289 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7290 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7291 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7292 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7293 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7294 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7295 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7296 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7298 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7299 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7301 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7302 818c7501 2019-07-11 stsp switch (ch) {
7304 818c7501 2019-07-11 stsp abort_rebase = 1;
7307 818c7501 2019-07-11 stsp continue_rebase = 1;
7310 818c7501 2019-07-11 stsp usage_rebase();
7311 818c7501 2019-07-11 stsp /* NOTREACHED */
7315 818c7501 2019-07-11 stsp argc -= optind;
7316 818c7501 2019-07-11 stsp argv += optind;
7318 43012d58 2019-07-14 stsp #ifndef PROFILE
7319 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7320 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7321 43012d58 2019-07-14 stsp err(1, "pledge");
7323 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7324 818c7501 2019-07-11 stsp usage_rebase();
7325 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7326 818c7501 2019-07-11 stsp if (argc != 0)
7327 818c7501 2019-07-11 stsp usage_rebase();
7328 818c7501 2019-07-11 stsp } else if (argc != 1)
7329 818c7501 2019-07-11 stsp usage_rebase();
7331 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7332 818c7501 2019-07-11 stsp if (cwd == NULL) {
7333 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7334 818c7501 2019-07-11 stsp goto done;
7336 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7337 fa51e947 2020-03-27 stsp if (error) {
7338 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7339 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7340 818c7501 2019-07-11 stsp goto done;
7343 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7345 818c7501 2019-07-11 stsp if (error != NULL)
7346 818c7501 2019-07-11 stsp goto done;
7348 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7349 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7350 7ef62c4e 2020-02-24 stsp if (error)
7351 7ef62c4e 2020-02-24 stsp goto done;
7353 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7354 7ef62c4e 2020-02-24 stsp worktree);
7355 818c7501 2019-07-11 stsp if (error)
7356 7ef62c4e 2020-02-24 stsp goto done;
7357 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7358 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7359 818c7501 2019-07-11 stsp goto done;
7362 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7363 818c7501 2019-07-11 stsp if (error)
7364 818c7501 2019-07-11 stsp goto done;
7366 f6794adc 2019-07-23 stsp if (abort_rebase) {
7367 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7368 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7369 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7370 f6794adc 2019-07-23 stsp goto done;
7372 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7373 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7374 3e3a69f1 2019-07-25 stsp worktree, repo);
7375 818c7501 2019-07-11 stsp if (error)
7376 818c7501 2019-07-11 stsp goto done;
7377 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7378 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7379 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7380 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7381 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7382 818c7501 2019-07-11 stsp if (error)
7383 818c7501 2019-07-11 stsp goto done;
7384 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7385 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7386 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7389 818c7501 2019-07-11 stsp if (continue_rebase) {
7390 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7391 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7392 f6794adc 2019-07-23 stsp goto done;
7394 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7395 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7396 3e3a69f1 2019-07-25 stsp worktree, repo);
7397 818c7501 2019-07-11 stsp if (error)
7398 818c7501 2019-07-11 stsp goto done;
7400 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7401 01757395 2019-07-12 stsp resume_commit_id, repo);
7402 818c7501 2019-07-11 stsp if (error)
7403 818c7501 2019-07-11 stsp goto done;
7405 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7406 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7407 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7408 818c7501 2019-07-11 stsp goto done;
7411 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7412 818c7501 2019-07-11 stsp if (error != NULL)
7413 818c7501 2019-07-11 stsp goto done;
7416 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7417 ff0d2220 2019-07-11 stsp if (error)
7418 ff0d2220 2019-07-11 stsp goto done;
7420 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7421 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7423 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7424 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7425 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7426 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7427 818c7501 2019-07-11 stsp if (error)
7428 818c7501 2019-07-11 stsp goto done;
7429 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7430 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7431 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7432 818c7501 2019-07-11 stsp "with work tree's branch");
7433 818c7501 2019-07-11 stsp goto done;
7436 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7437 a51a74b3 2019-07-27 stsp if (error) {
7438 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7439 a51a74b3 2019-07-27 stsp goto done;
7440 a51a74b3 2019-07-27 stsp error = NULL;
7442 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7443 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7444 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7445 a51a74b3 2019-07-27 stsp goto done;
7447 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7448 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7449 818c7501 2019-07-11 stsp if (error)
7450 818c7501 2019-07-11 stsp goto done;
7453 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7454 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7455 818c7501 2019-07-11 stsp if (error)
7456 818c7501 2019-07-11 stsp goto done;
7458 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7459 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7460 fc66b545 2019-08-12 stsp if (pid == NULL) {
7461 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7462 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7463 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7464 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7465 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7466 fc66b545 2019-08-12 stsp if (error)
7467 fc66b545 2019-08-12 stsp goto done;
7468 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7469 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7470 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7473 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7474 fc66b545 2019-08-12 stsp goto done;
7476 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7477 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7478 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7479 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7480 818c7501 2019-07-11 stsp commit = NULL;
7481 818c7501 2019-07-11 stsp if (error)
7482 818c7501 2019-07-11 stsp goto done;
7484 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7485 38b0338b 2019-11-29 stsp if (continue_rebase) {
7486 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7487 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7488 38b0338b 2019-11-29 stsp goto done;
7490 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7491 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7492 38b0338b 2019-11-29 stsp char *id_str;
7493 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7494 38b0338b 2019-11-29 stsp new_base_branch);
7495 38b0338b 2019-11-29 stsp if (error)
7496 38b0338b 2019-11-29 stsp goto done;
7497 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7498 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7499 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7500 38b0338b 2019-11-29 stsp free(id_str);
7501 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7502 38b0338b 2019-11-29 stsp new_head_commit_id);
7503 38b0338b 2019-11-29 stsp if (error)
7504 38b0338b 2019-11-29 stsp goto done;
7508 818c7501 2019-07-11 stsp pid = NULL;
7509 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7510 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7512 818c7501 2019-07-11 stsp commit_id = qid->id;
7513 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7514 818c7501 2019-07-11 stsp pid = qid;
7516 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7517 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7518 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7519 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7520 818c7501 2019-07-11 stsp if (error)
7521 818c7501 2019-07-11 stsp goto done;
7523 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7524 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7525 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7527 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7528 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7529 a0ea4fc0 2020-02-28 stsp if (error)
7530 a0ea4fc0 2020-02-28 stsp goto done;
7531 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7535 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7536 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7537 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7538 818c7501 2019-07-11 stsp if (error)
7539 818c7501 2019-07-11 stsp goto done;
7542 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7543 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7544 818c7501 2019-07-11 stsp if (error)
7545 818c7501 2019-07-11 stsp goto done;
7546 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7547 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7549 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7550 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7552 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7553 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7554 818c7501 2019-07-11 stsp free(resume_commit_id);
7555 818c7501 2019-07-11 stsp free(yca_id);
7556 818c7501 2019-07-11 stsp if (commit)
7557 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7558 818c7501 2019-07-11 stsp if (branch)
7559 818c7501 2019-07-11 stsp got_ref_close(branch);
7560 818c7501 2019-07-11 stsp if (new_base_branch)
7561 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7562 818c7501 2019-07-11 stsp if (tmp_branch)
7563 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7564 5ef14e63 2019-06-02 stsp if (worktree)
7565 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7567 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7568 5ef14e63 2019-06-02 stsp return error;
7571 0ebf8283 2019-07-24 stsp __dead static void
7572 0ebf8283 2019-07-24 stsp usage_histedit(void)
7574 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7575 0ebf8283 2019-07-24 stsp getprogname());
7579 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7580 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7581 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7582 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7583 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7585 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7586 0ebf8283 2019-07-24 stsp unsigned char code;
7587 0ebf8283 2019-07-24 stsp const char *name;
7588 0ebf8283 2019-07-24 stsp const char *desc;
7589 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7590 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7591 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7592 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7593 82997472 2020-01-29 stsp "be used" },
7594 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7595 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7596 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7599 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7600 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7601 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7602 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7603 0ebf8283 2019-07-24 stsp char *logmsg;
7605 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7607 0ebf8283 2019-07-24 stsp static const struct got_error *
7608 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7609 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7611 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7612 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7613 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7616 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7618 0ebf8283 2019-07-24 stsp goto done;
7620 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7622 0ebf8283 2019-07-24 stsp goto done;
7624 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7626 0ebf8283 2019-07-24 stsp goto done;
7628 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7629 0ebf8283 2019-07-24 stsp if (n < 0)
7630 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7632 0ebf8283 2019-07-24 stsp if (commit)
7633 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7634 0ebf8283 2019-07-24 stsp free(id_str);
7635 0ebf8283 2019-07-24 stsp free(logmsg);
7636 0ebf8283 2019-07-24 stsp return err;
7639 0ebf8283 2019-07-24 stsp static const struct got_error *
7640 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7641 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7643 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7644 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7646 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7647 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7649 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7650 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7654 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7655 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7656 083957f4 2020-02-24 stsp if (n < 0) {
7657 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7663 0ebf8283 2019-07-24 stsp return err;
7666 0ebf8283 2019-07-24 stsp static const struct got_error *
7667 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7668 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7670 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7672 514f2ffe 2020-01-29 stsp char *id_str;
7673 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7675 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7676 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7678 514f2ffe 2020-01-29 stsp return err;
7680 514f2ffe 2020-01-29 stsp n = fprintf(f,
7681 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7682 514f2ffe 2020-01-29 stsp "# commit %s\n"
7683 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7684 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7685 514f2ffe 2020-01-29 stsp if (n < 0) {
7686 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7687 514f2ffe 2020-01-29 stsp goto done;
7690 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7691 514f2ffe 2020-01-29 stsp if (n < 0) {
7692 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7693 514f2ffe 2020-01-29 stsp goto done;
7696 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7697 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7698 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7699 0ebf8283 2019-07-24 stsp cmd->desc);
7700 0ebf8283 2019-07-24 stsp if (n < 0) {
7701 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7706 514f2ffe 2020-01-29 stsp free(id_str);
7707 0ebf8283 2019-07-24 stsp return err;
7710 0ebf8283 2019-07-24 stsp static const struct got_error *
7711 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7713 0ebf8283 2019-07-24 stsp static char msg[42];
7716 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7718 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7719 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7721 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7724 0ebf8283 2019-07-24 stsp static const struct got_error *
7725 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7726 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7728 0ebf8283 2019-07-24 stsp const struct got_error *err;
7729 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7730 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7732 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7734 0ebf8283 2019-07-24 stsp return err;
7736 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7738 0ebf8283 2019-07-24 stsp goto done;
7740 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7742 5943eee2 2019-08-13 stsp goto done;
7743 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7744 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7745 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7746 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7749 0ebf8283 2019-07-24 stsp if (folded_commit)
7750 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7751 0ebf8283 2019-07-24 stsp free(id_str);
7752 5943eee2 2019-08-13 stsp free(folded_logmsg);
7753 0ebf8283 2019-07-24 stsp return err;
7756 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7757 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7759 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7761 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7762 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7763 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7764 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7765 3f9de99f 2019-07-24 stsp folded = prev;
7766 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7769 0ebf8283 2019-07-24 stsp return folded;
7772 0ebf8283 2019-07-24 stsp static const struct got_error *
7773 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7774 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7776 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7777 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7778 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7779 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7780 1601cb9f 2020-09-11 naddy int logmsg_len;
7782 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7784 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7786 0ebf8283 2019-07-24 stsp return err;
7788 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7789 0ebf8283 2019-07-24 stsp if (folded) {
7790 0ebf8283 2019-07-24 stsp while (folded != hle) {
7791 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7792 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7795 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7796 0ebf8283 2019-07-24 stsp logmsg, repo);
7798 0ebf8283 2019-07-24 stsp goto done;
7799 0ebf8283 2019-07-24 stsp free(logmsg);
7800 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7801 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7805 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7807 0ebf8283 2019-07-24 stsp goto done;
7808 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7810 5943eee2 2019-08-13 stsp goto done;
7811 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7812 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7813 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7814 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7815 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7816 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp free(logmsg);
7819 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7821 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7823 0ebf8283 2019-07-24 stsp goto done;
7825 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7826 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7828 0ebf8283 2019-07-24 stsp goto done;
7830 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7831 0ebf8283 2019-07-24 stsp close(fd);
7833 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7835 0ebf8283 2019-07-24 stsp goto done;
7837 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7838 0ebf8283 2019-07-24 stsp if (err) {
7839 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7844 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7845 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7846 0ebf8283 2019-07-24 stsp free(logmsg_path);
7847 0ebf8283 2019-07-24 stsp free(logmsg);
7848 5943eee2 2019-08-13 stsp free(orig_logmsg);
7849 0ebf8283 2019-07-24 stsp free(editor);
7850 0ebf8283 2019-07-24 stsp if (commit)
7851 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7852 0ebf8283 2019-07-24 stsp return err;
7855 0ebf8283 2019-07-24 stsp static const struct got_error *
7856 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7857 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7859 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7860 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7861 0ebf8283 2019-07-24 stsp size_t size;
7862 0ebf8283 2019-07-24 stsp ssize_t len;
7863 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7864 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7865 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7866 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7868 0ebf8283 2019-07-24 stsp for (;;) {
7869 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7870 0ebf8283 2019-07-24 stsp if (len == -1) {
7871 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7872 0ebf8283 2019-07-24 stsp if (feof(f))
7874 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7875 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7880 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7882 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7883 0ebf8283 2019-07-24 stsp free(line);
7884 0ebf8283 2019-07-24 stsp line = NULL;
7887 0ebf8283 2019-07-24 stsp cmd = NULL;
7888 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7889 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7890 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7891 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7892 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7895 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7900 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7901 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7904 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7906 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7907 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7908 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7911 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7912 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7916 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7917 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7918 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7922 0ebf8283 2019-07-24 stsp free(line);
7923 0ebf8283 2019-07-24 stsp line = NULL;
7927 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7929 0ebf8283 2019-07-24 stsp *end = '\0';
7931 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7932 0ebf8283 2019-07-24 stsp if (err) {
7933 0ebf8283 2019-07-24 stsp /* override error code */
7934 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7938 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7939 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7940 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7943 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7944 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7945 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7946 0ebf8283 2019-07-24 stsp commit_id = NULL;
7947 0ebf8283 2019-07-24 stsp free(line);
7948 0ebf8283 2019-07-24 stsp line = NULL;
7949 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7952 0ebf8283 2019-07-24 stsp free(line);
7953 0ebf8283 2019-07-24 stsp free(commit_id);
7954 0ebf8283 2019-07-24 stsp return err;
7957 0ebf8283 2019-07-24 stsp static const struct got_error *
7958 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7959 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7961 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7962 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7963 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7964 5b87815e 2020-03-05 stsp static char msg[92];
7965 bfce7f83 2019-07-27 stsp char *id_str;
7967 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7968 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7969 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7970 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7971 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7973 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7974 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7975 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7976 5b87815e 2020-03-05 stsp if (hle == hle2)
7978 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7979 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7981 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7983 5b87815e 2020-03-05 stsp return err;
7984 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7985 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7986 5b87815e 2020-03-05 stsp free(id_str);
7987 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7991 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7992 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7993 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7996 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7997 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7999 bfce7f83 2019-07-27 stsp return err;
8000 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8001 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8002 bfce7f83 2019-07-27 stsp free(id_str);
8003 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8007 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8008 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8009 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8010 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8012 bfce7f83 2019-07-27 stsp return NULL;
8015 bfce7f83 2019-07-27 stsp static const struct got_error *
8016 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8017 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8018 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8020 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8021 0ebf8283 2019-07-24 stsp char *editor;
8022 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8024 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8026 0ebf8283 2019-07-24 stsp return err;
8028 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8029 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8030 0ebf8283 2019-07-24 stsp goto done;
8033 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8034 0ebf8283 2019-07-24 stsp if (f == NULL) {
8035 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8036 0ebf8283 2019-07-24 stsp goto done;
8038 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8040 bfce7f83 2019-07-27 stsp goto done;
8042 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8044 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8045 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8046 0ebf8283 2019-07-24 stsp free(editor);
8047 0ebf8283 2019-07-24 stsp return err;
8050 0ebf8283 2019-07-24 stsp static const struct got_error *
8051 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8052 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8053 514f2ffe 2020-01-29 stsp struct got_repository *);
8055 0ebf8283 2019-07-24 stsp static const struct got_error *
8056 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8057 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8058 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8060 0ebf8283 2019-07-24 stsp const struct got_error *err;
8061 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8062 0ebf8283 2019-07-24 stsp char *path = NULL;
8064 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8066 0ebf8283 2019-07-24 stsp return err;
8068 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8070 0ebf8283 2019-07-24 stsp goto done;
8072 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8074 0ebf8283 2019-07-24 stsp goto done;
8076 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8077 083957f4 2020-02-24 stsp rewind(f);
8078 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8080 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8081 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8082 0ebf8283 2019-07-24 stsp goto done;
8085 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8086 083957f4 2020-02-24 stsp if (err) {
8087 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8088 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8089 083957f4 2020-02-24 stsp goto done;
8090 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8091 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8095 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8096 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8097 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8098 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8099 0ebf8283 2019-07-24 stsp free(path);
8100 0ebf8283 2019-07-24 stsp return err;
8103 0ebf8283 2019-07-24 stsp static const struct got_error *
8104 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8105 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8107 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8108 0ebf8283 2019-07-24 stsp char *path = NULL;
8109 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8110 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8111 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8113 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8115 0ebf8283 2019-07-24 stsp return err;
8117 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8118 0ebf8283 2019-07-24 stsp if (f == NULL) {
8119 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8120 0ebf8283 2019-07-24 stsp goto done;
8122 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8123 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8128 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8129 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8130 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8131 0ebf8283 2019-07-24 stsp if (n < 0) {
8132 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8138 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8139 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8140 0ebf8283 2019-07-24 stsp free(path);
8141 0ebf8283 2019-07-24 stsp if (commit)
8142 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8143 0ebf8283 2019-07-24 stsp return err;
8147 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8149 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8151 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8152 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8153 bfce7f83 2019-07-27 stsp free(hle);
8157 0ebf8283 2019-07-24 stsp static const struct got_error *
8158 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8159 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8161 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8162 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8164 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8165 0ebf8283 2019-07-24 stsp if (f == NULL) {
8166 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8167 0ebf8283 2019-07-24 stsp goto done;
8170 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8172 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8174 0ebf8283 2019-07-24 stsp return err;
8177 0ebf8283 2019-07-24 stsp static const struct got_error *
8178 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8179 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8180 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8182 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8183 0ebf8283 2019-07-24 stsp int resp = ' ';
8185 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8186 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8187 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8188 0ebf8283 2019-07-24 stsp resp = getchar();
8189 a61a4414 2019-08-07 stsp if (resp == '\n')
8190 a61a4414 2019-08-07 stsp resp = getchar();
8191 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8192 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8193 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8195 426ebf2e 2019-07-25 stsp if (err) {
8196 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8197 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8199 bfce7f83 2019-07-27 stsp prev_err = err;
8200 426ebf2e 2019-07-25 stsp resp = ' ';
8204 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8205 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8206 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8207 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8208 426ebf2e 2019-07-25 stsp if (err) {
8209 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8210 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8212 bfce7f83 2019-07-27 stsp prev_err = err;
8213 426ebf2e 2019-07-25 stsp resp = ' ';
8217 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8218 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8221 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8224 0ebf8283 2019-07-24 stsp return err;
8227 0ebf8283 2019-07-24 stsp static const struct got_error *
8228 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8229 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8230 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8232 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8233 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8234 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8235 3e3a69f1 2019-07-25 stsp branch, repo);
8238 0ebf8283 2019-07-24 stsp static const struct got_error *
8239 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8240 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8242 0ebf8283 2019-07-24 stsp const struct got_error *err;
8243 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8245 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8247 0ebf8283 2019-07-24 stsp goto done;
8249 0ebf8283 2019-07-24 stsp if (new_id) {
8250 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8252 0ebf8283 2019-07-24 stsp goto done;
8255 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8256 0ebf8283 2019-07-24 stsp if (new_id_str)
8257 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8259 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8260 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8261 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8262 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8263 0ebf8283 2019-07-24 stsp goto done;
8265 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8267 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8269 0ebf8283 2019-07-24 stsp goto done;
8272 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8273 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8274 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8275 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8276 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8278 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8279 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8280 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8287 0ebf8283 2019-07-24 stsp free(old_id_str);
8288 0ebf8283 2019-07-24 stsp free(new_id_str);
8289 0ebf8283 2019-07-24 stsp return err;
8292 0ebf8283 2019-07-24 stsp static const struct got_error *
8293 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8294 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8295 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8296 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8298 0ebf8283 2019-07-24 stsp const struct got_error *err;
8299 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8300 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8302 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8303 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8304 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8306 0ebf8283 2019-07-24 stsp return err;
8309 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8311 0ebf8283 2019-07-24 stsp return err;
8313 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8314 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8315 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8316 0ebf8283 2019-07-24 stsp if (err) {
8317 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8318 0ebf8283 2019-07-24 stsp goto done;
8319 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8321 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8322 0ebf8283 2019-07-24 stsp free(new_commit_id);
8325 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8326 0ebf8283 2019-07-24 stsp return err;
8329 0ebf8283 2019-07-24 stsp static const struct got_error *
8330 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8331 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8333 0ebf8283 2019-07-24 stsp const struct got_error *error;
8334 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8336 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8338 0ebf8283 2019-07-24 stsp if (error)
8339 0ebf8283 2019-07-24 stsp return error;
8341 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8342 0ebf8283 2019-07-24 stsp if (error)
8343 0ebf8283 2019-07-24 stsp return error;
8345 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8346 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8347 0ebf8283 2019-07-24 stsp return error;
8350 ab20a43a 2020-01-29 stsp static const struct got_error *
8351 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8352 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8353 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8354 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8356 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8358 ab20a43a 2020-01-29 stsp switch (status) {
8359 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8360 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8361 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8362 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8363 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8364 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8369 ab20a43a 2020-01-29 stsp switch (staged_status) {
8370 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8371 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8372 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8373 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8374 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8379 ab20a43a 2020-01-29 stsp return NULL;
8382 0ebf8283 2019-07-24 stsp static const struct got_error *
8383 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8385 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8386 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8387 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8388 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8389 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8390 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8391 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8392 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8393 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8394 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8395 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8396 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8397 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8398 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8399 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8400 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8401 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8402 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8403 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8404 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8405 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8406 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8407 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8409 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8410 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8411 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8412 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8414 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8415 0ebf8283 2019-07-24 stsp switch (ch) {
8417 0ebf8283 2019-07-24 stsp abort_edit = 1;
8420 0ebf8283 2019-07-24 stsp continue_edit = 1;
8423 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8426 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8429 0ebf8283 2019-07-24 stsp usage_histedit();
8430 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8434 0ebf8283 2019-07-24 stsp argc -= optind;
8435 0ebf8283 2019-07-24 stsp argv += optind;
8437 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8438 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8439 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8440 0ebf8283 2019-07-24 stsp err(1, "pledge");
8442 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8443 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8444 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8445 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8446 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8447 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8448 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8449 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8450 0ebf8283 2019-07-24 stsp if (argc != 0)
8451 0ebf8283 2019-07-24 stsp usage_histedit();
8454 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8455 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8456 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8457 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8458 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8459 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8462 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8463 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8464 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8465 0ebf8283 2019-07-24 stsp goto done;
8467 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8468 fa51e947 2020-03-27 stsp if (error) {
8469 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8470 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8471 0ebf8283 2019-07-24 stsp goto done;
8474 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8476 0ebf8283 2019-07-24 stsp if (error != NULL)
8477 0ebf8283 2019-07-24 stsp goto done;
8479 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8480 0ebf8283 2019-07-24 stsp if (error)
8481 0ebf8283 2019-07-24 stsp goto done;
8482 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8483 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8484 0ebf8283 2019-07-24 stsp goto done;
8487 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8488 0ebf8283 2019-07-24 stsp if (error)
8489 0ebf8283 2019-07-24 stsp goto done;
8491 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8492 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8493 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8494 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8495 083957f4 2020-02-24 stsp "before the -m option can be used");
8496 083957f4 2020-02-24 stsp goto done;
8499 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8500 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8501 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8502 3e3a69f1 2019-07-25 stsp worktree, repo);
8503 0ebf8283 2019-07-24 stsp if (error)
8504 0ebf8283 2019-07-24 stsp goto done;
8505 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8506 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8507 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8508 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8509 0ebf8283 2019-07-24 stsp if (error)
8510 0ebf8283 2019-07-24 stsp goto done;
8511 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8512 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8513 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8514 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8515 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8516 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8517 0ebf8283 2019-07-24 stsp goto done;
8520 0ebf8283 2019-07-24 stsp if (continue_edit) {
8521 0ebf8283 2019-07-24 stsp char *path;
8523 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8524 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8525 0ebf8283 2019-07-24 stsp goto done;
8528 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8529 0ebf8283 2019-07-24 stsp if (error)
8530 0ebf8283 2019-07-24 stsp goto done;
8532 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8533 0ebf8283 2019-07-24 stsp free(path);
8534 0ebf8283 2019-07-24 stsp if (error)
8535 0ebf8283 2019-07-24 stsp goto done;
8537 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8538 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8539 3e3a69f1 2019-07-25 stsp worktree, repo);
8540 0ebf8283 2019-07-24 stsp if (error)
8541 0ebf8283 2019-07-24 stsp goto done;
8543 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8544 0ebf8283 2019-07-24 stsp if (error)
8545 0ebf8283 2019-07-24 stsp goto done;
8547 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8548 0ebf8283 2019-07-24 stsp head_commit_id);
8549 0ebf8283 2019-07-24 stsp if (error)
8550 0ebf8283 2019-07-24 stsp goto done;
8551 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8552 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8553 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8554 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8555 3aac7cf7 2019-07-25 stsp goto done;
8557 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8558 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8559 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8560 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8561 0ebf8283 2019-07-24 stsp commit = NULL;
8562 0ebf8283 2019-07-24 stsp if (error)
8563 0ebf8283 2019-07-24 stsp goto done;
8565 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8566 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8567 0ebf8283 2019-07-24 stsp goto done;
8570 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8571 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8572 0ebf8283 2019-07-24 stsp if (error != NULL)
8573 0ebf8283 2019-07-24 stsp goto done;
8575 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8576 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8577 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8578 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8579 c7d20a3f 2019-07-30 stsp goto done;
8582 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8583 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8584 bfce7f83 2019-07-27 stsp branch = NULL;
8585 0ebf8283 2019-07-24 stsp if (error)
8586 0ebf8283 2019-07-24 stsp goto done;
8588 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8589 0ebf8283 2019-07-24 stsp head_commit_id);
8590 0ebf8283 2019-07-24 stsp if (error)
8591 0ebf8283 2019-07-24 stsp goto done;
8592 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8593 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8594 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8595 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8596 3aac7cf7 2019-07-25 stsp goto done;
8598 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8599 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8600 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8601 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8602 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8603 0ebf8283 2019-07-24 stsp commit = NULL;
8604 0ebf8283 2019-07-24 stsp if (error)
8605 0ebf8283 2019-07-24 stsp goto done;
8607 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8608 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8609 c5996fff 2020-01-29 stsp goto done;
8612 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8613 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8614 bfce7f83 2019-07-27 stsp if (error)
8615 bfce7f83 2019-07-27 stsp goto done;
8617 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8618 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8619 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8620 6ba2bea2 2019-07-27 stsp if (error) {
8621 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8622 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8623 9627c110 2020-04-18 stsp update_progress, &upa);
8624 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8625 0ebf8283 2019-07-24 stsp goto done;
8628 514f2ffe 2020-01-29 stsp const char *branch_name;
8629 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8630 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8631 514f2ffe 2020-01-29 stsp branch_name += 11;
8632 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8633 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8634 6ba2bea2 2019-07-27 stsp if (error) {
8635 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8636 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8637 9627c110 2020-04-18 stsp update_progress, &upa);
8638 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8639 0ebf8283 2019-07-24 stsp goto done;
8644 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8646 6ba2bea2 2019-07-27 stsp if (error) {
8647 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8648 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8649 9627c110 2020-04-18 stsp update_progress, &upa);
8650 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8651 0ebf8283 2019-07-24 stsp goto done;
8656 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8657 4ec14e60 2019-08-28 hiltjo if (error)
8658 0ebf8283 2019-07-24 stsp goto done;
8660 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8661 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8662 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8663 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8666 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8667 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8668 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8669 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8671 ab20a43a 2020-01-29 stsp if (error)
8672 ab20a43a 2020-01-29 stsp goto done;
8674 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8675 ab20a43a 2020-01-29 stsp int have_changes = 0;
8677 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8678 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8679 ab20a43a 2020-01-29 stsp if (error)
8680 ab20a43a 2020-01-29 stsp goto done;
8681 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8682 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8683 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8684 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8685 ab20a43a 2020-01-29 stsp if (error) {
8686 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8687 ab20a43a 2020-01-29 stsp goto done;
8688 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8689 ab20a43a 2020-01-29 stsp goto done;
8691 ab20a43a 2020-01-29 stsp if (have_changes) {
8692 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8693 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8694 ab20a43a 2020-01-29 stsp if (error)
8695 ab20a43a 2020-01-29 stsp goto done;
8697 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8698 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8699 ab20a43a 2020-01-29 stsp if (error)
8700 ab20a43a 2020-01-29 stsp goto done;
8701 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8702 ab20a43a 2020-01-29 stsp hle, NULL);
8703 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8704 ab20a43a 2020-01-29 stsp commit = NULL;
8705 ab20a43a 2020-01-29 stsp if (error)
8706 ab20a43a 2020-01-29 stsp goto done;
8712 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8713 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8714 0ebf8283 2019-07-24 stsp if (error)
8715 0ebf8283 2019-07-24 stsp goto done;
8719 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8720 0ebf8283 2019-07-24 stsp hle->commit_id);
8721 0ebf8283 2019-07-24 stsp if (error)
8722 0ebf8283 2019-07-24 stsp goto done;
8723 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8724 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8726 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8727 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8728 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8729 0ebf8283 2019-07-24 stsp if (error)
8730 0ebf8283 2019-07-24 stsp goto done;
8731 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8732 0ebf8283 2019-07-24 stsp commit = NULL;
8734 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8735 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8736 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8738 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8739 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8741 a0ea4fc0 2020-02-28 stsp if (error)
8742 a0ea4fc0 2020-02-28 stsp goto done;
8743 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8747 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8748 0ebf8283 2019-07-24 stsp char *id_str;
8749 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8750 0ebf8283 2019-07-24 stsp if (error)
8751 0ebf8283 2019-07-24 stsp goto done;
8752 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8754 0ebf8283 2019-07-24 stsp free(id_str);
8755 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8756 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8757 3e3a69f1 2019-07-25 stsp fileindex);
8758 0ebf8283 2019-07-24 stsp goto done;
8761 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8762 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8763 0ebf8283 2019-07-24 stsp if (error)
8764 0ebf8283 2019-07-24 stsp goto done;
8768 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8769 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8770 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8771 0ebf8283 2019-07-24 stsp if (error)
8772 0ebf8283 2019-07-24 stsp goto done;
8775 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8776 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8777 0ebf8283 2019-07-24 stsp if (error)
8778 0ebf8283 2019-07-24 stsp goto done;
8779 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8780 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8782 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8783 3e3a69f1 2019-07-25 stsp branch, repo);
8785 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8786 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8787 0ebf8283 2019-07-24 stsp free(head_commit_id);
8788 0ebf8283 2019-07-24 stsp free(base_commit_id);
8789 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8790 0ebf8283 2019-07-24 stsp if (commit)
8791 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8792 0ebf8283 2019-07-24 stsp if (branch)
8793 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8794 0ebf8283 2019-07-24 stsp if (tmp_branch)
8795 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8796 0ebf8283 2019-07-24 stsp if (worktree)
8797 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8799 2822a352 2019-10-15 stsp got_repo_close(repo);
8800 2822a352 2019-10-15 stsp return error;
8803 2822a352 2019-10-15 stsp __dead static void
8804 2822a352 2019-10-15 stsp usage_integrate(void)
8806 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8810 2822a352 2019-10-15 stsp static const struct got_error *
8811 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8813 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8814 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8815 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8816 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8817 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8818 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8819 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8820 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8822 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8824 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8825 2822a352 2019-10-15 stsp switch (ch) {
8827 2822a352 2019-10-15 stsp usage_integrate();
8828 2822a352 2019-10-15 stsp /* NOTREACHED */
8832 2822a352 2019-10-15 stsp argc -= optind;
8833 2822a352 2019-10-15 stsp argv += optind;
8835 2822a352 2019-10-15 stsp if (argc != 1)
8836 2822a352 2019-10-15 stsp usage_integrate();
8837 2822a352 2019-10-15 stsp branch_arg = argv[0];
8839 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8840 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8841 2822a352 2019-10-15 stsp err(1, "pledge");
8843 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8844 2822a352 2019-10-15 stsp if (cwd == NULL) {
8845 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8846 2822a352 2019-10-15 stsp goto done;
8849 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8850 fa51e947 2020-03-27 stsp if (error) {
8851 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8852 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8854 2822a352 2019-10-15 stsp goto done;
8857 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8858 2822a352 2019-10-15 stsp if (error)
8859 2822a352 2019-10-15 stsp goto done;
8861 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8863 2822a352 2019-10-15 stsp if (error != NULL)
8864 2822a352 2019-10-15 stsp goto done;
8866 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8867 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8868 2822a352 2019-10-15 stsp if (error)
8869 2822a352 2019-10-15 stsp goto done;
8871 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8872 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8873 2822a352 2019-10-15 stsp goto done;
8876 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8877 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8878 2822a352 2019-10-15 stsp if (error)
8879 2822a352 2019-10-15 stsp goto done;
8881 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8882 2822a352 2019-10-15 stsp if (refname == NULL) {
8883 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8884 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8885 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8886 2822a352 2019-10-15 stsp goto done;
8888 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8889 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8890 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8891 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8892 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8893 2822a352 2019-10-15 stsp goto done;
8896 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8897 2822a352 2019-10-15 stsp if (error)
8898 2822a352 2019-10-15 stsp goto done;
8900 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8901 2822a352 2019-10-15 stsp if (error)
8902 2822a352 2019-10-15 stsp goto done;
8904 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8905 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8906 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8907 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8908 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8909 2822a352 2019-10-15 stsp goto done;
8912 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8913 2822a352 2019-10-15 stsp if (error) {
8914 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8915 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8916 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8917 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8918 2822a352 2019-10-15 stsp goto done;
8921 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8922 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8923 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8924 2822a352 2019-10-15 stsp check_cancelled, NULL);
8925 2822a352 2019-10-15 stsp if (error)
8926 2822a352 2019-10-15 stsp goto done;
8928 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8929 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8932 715dc77e 2019-08-03 stsp got_repo_close(repo);
8933 2822a352 2019-10-15 stsp if (worktree)
8934 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8935 2822a352 2019-10-15 stsp free(cwd);
8936 2822a352 2019-10-15 stsp free(base_commit_id);
8937 2822a352 2019-10-15 stsp free(commit_id);
8938 2822a352 2019-10-15 stsp free(refname);
8939 2822a352 2019-10-15 stsp free(base_refname);
8940 715dc77e 2019-08-03 stsp return error;
8943 715dc77e 2019-08-03 stsp __dead static void
8944 715dc77e 2019-08-03 stsp usage_stage(void)
8946 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8947 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
8948 715dc77e 2019-08-03 stsp getprogname());
8952 715dc77e 2019-08-03 stsp static const struct got_error *
8953 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8954 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8955 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8956 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8958 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8959 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8961 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8962 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8963 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8964 408b4ebc 2019-08-03 stsp return NULL;
8966 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8967 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8968 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8970 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8972 408b4ebc 2019-08-03 stsp return err;
8974 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8975 408b4ebc 2019-08-03 stsp free(id_str);
8976 dc424a06 2019-08-07 stsp return NULL;
8979 dc424a06 2019-08-07 stsp static const struct got_error *
8980 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8982 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8983 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8984 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8985 715dc77e 2019-08-03 stsp char *cwd = NULL;
8986 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8987 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8988 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8989 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8990 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8991 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8993 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8995 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8996 715dc77e 2019-08-03 stsp switch (ch) {
8998 408b4ebc 2019-08-03 stsp list_stage = 1;
9001 dc424a06 2019-08-07 stsp pflag = 1;
9004 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9007 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9010 715dc77e 2019-08-03 stsp usage_stage();
9011 715dc77e 2019-08-03 stsp /* NOTREACHED */
9015 715dc77e 2019-08-03 stsp argc -= optind;
9016 715dc77e 2019-08-03 stsp argv += optind;
9018 715dc77e 2019-08-03 stsp #ifndef PROFILE
9019 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9020 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9021 715dc77e 2019-08-03 stsp err(1, "pledge");
9023 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9024 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9025 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9026 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9028 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9029 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9030 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9031 715dc77e 2019-08-03 stsp goto done;
9034 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9035 fa51e947 2020-03-27 stsp if (error) {
9036 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9037 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9038 715dc77e 2019-08-03 stsp goto done;
9041 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9043 715dc77e 2019-08-03 stsp if (error != NULL)
9044 715dc77e 2019-08-03 stsp goto done;
9046 dc424a06 2019-08-07 stsp if (patch_script_path) {
9047 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9048 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9049 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9050 dc424a06 2019-08-07 stsp patch_script_path);
9051 dc424a06 2019-08-07 stsp goto done;
9054 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9055 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9056 715dc77e 2019-08-03 stsp if (error)
9057 715dc77e 2019-08-03 stsp goto done;
9059 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9060 6d022e97 2019-08-04 stsp if (error)
9061 6d022e97 2019-08-04 stsp goto done;
9063 6d022e97 2019-08-04 stsp if (list_stage)
9064 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9065 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9067 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9068 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9069 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9070 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9071 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9072 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9075 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9076 2e1f37b0 2019-08-08 stsp error == NULL)
9077 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9079 ad493afc 2019-08-03 stsp got_repo_close(repo);
9080 ad493afc 2019-08-03 stsp if (worktree)
9081 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9082 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9083 ad493afc 2019-08-03 stsp free((char *)pe->path);
9084 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9085 ad493afc 2019-08-03 stsp free(cwd);
9086 ad493afc 2019-08-03 stsp return error;
9089 ad493afc 2019-08-03 stsp __dead static void
9090 ad493afc 2019-08-03 stsp usage_unstage(void)
9092 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9093 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9094 ad493afc 2019-08-03 stsp getprogname());
9099 ad493afc 2019-08-03 stsp static const struct got_error *
9100 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9102 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9103 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9104 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9105 ad493afc 2019-08-03 stsp char *cwd = NULL;
9106 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9107 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9108 9627c110 2020-04-18 stsp int ch, pflag = 0;
9109 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9110 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9111 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9112 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9114 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9116 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9117 ad493afc 2019-08-03 stsp switch (ch) {
9119 2e1f37b0 2019-08-08 stsp pflag = 1;
9122 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9125 ad493afc 2019-08-03 stsp usage_unstage();
9126 ad493afc 2019-08-03 stsp /* NOTREACHED */
9130 ad493afc 2019-08-03 stsp argc -= optind;
9131 ad493afc 2019-08-03 stsp argv += optind;
9133 ad493afc 2019-08-03 stsp #ifndef PROFILE
9134 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9135 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9136 ad493afc 2019-08-03 stsp err(1, "pledge");
9138 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9139 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9141 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9142 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9143 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9144 ad493afc 2019-08-03 stsp goto done;
9147 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9148 fa51e947 2020-03-27 stsp if (error) {
9149 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9150 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9151 ad493afc 2019-08-03 stsp goto done;
9154 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9156 ad493afc 2019-08-03 stsp if (error != NULL)
9157 ad493afc 2019-08-03 stsp goto done;
9159 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9160 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9161 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9162 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9163 2e1f37b0 2019-08-08 stsp patch_script_path);
9164 2e1f37b0 2019-08-08 stsp goto done;
9168 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9169 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9170 ad493afc 2019-08-03 stsp if (error)
9171 ad493afc 2019-08-03 stsp goto done;
9173 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9174 ad493afc 2019-08-03 stsp if (error)
9175 ad493afc 2019-08-03 stsp goto done;
9177 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9178 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9179 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9180 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9181 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9182 9627c110 2020-04-18 stsp if (!error)
9183 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9185 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9186 2e1f37b0 2019-08-08 stsp error == NULL)
9187 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9189 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9190 715dc77e 2019-08-03 stsp if (worktree)
9191 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9192 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9193 715dc77e 2019-08-03 stsp free((char *)pe->path);
9194 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9195 715dc77e 2019-08-03 stsp free(cwd);
9196 01073a5d 2019-08-22 stsp return error;
9199 01073a5d 2019-08-22 stsp __dead static void
9200 01073a5d 2019-08-22 stsp usage_cat(void)
9202 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9203 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9207 01073a5d 2019-08-22 stsp static const struct got_error *
9208 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9210 01073a5d 2019-08-22 stsp const struct got_error *err;
9211 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9213 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9215 01073a5d 2019-08-22 stsp return err;
9217 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9218 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9219 01073a5d 2019-08-22 stsp return err;
9222 01073a5d 2019-08-22 stsp static const struct got_error *
9223 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9225 01073a5d 2019-08-22 stsp const struct got_error *err;
9226 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9227 56e0773d 2019-11-28 stsp int nentries, i;
9229 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9231 01073a5d 2019-08-22 stsp return err;
9233 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9234 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9235 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9236 01073a5d 2019-08-22 stsp char *id_str;
9237 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9239 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9240 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9243 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9244 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9245 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9246 01073a5d 2019-08-22 stsp free(id_str);
9249 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9250 01073a5d 2019-08-22 stsp return err;
9253 01073a5d 2019-08-22 stsp static const struct got_error *
9254 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9256 01073a5d 2019-08-22 stsp const struct got_error *err;
9257 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9258 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9259 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9260 01073a5d 2019-08-22 stsp char *id_str = NULL;
9261 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9263 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9265 01073a5d 2019-08-22 stsp return err;
9267 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9269 01073a5d 2019-08-22 stsp goto done;
9271 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9272 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9273 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9274 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9275 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9276 01073a5d 2019-08-22 stsp char *pid_str;
9277 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9279 01073a5d 2019-08-22 stsp goto done;
9280 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9281 01073a5d 2019-08-22 stsp free(pid_str);
9283 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9284 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9285 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9287 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9288 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9289 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9291 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9292 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9293 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9295 01073a5d 2019-08-22 stsp free(id_str);
9296 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9297 01073a5d 2019-08-22 stsp return err;
9300 01073a5d 2019-08-22 stsp static const struct got_error *
9301 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9303 01073a5d 2019-08-22 stsp const struct got_error *err;
9304 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9305 01073a5d 2019-08-22 stsp char *id_str = NULL;
9306 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9308 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9310 01073a5d 2019-08-22 stsp return err;
9312 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9314 01073a5d 2019-08-22 stsp goto done;
9316 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9318 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9319 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9320 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9321 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9323 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9324 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9325 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9327 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9328 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9329 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9331 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9332 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9333 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9339 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9340 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9342 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9343 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9344 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9346 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9347 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9348 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9350 01073a5d 2019-08-22 stsp free(id_str);
9351 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9352 01073a5d 2019-08-22 stsp return err;
9355 01073a5d 2019-08-22 stsp static const struct got_error *
9356 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9358 01073a5d 2019-08-22 stsp const struct got_error *error;
9359 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9360 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9361 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9362 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9363 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9364 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9366 01073a5d 2019-08-22 stsp #ifndef PROFILE
9367 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9368 01073a5d 2019-08-22 stsp NULL) == -1)
9369 01073a5d 2019-08-22 stsp err(1, "pledge");
9372 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9373 01073a5d 2019-08-22 stsp switch (ch) {
9375 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9378 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9379 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9380 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9382 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9385 896e9b6f 2019-08-26 stsp force_path = 1;
9388 01073a5d 2019-08-22 stsp usage_cat();
9389 01073a5d 2019-08-22 stsp /* NOTREACHED */
9393 01073a5d 2019-08-22 stsp argc -= optind;
9394 01073a5d 2019-08-22 stsp argv += optind;
9396 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9397 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9398 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9399 01073a5d 2019-08-22 stsp goto done;
9401 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9402 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9403 01073a5d 2019-08-22 stsp goto done;
9404 01073a5d 2019-08-22 stsp if (worktree) {
9405 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9406 01073a5d 2019-08-22 stsp repo_path = strdup(
9407 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9408 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9409 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9410 01073a5d 2019-08-22 stsp goto done;
9415 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9416 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9417 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9418 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9421 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9422 01073a5d 2019-08-22 stsp free(repo_path);
9423 01073a5d 2019-08-22 stsp if (error != NULL)
9424 01073a5d 2019-08-22 stsp goto done;
9426 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9427 896e9b6f 2019-08-26 stsp if (error)
9428 896e9b6f 2019-08-26 stsp goto done;
9430 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9431 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9432 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9433 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9434 01073a5d 2019-08-22 stsp if (error)
9435 01073a5d 2019-08-22 stsp goto done;
9437 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9438 896e9b6f 2019-08-26 stsp if (force_path) {
9439 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9441 896e9b6f 2019-08-26 stsp if (error)
9444 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9445 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9446 896e9b6f 2019-08-26 stsp if (error) {
9447 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9448 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9450 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9451 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9452 896e9b6f 2019-08-26 stsp if (error)
9457 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9458 01073a5d 2019-08-22 stsp if (error)
9461 01073a5d 2019-08-22 stsp switch (obj_type) {
9462 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9463 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9465 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9466 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9468 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9469 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9471 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9472 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9475 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9478 01073a5d 2019-08-22 stsp if (error)
9480 01073a5d 2019-08-22 stsp free(label);
9481 01073a5d 2019-08-22 stsp label = NULL;
9483 01073a5d 2019-08-22 stsp id = NULL;
9486 01073a5d 2019-08-22 stsp free(label);
9488 896e9b6f 2019-08-26 stsp free(commit_id);
9489 01073a5d 2019-08-22 stsp if (worktree)
9490 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9491 01073a5d 2019-08-22 stsp if (repo) {
9492 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9493 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9494 01073a5d 2019-08-22 stsp if (error == NULL)
9495 01073a5d 2019-08-22 stsp error = repo_error;
9497 b2118c49 2020-07-28 stsp return error;
9500 b2118c49 2020-07-28 stsp __dead static void
9501 b2118c49 2020-07-28 stsp usage_info(void)
9503 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9504 b2118c49 2020-07-28 stsp getprogname());
9508 b2118c49 2020-07-28 stsp static const struct got_error *
9509 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9510 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9511 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9513 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9514 b2118c49 2020-07-28 stsp char *id_str = NULL;
9515 b2118c49 2020-07-28 stsp char datebuf[128];
9516 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9517 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9518 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9521 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9522 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9524 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9525 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9526 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9527 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9528 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9531 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9532 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9533 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9534 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9535 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9536 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9537 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9538 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9540 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9542 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9543 b2118c49 2020-07-28 stsp if (tm == NULL)
9544 b2118c49 2020-07-28 stsp return NULL;
9545 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9546 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9547 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9549 b2118c49 2020-07-28 stsp if (blob_id) {
9550 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9552 b2118c49 2020-07-28 stsp return err;
9553 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9554 b2118c49 2020-07-28 stsp free(id_str);
9557 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9558 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9560 b2118c49 2020-07-28 stsp return err;
9561 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9562 b2118c49 2020-07-28 stsp free(id_str);
9565 b2118c49 2020-07-28 stsp if (commit_id) {
9566 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9568 b2118c49 2020-07-28 stsp return err;
9569 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9570 b2118c49 2020-07-28 stsp free(id_str);
9573 b2118c49 2020-07-28 stsp return NULL;
9576 b2118c49 2020-07-28 stsp static const struct got_error *
9577 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9579 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9580 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9581 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9582 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9583 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9584 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9585 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9587 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9589 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9590 b2118c49 2020-07-28 stsp switch (ch) {
9592 b2118c49 2020-07-28 stsp usage_info();
9593 b2118c49 2020-07-28 stsp /* NOTREACHED */
9597 b2118c49 2020-07-28 stsp argc -= optind;
9598 b2118c49 2020-07-28 stsp argv += optind;
9600 b2118c49 2020-07-28 stsp #ifndef PROFILE
9601 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9602 b2118c49 2020-07-28 stsp NULL) == -1)
9603 b2118c49 2020-07-28 stsp err(1, "pledge");
9605 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9606 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9607 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9608 b2118c49 2020-07-28 stsp goto done;
9611 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9612 b2118c49 2020-07-28 stsp if (error) {
9613 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9614 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9615 b2118c49 2020-07-28 stsp goto done;
9618 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9619 b2118c49 2020-07-28 stsp if (error)
9620 b2118c49 2020-07-28 stsp goto done;
9622 b2118c49 2020-07-28 stsp if (argc >= 1) {
9623 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9624 b2118c49 2020-07-28 stsp worktree);
9625 b2118c49 2020-07-28 stsp if (error)
9626 b2118c49 2020-07-28 stsp goto done;
9627 b2118c49 2020-07-28 stsp show_files = 1;
9630 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9631 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9632 b2118c49 2020-07-28 stsp if (error)
9633 b2118c49 2020-07-28 stsp goto done;
9635 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9636 b2118c49 2020-07-28 stsp if (error)
9637 b2118c49 2020-07-28 stsp goto done;
9639 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9640 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9641 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9642 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9643 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9644 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9645 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9646 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9648 b2118c49 2020-07-28 stsp if (show_files) {
9649 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9650 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9651 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9654 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9655 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9657 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9658 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9660 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9661 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9662 b2118c49 2020-07-28 stsp if (error)
9663 b2118c49 2020-07-28 stsp goto done;
9664 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9665 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9666 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9672 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9673 b2118c49 2020-07-28 stsp free((char *)pe->path);
9674 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9675 b2118c49 2020-07-28 stsp free(cwd);
9676 b2118c49 2020-07-28 stsp free(id_str);
9677 b2118c49 2020-07-28 stsp free(uuidstr);
9678 0ebf8283 2019-07-24 stsp return error;