2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
40 53ccebc2 2019-07-30 stsp #include "got_version.h"
41 f42b1b34 2018-03-12 stsp #include "got_error.h"
42 f42b1b34 2018-03-12 stsp #include "got_object.h"
43 5261c201 2018-04-01 stsp #include "got_reference.h"
44 f42b1b34 2018-03-12 stsp #include "got_repository.h"
45 1dd54920 2019-05-11 stsp #include "got_path.h"
46 e6209546 2019-08-22 stsp #include "got_cancel.h"
47 c09a553d 2018-03-12 stsp #include "got_worktree.h"
48 79109fed 2018-03-27 stsp #include "got_diff.h"
49 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
50 404c43c4 2018-06-21 stsp #include "got_blame.h"
51 63219cd2 2019-01-04 stsp #include "got_privsep.h"
52 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
54 5c860e29 2018-03-12 stsp #ifndef nitems
55 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
59 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
62 99437157 2018-11-11 stsp catch_sigint(int signo)
64 99437157 2018-11-11 stsp sigint_received = 1;
68 99437157 2018-11-11 stsp catch_sigpipe(int signo)
70 99437157 2018-11-11 stsp sigpipe_received = 1;
74 8cfb4057 2019-07-09 stsp struct got_cmd {
75 5c860e29 2018-03-12 stsp const char *cmd_name;
76 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
78 97b3a7be 2019-07-09 stsp const char *cmd_alias;
81 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
82 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
83 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
85 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
88 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
89 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
90 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
91 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
92 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
93 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
94 d00136be 2019-03-26 stsp __dead static void usage_add(void);
95 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
96 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
97 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
98 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
99 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
100 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
101 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
102 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
103 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
104 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
105 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
107 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
108 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
110 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
111 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
113 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
114 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
115 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
116 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
117 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
118 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
119 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
120 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
121 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
122 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
123 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
124 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
125 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
126 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
127 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
128 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
129 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
130 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
132 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
133 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
134 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
135 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
136 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
137 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
138 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
139 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
140 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
141 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
142 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
143 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
144 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
145 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
146 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
147 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
148 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
149 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
151 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
152 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
153 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
154 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
155 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
156 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
159 ce5b7c56 2019-07-09 stsp static void
160 ce5b7c56 2019-07-09 stsp list_commands(void)
164 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
165 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
166 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
167 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
169 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
173 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
175 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
176 5c860e29 2018-03-12 stsp unsigned int i;
178 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
180 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
182 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
183 5c860e29 2018-03-12 stsp switch (ch) {
191 ce5b7c56 2019-07-09 stsp usage(hflag);
192 5c860e29 2018-03-12 stsp /* NOTREACHED */
196 5c860e29 2018-03-12 stsp argc -= optind;
197 5c860e29 2018-03-12 stsp argv += optind;
198 1e70621d 2018-03-27 stsp optind = 0;
200 53ccebc2 2019-07-30 stsp if (Vflag) {
201 53ccebc2 2019-07-30 stsp got_version_print_str();
205 5c860e29 2018-03-12 stsp if (argc <= 0)
206 ce5b7c56 2019-07-09 stsp usage(hflag);
208 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
209 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
211 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
212 d7d4f210 2018-03-12 stsp const struct got_error *error;
214 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
216 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
221 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
223 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
224 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
225 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
226 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
227 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
228 70015d7a 2019-11-08 stsp !(sigint_received &&
229 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
237 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
238 ce5b7c56 2019-07-09 stsp list_commands();
242 4ed7e80c 2018-05-20 stsp __dead static void
243 ce5b7c56 2019-07-09 stsp usage(int hflag)
245 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
246 53ccebc2 2019-07-30 stsp getprogname());
248 ce5b7c56 2019-07-09 stsp list_commands();
252 0266afb7 2019-01-04 stsp static const struct got_error *
253 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
255 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
256 e2ba3d07 2019-05-13 stsp const char *editor;
258 8920fa04 2019-08-18 stsp *abspath = NULL;
260 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
261 e2ba3d07 2019-05-13 stsp if (editor == NULL)
262 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
264 0ee7065d 2019-05-13 stsp if (editor) {
265 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
267 0ee7065d 2019-05-13 stsp return err;
270 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
271 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
272 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
273 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
276 e2ba3d07 2019-05-13 stsp return NULL;
279 e2ba3d07 2019-05-13 stsp static const struct got_error *
280 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
281 c530dc23 2019-07-23 stsp const char *worktree_path)
283 163ce85a 2019-05-13 stsp const struct got_error *err;
285 37c06ea4 2019-07-15 stsp #ifdef PROFILE
286 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
287 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
289 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
292 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
295 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
296 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
298 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
299 163ce85a 2019-05-13 stsp if (err != NULL)
300 163ce85a 2019-05-13 stsp return err;
302 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
305 0266afb7 2019-01-04 stsp return NULL;
308 2c7829a4 2019-06-17 stsp __dead static void
309 2c7829a4 2019-06-17 stsp usage_init(void)
311 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
315 2c7829a4 2019-06-17 stsp static const struct got_error *
316 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
318 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
319 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
322 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
323 2c7829a4 2019-06-17 stsp switch (ch) {
325 2c7829a4 2019-06-17 stsp usage_init();
326 2c7829a4 2019-06-17 stsp /* NOTREACHED */
330 2c7829a4 2019-06-17 stsp argc -= optind;
331 2c7829a4 2019-06-17 stsp argv += optind;
333 2c7829a4 2019-06-17 stsp #ifndef PROFILE
334 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
335 2c7829a4 2019-06-17 stsp err(1, "pledge");
337 2c7829a4 2019-06-17 stsp if (argc != 1)
338 bc20e173 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
341 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
342 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
344 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
346 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
347 2c7829a4 2019-06-17 stsp if (error &&
348 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
351 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
355 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
356 2c7829a4 2019-06-17 stsp if (error != NULL)
360 3ce1b845 2019-07-15 stsp free(repo_path);
361 3ce1b845 2019-07-15 stsp return error;
364 3ce1b845 2019-07-15 stsp __dead static void
365 3ce1b845 2019-07-15 stsp usage_import(void)
367 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
373 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
376 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
377 3ce1b845 2019-07-15 stsp int st = -1;
379 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
380 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
381 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
383 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
385 3ce1b845 2019-07-15 stsp goto doneediting;
387 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
388 3ce1b845 2019-07-15 stsp _exit(127);
391 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
392 3ce1b845 2019-07-15 stsp if (errno != EINTR)
395 3ce1b845 2019-07-15 stsp doneediting:
396 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
397 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
398 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
400 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
401 3ce1b845 2019-07-15 stsp errno = EINTR;
405 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
408 3ce1b845 2019-07-15 stsp static const struct got_error *
409 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 3ce1b845 2019-07-15 stsp const char *initial_content)
412 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
413 3ce1b845 2019-07-15 stsp char buf[1024];
414 3ce1b845 2019-07-15 stsp struct stat st, st2;
416 3ce1b845 2019-07-15 stsp int content_changed = 0;
417 3ce1b845 2019-07-15 stsp size_t len;
419 3ce1b845 2019-07-15 stsp *logmsg = NULL;
421 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
422 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
424 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
425 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
427 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
428 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
430 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
434 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
435 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
437 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
440 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
441 3ce1b845 2019-07-15 stsp if (fp == NULL) {
442 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
445 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
446 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
447 3ce1b845 2019-07-15 stsp content_changed = 1;
448 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
450 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
452 3ce1b845 2019-07-15 stsp fclose(fp);
454 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
459 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
460 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
464 3ce1b845 2019-07-15 stsp free(*logmsg);
465 3ce1b845 2019-07-15 stsp *logmsg = NULL;
467 3ce1b845 2019-07-15 stsp return err;
470 3ce1b845 2019-07-15 stsp static const struct got_error *
471 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
474 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
475 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
478 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
479 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
480 3ce1b845 2019-07-15 stsp branch_name) == -1)
481 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
483 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
487 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
490 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
492 3ce1b845 2019-07-15 stsp free(initial_content);
493 3ce1b845 2019-07-15 stsp return err;
496 3ce1b845 2019-07-15 stsp static const struct got_error *
497 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
499 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
500 3ce1b845 2019-07-15 stsp return NULL;
503 3ce1b845 2019-07-15 stsp static const struct got_error *
504 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
506 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
507 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
509 84792843 2019-08-09 stsp *author = NULL;
511 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
512 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
513 c9956ddf 2019-09-08 stsp if (name && email) {
514 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
515 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
516 aba9c984 2019-09-08 stsp return NULL;
519 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
520 84792843 2019-08-09 stsp if (got_author == NULL) {
521 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
522 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
523 c9956ddf 2019-09-08 stsp if (name && email) {
524 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
525 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
526 c9956ddf 2019-09-08 stsp return NULL;
528 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
529 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 aba9c984 2019-09-08 stsp *author = strdup(got_author);
533 aba9c984 2019-09-08 stsp if (*author == NULL)
534 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
537 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
538 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
540 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
541 84792843 2019-08-09 stsp got_author++;
542 aba9c984 2019-09-08 stsp if (*got_author != '<') {
543 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
547 84792843 2019-08-09 stsp got_author++;
548 aba9c984 2019-09-08 stsp if (*got_author != '@') {
549 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
553 84792843 2019-08-09 stsp got_author++;
554 84792843 2019-08-09 stsp if (*got_author != '>')
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 aba9c984 2019-09-08 stsp free(*author);
559 aba9c984 2019-09-08 stsp *author = NULL;
561 aba9c984 2019-09-08 stsp return err;
564 c9956ddf 2019-09-08 stsp static const struct got_error *
565 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
567 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
569 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
570 c9956ddf 2019-09-08 stsp if (homedir) {
571 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
575 c9956ddf 2019-09-08 stsp return NULL;
578 84792843 2019-08-09 stsp static const struct got_error *
579 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
581 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
582 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
585 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
587 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
590 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
591 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
592 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
594 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
596 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
597 3ce1b845 2019-07-15 stsp switch (ch) {
599 3ce1b845 2019-07-15 stsp branch_name = optarg;
602 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
603 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
604 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
609 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
610 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
611 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
617 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
619 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
625 b2b65d18 2019-08-22 stsp usage_import();
626 3ce1b845 2019-07-15 stsp /* NOTREACHED */
630 3ce1b845 2019-07-15 stsp argc -= optind;
631 3ce1b845 2019-07-15 stsp argv += optind;
633 3ce1b845 2019-07-15 stsp #ifndef PROFILE
634 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
636 3ce1b845 2019-07-15 stsp NULL) == -1)
637 3ce1b845 2019-07-15 stsp err(1, "pledge");
639 3ce1b845 2019-07-15 stsp if (argc != 1)
640 3ce1b845 2019-07-15 stsp usage_import();
642 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
643 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
644 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
645 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
647 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
648 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
651 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
655 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
657 aba9c984 2019-09-08 stsp return error;
660 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
661 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
662 e560b7e0 2019-11-28 stsp * an unintended typo.
664 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
665 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
667 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
672 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
673 3ce1b845 2019-07-15 stsp if (error) {
674 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
677 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 3ce1b845 2019-07-15 stsp "import target branch already exists");
682 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
683 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
684 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
687 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
690 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
691 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
693 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
694 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
697 8e158b01 2019-09-22 stsp free(logmsg);
698 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
699 ef293bdd 2019-10-21 stsp path_dir, refname);
700 ef293bdd 2019-10-21 stsp if (error) {
701 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
702 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
703 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
708 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
709 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
710 ef293bdd 2019-10-21 stsp if (logmsg_path)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
715 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
716 ef293bdd 2019-10-21 stsp if (error) {
717 ef293bdd 2019-10-21 stsp if (logmsg_path)
718 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
722 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
730 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
731 ef293bdd 2019-10-21 stsp if (error) {
732 ef293bdd 2019-10-21 stsp if (logmsg_path)
733 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
737 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
738 ef293bdd 2019-10-21 stsp if (error) {
739 ef293bdd 2019-10-21 stsp if (logmsg_path)
740 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
744 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
745 ef293bdd 2019-10-21 stsp if (error) {
746 ef293bdd 2019-10-21 stsp if (logmsg_path)
747 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
751 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
752 3ce1b845 2019-07-15 stsp if (error) {
753 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
759 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
760 3ce1b845 2019-07-15 stsp branch_ref);
761 ef293bdd 2019-10-21 stsp if (error) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
767 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
768 ef293bdd 2019-10-21 stsp if (error) {
769 ef293bdd 2019-10-21 stsp if (logmsg_path)
770 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
775 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
776 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
778 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
779 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
780 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
781 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
783 8e158b01 2019-09-22 stsp free(logmsg);
784 ef293bdd 2019-10-21 stsp free(logmsg_path);
785 2c7829a4 2019-06-17 stsp free(repo_path);
786 3ce1b845 2019-07-15 stsp free(editor);
787 3ce1b845 2019-07-15 stsp free(refname);
788 3ce1b845 2019-07-15 stsp free(new_commit_id);
789 3ce1b845 2019-07-15 stsp free(id_str);
790 aba9c984 2019-09-08 stsp free(author);
791 c9956ddf 2019-09-08 stsp free(gitconfig_path);
792 3ce1b845 2019-07-15 stsp if (branch_ref)
793 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
794 3ce1b845 2019-07-15 stsp if (head_ref)
795 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
796 2c7829a4 2019-06-17 stsp return error;
799 4ed7e80c 2018-05-20 stsp __dead static void
800 c09a553d 2018-03-12 stsp usage_checkout(void)
802 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 7f47418f 2019-12-20 stsp static void
808 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
810 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
811 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
812 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
813 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
814 7f47418f 2019-12-20 stsp getprogname());
817 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
818 7f47418f 2019-12-20 stsp const char *worktree_path;
819 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
822 1ee397ad 2019-07-12 stsp static const struct got_error *
823 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
825 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
827 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
828 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
829 7f47418f 2019-12-20 stsp return NULL;
831 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
832 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
833 1ee397ad 2019-07-12 stsp return NULL;
836 92a684f4 2018-03-12 stsp while (path[0] == '/')
839 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
840 1ee397ad 2019-07-12 stsp return NULL;
843 99437157 2018-11-11 stsp static const struct got_error *
844 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
846 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
847 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
848 99437157 2018-11-11 stsp return NULL;
851 8069f636 2019-01-12 stsp static const struct got_error *
852 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
853 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 3aef623b 2019-10-15 stsp struct got_repository *repo)
856 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
857 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
859 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
862 36a38700 2019-05-10 stsp return err;
864 d5bea539 2019-05-13 stsp if (yca_id == NULL)
865 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
868 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
869 d5bea539 2019-05-13 stsp * and the work tree's base commit.
871 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
873 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
881 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
882 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
883 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
885 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
886 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
888 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
890 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
892 d5bea539 2019-05-13 stsp free(yca_id);
893 d5bea539 2019-05-13 stsp return NULL;
896 a367ff0f 2019-05-14 stsp static const struct got_error *
897 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
898 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
899 a51a74b3 2019-07-27 stsp struct got_repository *repo)
901 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
902 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
903 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
904 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
906 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
911 a51a74b3 2019-07-27 stsp is_same_branch = 1;
914 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
915 a51a74b3 2019-07-27 stsp is_same_branch = 1;
919 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
923 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
929 a367ff0f 2019-05-14 stsp struct got_object_id *id;
930 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
932 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
933 a367ff0f 2019-05-14 stsp err = NULL;
935 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
937 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
938 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
944 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
946 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
947 a367ff0f 2019-05-14 stsp is_same_branch = 1;
954 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
955 a367ff0f 2019-05-14 stsp free(head_commit_id);
956 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
957 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
958 04f57cb3 2019-07-25 stsp return err;
961 04f57cb3 2019-07-25 stsp static const struct got_error *
962 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
963 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
965 04f57cb3 2019-07-25 stsp const struct got_error *err;
966 04f57cb3 2019-07-25 stsp struct got_reference *ref;
967 303e2782 2019-08-09 stsp struct got_tag_object *tag;
969 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
970 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
971 303e2782 2019-08-09 stsp if (err == NULL) {
972 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
973 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
974 303e2782 2019-08-09 stsp if (*commit_id == NULL)
975 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
976 303e2782 2019-08-09 stsp got_object_tag_close(tag);
977 303e2782 2019-08-09 stsp return err;
978 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
979 303e2782 2019-08-09 stsp return err;
981 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
982 04f57cb3 2019-07-25 stsp if (err == NULL) {
983 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
984 04f57cb3 2019-07-25 stsp got_ref_close(ref);
986 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
987 04f57cb3 2019-07-25 stsp return err;
988 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
989 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
991 a367ff0f 2019-05-14 stsp return err;
994 4ed7e80c 2018-05-20 stsp static const struct got_error *
995 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
997 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
998 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
999 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1000 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1001 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1002 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1003 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1004 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1005 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1006 72151b04 2019-05-11 stsp int ch, same_path_prefix;
1007 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1008 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1010 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1012 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1013 0bb8a95e 2018-03-12 stsp switch (ch) {
1015 08573d5b 2019-05-14 stsp branch_name = optarg;
1018 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1019 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1020 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1023 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1026 2deda0b9 2019-03-07 stsp usage_checkout();
1027 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1031 0bb8a95e 2018-03-12 stsp argc -= optind;
1032 0bb8a95e 2018-03-12 stsp argv += optind;
1034 6715a751 2018-03-16 stsp #ifndef PROFILE
1035 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1037 c09a553d 2018-03-12 stsp err(1, "pledge");
1039 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1040 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1041 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1042 76089277 2018-04-01 stsp if (repo_path == NULL)
1043 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1044 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1045 76089277 2018-04-01 stsp if (cwd == NULL) {
1046 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1047 76089277 2018-04-01 stsp goto done;
1049 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1050 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1051 230a42bd 2019-05-11 jcs if (base == NULL) {
1052 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1053 230a42bd 2019-05-11 jcs path_prefix);
1057 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1058 230a42bd 2019-05-11 jcs if (base == NULL) {
1059 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1060 230a42bd 2019-05-11 jcs repo_path);
1064 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1065 c09a553d 2018-03-12 stsp if (dotgit)
1066 c09a553d 2018-03-12 stsp *dotgit = '\0';
1067 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1068 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1069 c09a553d 2018-03-12 stsp free(cwd);
1070 76089277 2018-04-01 stsp goto done;
1072 c09a553d 2018-03-12 stsp free(cwd);
1073 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1074 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1075 76089277 2018-04-01 stsp if (repo_path == NULL) {
1076 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1077 76089277 2018-04-01 stsp goto done;
1079 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1080 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1081 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1082 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1084 b4b3a7dd 2019-07-22 stsp goto done;
1086 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1087 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1088 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1089 b4b3a7dd 2019-07-22 stsp goto done;
1093 c09a553d 2018-03-12 stsp usage_checkout();
1095 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1096 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1098 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1099 c09a553d 2018-03-12 stsp if (error != NULL)
1100 c02c541e 2019-03-29 stsp goto done;
1102 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1103 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1104 c530dc23 2019-07-23 stsp if (error) {
1105 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1106 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1107 c530dc23 2019-07-23 stsp goto done;
1108 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1109 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1110 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1111 c530dc23 2019-07-23 stsp goto done;
1115 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1116 c02c541e 2019-03-29 stsp if (error)
1117 c09a553d 2018-03-12 stsp goto done;
1119 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1120 c09a553d 2018-03-12 stsp if (error != NULL)
1121 c09a553d 2018-03-12 stsp goto done;
1123 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1124 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1125 c09a553d 2018-03-12 stsp goto done;
1127 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1128 c09a553d 2018-03-12 stsp if (error != NULL)
1129 c09a553d 2018-03-12 stsp goto done;
1131 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1132 e5dc7198 2018-12-29 stsp path_prefix);
1133 e5dc7198 2018-12-29 stsp if (error != NULL)
1134 e5dc7198 2018-12-29 stsp goto done;
1135 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1136 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1137 49520a32 2018-12-29 stsp goto done;
1140 8069f636 2019-01-12 stsp if (commit_id_str) {
1141 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1142 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1143 30837e32 2019-07-25 stsp if (error)
1144 8069f636 2019-01-12 stsp goto done;
1145 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1146 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1147 8069f636 2019-01-12 stsp if (error != NULL) {
1148 8069f636 2019-01-12 stsp free(commit_id);
1149 8069f636 2019-01-12 stsp goto done;
1151 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1152 45d344f6 2019-05-14 stsp if (error)
1153 45d344f6 2019-05-14 stsp goto done;
1154 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1155 8069f636 2019-01-12 stsp commit_id);
1156 8069f636 2019-01-12 stsp free(commit_id);
1157 8069f636 2019-01-12 stsp if (error)
1158 8069f636 2019-01-12 stsp goto done;
1161 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1162 f2ea84fa 2019-07-27 stsp if (error)
1163 f2ea84fa 2019-07-27 stsp goto done;
1164 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1165 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1166 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1167 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1168 c09a553d 2018-03-12 stsp if (error != NULL)
1169 c09a553d 2018-03-12 stsp goto done;
1171 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1172 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1173 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1176 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1177 8069f636 2019-01-12 stsp free(commit_id_str);
1178 76089277 2018-04-01 stsp free(repo_path);
1179 507dc3bb 2018-12-29 stsp free(worktree_path);
1180 507dc3bb 2018-12-29 stsp return error;
1183 507dc3bb 2018-12-29 stsp __dead static void
1184 507dc3bb 2018-12-29 stsp usage_update(void)
1186 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1187 507dc3bb 2018-12-29 stsp getprogname());
1191 1ee397ad 2019-07-12 stsp static const struct got_error *
1192 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1194 784955db 2019-01-12 stsp int *did_something = arg;
1196 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1197 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1198 1ee397ad 2019-07-12 stsp return NULL;
1200 1545c615 2019-02-10 stsp *did_something = 1;
1202 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1203 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1204 1ee397ad 2019-07-12 stsp return NULL;
1206 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1208 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1209 1ee397ad 2019-07-12 stsp return NULL;
1212 be7061eb 2018-12-30 stsp static const struct got_error *
1213 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1214 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1215 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1217 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1218 a1fb16d8 2019-05-24 stsp char *base_id_str;
1219 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1221 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1222 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1223 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1224 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1225 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1226 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1227 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1230 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1231 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1232 a1fb16d8 2019-05-24 stsp if (err) {
1233 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1234 a1fb16d8 2019-05-24 stsp return err;
1235 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1237 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1238 a1fb16d8 2019-05-24 stsp return NULL;
1240 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1241 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1242 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1244 a1fb16d8 2019-05-24 stsp return err;
1245 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1246 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1247 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1248 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1249 0ebf8283 2019-07-24 stsp return NULL;
1252 0ebf8283 2019-07-24 stsp static const struct got_error *
1253 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1255 0ebf8283 2019-07-24 stsp const struct got_error *err;
1256 0ebf8283 2019-07-24 stsp int in_progress;
1258 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1260 0ebf8283 2019-07-24 stsp return err;
1261 0ebf8283 2019-07-24 stsp if (in_progress)
1262 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1264 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1266 0ebf8283 2019-07-24 stsp return err;
1267 0ebf8283 2019-07-24 stsp if (in_progress)
1268 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1270 a1fb16d8 2019-05-24 stsp return NULL;
1273 a5edda0a 2019-07-27 stsp static const struct got_error *
1274 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1275 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1277 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1278 a5edda0a 2019-07-27 stsp char *path;
1281 a5edda0a 2019-07-27 stsp if (argc == 0) {
1282 a5edda0a 2019-07-27 stsp path = strdup("");
1283 a5edda0a 2019-07-27 stsp if (path == NULL)
1284 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1285 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1288 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1289 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1292 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1293 a5edda0a 2019-07-27 stsp if (err) {
1294 a5edda0a 2019-07-27 stsp free(path);
1299 a5edda0a 2019-07-27 stsp return err;
1302 a1fb16d8 2019-05-24 stsp static const struct got_error *
1303 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1305 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1306 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1307 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1308 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1309 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1310 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1311 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1312 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1313 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1314 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1315 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1317 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1319 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1320 507dc3bb 2018-12-29 stsp switch (ch) {
1322 024e9686 2019-05-14 stsp branch_name = optarg;
1325 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1326 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1327 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1330 2deda0b9 2019-03-07 stsp usage_update();
1331 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1335 507dc3bb 2018-12-29 stsp argc -= optind;
1336 507dc3bb 2018-12-29 stsp argv += optind;
1338 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1339 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1340 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1341 507dc3bb 2018-12-29 stsp err(1, "pledge");
1343 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1344 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1345 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1346 c4cdcb68 2019-04-03 stsp goto done;
1348 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1349 7d5807f4 2019-07-11 stsp if (error)
1350 7d5807f4 2019-07-11 stsp goto done;
1352 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1353 f2ea84fa 2019-07-27 stsp if (error)
1354 f2ea84fa 2019-07-27 stsp goto done;
1356 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1358 507dc3bb 2018-12-29 stsp if (error != NULL)
1359 507dc3bb 2018-12-29 stsp goto done;
1361 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1362 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1363 087fb88c 2019-08-04 stsp if (error)
1364 087fb88c 2019-08-04 stsp goto done;
1366 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1367 0266afb7 2019-01-04 stsp if (error)
1368 0266afb7 2019-01-04 stsp goto done;
1370 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1371 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1372 024e9686 2019-05-14 stsp if (error != NULL)
1373 024e9686 2019-05-14 stsp goto done;
1374 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1375 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1376 9c4b8182 2019-01-02 stsp if (error != NULL)
1377 9c4b8182 2019-01-02 stsp goto done;
1378 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1379 507dc3bb 2018-12-29 stsp if (error != NULL)
1380 507dc3bb 2018-12-29 stsp goto done;
1382 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1383 04f57cb3 2019-07-25 stsp free(commit_id_str);
1384 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1385 30837e32 2019-07-25 stsp if (error)
1386 a297e751 2019-07-11 stsp goto done;
1387 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1388 a297e751 2019-07-11 stsp if (error)
1389 507dc3bb 2018-12-29 stsp goto done;
1392 a1fb16d8 2019-05-24 stsp if (branch_name) {
1393 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1394 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1395 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1397 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1398 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1399 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1400 024e9686 2019-05-14 stsp goto done;
1402 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1403 024e9686 2019-05-14 stsp if (error)
1404 024e9686 2019-05-14 stsp goto done;
1405 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1407 a367ff0f 2019-05-14 stsp free(head_commit_id);
1408 024e9686 2019-05-14 stsp if (error != NULL)
1409 024e9686 2019-05-14 stsp goto done;
1410 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1411 a367ff0f 2019-05-14 stsp if (error)
1412 a367ff0f 2019-05-14 stsp goto done;
1413 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1414 024e9686 2019-05-14 stsp if (error)
1415 024e9686 2019-05-14 stsp goto done;
1417 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1418 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1419 a367ff0f 2019-05-14 stsp if (error != NULL) {
1420 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1421 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1422 024e9686 2019-05-14 stsp goto done;
1424 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1425 a367ff0f 2019-05-14 stsp if (error)
1426 a367ff0f 2019-05-14 stsp goto done;
1429 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1430 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1431 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1432 507dc3bb 2018-12-29 stsp commit_id);
1433 507dc3bb 2018-12-29 stsp if (error)
1434 507dc3bb 2018-12-29 stsp goto done;
1437 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1438 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1439 507dc3bb 2018-12-29 stsp if (error != NULL)
1440 507dc3bb 2018-12-29 stsp goto done;
1442 784955db 2019-01-12 stsp if (did_something)
1443 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1445 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1447 c09a553d 2018-03-12 stsp free(worktree_path);
1448 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1449 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1450 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1451 507dc3bb 2018-12-29 stsp free(commit_id);
1452 9c4b8182 2019-01-02 stsp free(commit_id_str);
1453 c09a553d 2018-03-12 stsp return error;
1456 f42b1b34 2018-03-12 stsp static const struct got_error *
1457 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1458 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1459 63035f9f 2019-10-06 stsp struct got_repository *repo)
1461 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1462 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1464 44392932 2019-08-25 stsp if (blob_id1) {
1465 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1467 44392932 2019-08-25 stsp goto done;
1470 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1472 44392932 2019-08-25 stsp goto done;
1474 44392932 2019-08-25 stsp while (path[0] == '/')
1476 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1477 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1479 44392932 2019-08-25 stsp if (blob1)
1480 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1481 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1482 44392932 2019-08-25 stsp return err;
1485 44392932 2019-08-25 stsp static const struct got_error *
1486 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1487 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1488 63035f9f 2019-10-06 stsp struct got_repository *repo)
1490 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1491 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1492 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1494 44392932 2019-08-25 stsp if (tree_id1) {
1495 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1497 44392932 2019-08-25 stsp goto done;
1500 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1502 0f2b3dca 2018-12-22 stsp goto done;
1504 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1505 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1506 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1507 44392932 2019-08-25 stsp while (path[0] == '/')
1509 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1510 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1512 79109fed 2018-03-27 stsp if (tree1)
1513 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1514 366e0a5f 2019-10-10 stsp if (tree2)
1515 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1516 44392932 2019-08-25 stsp return err;
1519 44392932 2019-08-25 stsp static const struct got_error *
1520 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1521 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1523 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1524 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1525 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1526 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1527 44392932 2019-08-25 stsp struct got_object_qid *qid;
1529 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1530 44392932 2019-08-25 stsp if (qid != NULL) {
1531 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1534 44392932 2019-08-25 stsp return err;
1537 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1538 44392932 2019-08-25 stsp int obj_type;
1539 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1541 44392932 2019-08-25 stsp goto done;
1542 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1543 44392932 2019-08-25 stsp if (err) {
1544 44392932 2019-08-25 stsp free(obj_id2);
1545 44392932 2019-08-25 stsp goto done;
1547 44392932 2019-08-25 stsp if (pcommit) {
1548 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1549 44392932 2019-08-25 stsp qid->id, path);
1550 44392932 2019-08-25 stsp if (err) {
1551 44392932 2019-08-25 stsp free(obj_id2);
1552 44392932 2019-08-25 stsp goto done;
1554 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1555 44392932 2019-08-25 stsp if (err) {
1556 44392932 2019-08-25 stsp free(obj_id2);
1557 44392932 2019-08-25 stsp goto done;
1560 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1561 44392932 2019-08-25 stsp if (err) {
1562 44392932 2019-08-25 stsp free(obj_id2);
1563 44392932 2019-08-25 stsp goto done;
1565 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1566 44392932 2019-08-25 stsp switch (obj_type) {
1567 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1568 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1571 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1572 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1576 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1579 44392932 2019-08-25 stsp free(obj_id1);
1580 44392932 2019-08-25 stsp free(obj_id2);
1582 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1583 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1585 44392932 2019-08-25 stsp goto done;
1586 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1587 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1589 44392932 2019-08-25 stsp goto done;
1590 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1591 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1595 0f2b3dca 2018-12-22 stsp free(id_str1);
1596 0f2b3dca 2018-12-22 stsp free(id_str2);
1597 44392932 2019-08-25 stsp if (pcommit)
1598 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1599 79109fed 2018-03-27 stsp return err;
1602 4bb494d5 2018-06-16 stsp static char *
1603 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1605 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1606 09867e48 2019-08-13 stsp char *p, *s;
1608 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1609 09867e48 2019-08-13 stsp if (tm == NULL)
1610 09867e48 2019-08-13 stsp return NULL;
1611 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1612 09867e48 2019-08-13 stsp if (s == NULL)
1613 09867e48 2019-08-13 stsp return NULL;
1614 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1616 6c281f94 2018-06-11 stsp *p = '\0';
1620 6841bf13 2019-11-29 kn static const struct got_error *
1621 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1622 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1624 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1625 6841bf13 2019-11-29 kn regmatch_t regmatch;
1626 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1628 6841bf13 2019-11-29 kn *have_match = 0;
1630 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1634 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1638 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1639 6841bf13 2019-11-29 kn *have_match = 1;
1641 6841bf13 2019-11-29 kn free(id_str);
1642 6841bf13 2019-11-29 kn free(logmsg);
1646 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1648 79109fed 2018-03-27 stsp static const struct got_error *
1649 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1650 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1651 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1653 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1654 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1655 6c281f94 2018-06-11 stsp char datebuf[26];
1656 45d799e2 2018-12-23 stsp time_t committer_time;
1657 45d799e2 2018-12-23 stsp const char *author, *committer;
1658 199a4027 2019-02-02 stsp char *refs_str = NULL;
1659 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1661 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1663 199a4027 2019-02-02 stsp const char *name;
1664 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1667 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1668 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1670 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1671 199a4027 2019-02-02 stsp name += 5;
1672 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1674 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1675 e34f9ed6 2019-02-02 stsp name += 6;
1676 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1677 141c2bff 2019-02-04 stsp name += 8;
1678 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1679 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1680 5d844a1e 2019-08-13 stsp if (err) {
1681 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1682 5d844a1e 2019-08-13 stsp return err;
1683 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1684 5d844a1e 2019-08-13 stsp err = NULL;
1685 5d844a1e 2019-08-13 stsp tag = NULL;
1688 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1689 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1691 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1692 a436ad14 2019-08-13 stsp if (cmp != 0)
1694 199a4027 2019-02-02 stsp s = refs_str;
1695 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1696 199a4027 2019-02-02 stsp name) == -1) {
1697 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1699 34ca4898 2019-08-13 stsp return err;
1703 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1705 8bf5b3c9 2018-03-17 stsp return err;
1707 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1708 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1709 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1710 832c249c 2018-06-10 stsp free(id_str);
1711 d3d493d7 2019-02-21 stsp id_str = NULL;
1712 d3d493d7 2019-02-21 stsp free(refs_str);
1713 d3d493d7 2019-02-21 stsp refs_str = NULL;
1714 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1715 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1716 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1717 09867e48 2019-08-13 stsp if (datestr)
1718 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1719 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1720 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1721 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1722 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1723 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1724 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1725 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1726 3fe1abad 2018-06-10 stsp int n = 1;
1727 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1728 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1729 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1731 a0603db2 2018-06-10 stsp return err;
1732 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1733 a0603db2 2018-06-10 stsp free(id_str);
1737 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1739 5943eee2 2019-08-13 stsp return err;
1741 621015ac 2018-07-23 stsp logmsg = logmsg0;
1743 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1745 832c249c 2018-06-10 stsp printf(" %s\n", line);
1746 832c249c 2018-06-10 stsp } while (line);
1747 621015ac 2018-07-23 stsp free(logmsg0);
1749 971751ac 2018-03-27 stsp if (show_patch) {
1750 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1751 971751ac 2018-03-27 stsp if (err == 0)
1752 971751ac 2018-03-27 stsp printf("\n");
1755 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1757 07862c20 2018-09-15 stsp return err;
1760 f42b1b34 2018-03-12 stsp static const struct got_error *
1761 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1762 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1763 463a997a 2019-12-08 kn int diff_context, int limit, int first_parent_traversal,
1764 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1766 f42b1b34 2018-03-12 stsp const struct got_error *err;
1767 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1768 6841bf13 2019-11-29 kn regex_t regex;
1769 6841bf13 2019-11-29 kn int have_match;
1771 6841bf13 2019-11-29 kn if (search_pattern &&
1772 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1773 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1775 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1776 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1778 f42b1b34 2018-03-12 stsp return err;
1779 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1780 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1782 fcc85cad 2018-07-22 stsp goto done;
1784 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1785 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1787 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1790 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1791 372ccdbb 2018-06-10 stsp if (err) {
1792 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1793 9ba79e04 2018-06-11 stsp err = NULL;
1796 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1798 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1799 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1805 b43fbaa0 2018-06-11 stsp if (id == NULL)
1808 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1812 6841bf13 2019-11-29 kn if (search_pattern) {
1813 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
1815 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1818 6841bf13 2019-11-29 kn if (have_match == 0) {
1819 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1824 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1825 44392932 2019-08-25 stsp diff_context, refs);
1826 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1827 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1831 6841bf13 2019-11-29 kn if (search_pattern)
1832 6841bf13 2019-11-29 kn regfree(®ex);
1833 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1834 f42b1b34 2018-03-12 stsp return err;
1837 4ed7e80c 2018-05-20 stsp __dead static void
1838 6f3d1eb0 2018-03-12 stsp usage_log(void)
1840 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1841 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1845 b1ebc001 2019-08-13 stsp static int
1846 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1848 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1849 b1ebc001 2019-08-13 stsp long long n;
1850 b1ebc001 2019-08-13 stsp const char *errstr;
1852 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1853 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1855 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1856 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1861 4ed7e80c 2018-05-20 stsp static const struct got_error *
1862 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1864 f42b1b34 2018-03-12 stsp const struct got_error *error;
1865 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1866 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1867 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1868 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1869 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1870 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1871 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1872 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1873 64a96a6d 2018-04-01 stsp const char *errstr;
1874 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1876 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1878 6715a751 2018-03-16 stsp #ifndef PROFILE
1879 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1882 f42b1b34 2018-03-12 stsp err(1, "pledge");
1885 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1887 6841bf13 2019-11-29 kn while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1888 79109fed 2018-03-27 stsp switch (ch) {
1890 79109fed 2018-03-27 stsp show_patch = 1;
1893 d142fc45 2018-04-01 stsp start_commit = optarg;
1896 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1898 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1899 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1902 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1903 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1904 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1907 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1910 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1911 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1912 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1914 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1917 6841bf13 2019-11-29 kn search_pattern = optarg;
1920 2deda0b9 2019-03-07 stsp usage_log();
1921 79109fed 2018-03-27 stsp /* NOTREACHED */
1925 79109fed 2018-03-27 stsp argc -= optind;
1926 79109fed 2018-03-27 stsp argv += optind;
1928 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1929 dc1edbfa 2019-11-29 kn diff_context = 3;
1930 dc1edbfa 2019-11-29 kn else if (!show_patch)
1931 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1933 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1934 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1935 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1936 04ca23f4 2018-07-16 stsp goto done;
1939 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1940 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1941 cffc0aa4 2019-02-05 stsp goto done;
1942 cffc0aa4 2019-02-05 stsp error = NULL;
1944 e7301579 2019-03-18 stsp if (argc == 0) {
1945 cbd1af7a 2019-03-18 stsp path = strdup("");
1946 e7301579 2019-03-18 stsp if (path == NULL) {
1947 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1948 cbd1af7a 2019-03-18 stsp goto done;
1950 e7301579 2019-03-18 stsp } else if (argc == 1) {
1951 e7301579 2019-03-18 stsp if (worktree) {
1952 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1954 e7301579 2019-03-18 stsp if (error)
1955 e7301579 2019-03-18 stsp goto done;
1957 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1958 e7301579 2019-03-18 stsp if (path == NULL) {
1959 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1960 e7301579 2019-03-18 stsp goto done;
1964 cbd1af7a 2019-03-18 stsp usage_log();
1966 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1967 5486daa2 2019-05-11 stsp repo_path = worktree ?
1968 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1970 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1971 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1972 cffc0aa4 2019-02-05 stsp goto done;
1975 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1976 d7d4f210 2018-03-12 stsp if (error != NULL)
1977 04ca23f4 2018-07-16 stsp goto done;
1979 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1980 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1981 c02c541e 2019-03-29 stsp if (error)
1982 c02c541e 2019-03-29 stsp goto done;
1984 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1985 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1986 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1987 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1988 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1989 3235492e 2018-04-01 stsp if (error != NULL)
1990 3235492e 2018-04-01 stsp return error;
1991 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1992 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1993 3235492e 2018-04-01 stsp if (error != NULL)
1994 3235492e 2018-04-01 stsp return error;
1995 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1997 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1998 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1999 3235492e 2018-04-01 stsp if (error == NULL) {
2000 1caad61b 2019-02-01 stsp int obj_type;
2001 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2002 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2003 d1f2edc9 2018-06-13 stsp if (error != NULL)
2004 1caad61b 2019-02-01 stsp goto done;
2005 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2006 1caad61b 2019-02-01 stsp if (error != NULL)
2007 1caad61b 2019-02-01 stsp goto done;
2008 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2009 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2010 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2011 1caad61b 2019-02-01 stsp if (error != NULL)
2012 1caad61b 2019-02-01 stsp goto done;
2013 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2014 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2015 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2016 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2017 1caad61b 2019-02-01 stsp goto done;
2020 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2021 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2022 1caad61b 2019-02-01 stsp if (id == NULL)
2023 638f9024 2019-05-13 stsp error = got_error_from_errno(
2024 230a42bd 2019-05-11 jcs "got_object_id_dup");
2025 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2026 1caad61b 2019-02-01 stsp if (error)
2027 1caad61b 2019-02-01 stsp goto done;
2028 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2029 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2030 1caad61b 2019-02-01 stsp goto done;
2032 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2033 d1f2edc9 2018-06-13 stsp if (error != NULL)
2034 1caad61b 2019-02-01 stsp goto done;
2036 15a94983 2018-12-23 stsp if (commit == NULL) {
2037 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2038 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2039 d1f2edc9 2018-06-13 stsp if (error != NULL)
2040 d1f2edc9 2018-06-13 stsp return error;
2043 d7d4f210 2018-03-12 stsp if (error != NULL)
2044 04ca23f4 2018-07-16 stsp goto done;
2046 deeabeae 2019-08-27 stsp if (worktree) {
2047 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2049 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2050 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2051 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2052 deeabeae 2019-08-27 stsp goto done;
2054 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2057 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2058 04ca23f4 2018-07-16 stsp if (error != NULL)
2059 04ca23f4 2018-07-16 stsp goto done;
2060 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2061 04ca23f4 2018-07-16 stsp free(path);
2062 04ca23f4 2018-07-16 stsp path = in_repo_path;
2065 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2066 199a4027 2019-02-02 stsp if (error)
2067 199a4027 2019-02-02 stsp goto done;
2069 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2070 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
2072 04ca23f4 2018-07-16 stsp free(path);
2073 04ca23f4 2018-07-16 stsp free(repo_path);
2074 04ca23f4 2018-07-16 stsp free(cwd);
2076 cffc0aa4 2019-02-05 stsp if (worktree)
2077 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2078 ad242220 2018-09-08 stsp if (repo) {
2079 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2080 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2081 ad242220 2018-09-08 stsp if (error == NULL)
2082 ad242220 2018-09-08 stsp error = repo_error;
2084 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2085 8bf5b3c9 2018-03-17 stsp return error;
2088 4ed7e80c 2018-05-20 stsp __dead static void
2089 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2091 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2092 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2096 b72f483a 2019-02-05 stsp struct print_diff_arg {
2097 b72f483a 2019-02-05 stsp struct got_repository *repo;
2098 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2099 b72f483a 2019-02-05 stsp int diff_context;
2100 3fc0c068 2019-02-10 stsp const char *id_str;
2101 3fc0c068 2019-02-10 stsp int header_shown;
2102 98eaaa12 2019-08-03 stsp int diff_staged;
2103 63035f9f 2019-10-06 stsp int ignore_whitespace;
2106 4ed7e80c 2018-05-20 stsp static const struct got_error *
2107 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2108 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2109 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2110 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2112 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2113 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2114 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2115 12463d8b 2019-12-13 stsp int fd = -1;
2116 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2117 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2118 b72f483a 2019-02-05 stsp struct stat sb;
2120 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2121 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2122 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2123 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2124 98eaaa12 2019-08-03 stsp return NULL;
2126 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2127 98eaaa12 2019-08-03 stsp return NULL;
2128 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2129 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2130 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2131 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2132 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2133 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2134 98eaaa12 2019-08-03 stsp return NULL;
2137 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2138 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2139 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2140 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2141 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2144 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2145 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2146 98eaaa12 2019-08-03 stsp switch (staged_status) {
2147 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2148 98eaaa12 2019-08-03 stsp label1 = path;
2149 98eaaa12 2019-08-03 stsp label2 = path;
2151 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2152 98eaaa12 2019-08-03 stsp label2 = path;
2154 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2155 98eaaa12 2019-08-03 stsp label1 = path;
2158 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2160 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2161 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2162 63035f9f 2019-10-06 stsp a->repo, stdout);
2165 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2166 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2167 4ce46740 2019-08-08 stsp char *id_str;
2168 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2171 4ce46740 2019-08-08 stsp goto done;
2172 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2174 4ce46740 2019-08-08 stsp goto done;
2175 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2176 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2177 4ce46740 2019-08-08 stsp free(id_str);
2178 4ce46740 2019-08-08 stsp goto done;
2180 4ce46740 2019-08-08 stsp free(id_str);
2181 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2182 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2184 4ce46740 2019-08-08 stsp goto done;
2187 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2188 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2189 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2190 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2191 2ec1f75b 2019-03-26 stsp goto done;
2194 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2195 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2196 12463d8b 2019-12-13 stsp if (fd == -1) {
2197 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2198 12463d8b 2019-12-13 stsp goto done;
2201 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2202 12463d8b 2019-12-13 stsp if (fd == -1) {
2203 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2204 12463d8b 2019-12-13 stsp goto done;
2207 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2208 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2209 2ec1f75b 2019-03-26 stsp goto done;
2211 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2212 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2213 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2214 2ec1f75b 2019-03-26 stsp goto done;
2218 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2220 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2221 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2223 b72f483a 2019-02-05 stsp if (blob1)
2224 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2225 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2226 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2227 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2228 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2229 b72f483a 2019-02-05 stsp free(abspath);
2230 927df6b7 2019-02-10 stsp return err;
2233 927df6b7 2019-02-10 stsp static const struct got_error *
2234 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2235 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2236 01073a5d 2019-08-22 stsp struct got_repository *repo)
2238 0adb7196 2019-08-11 stsp const struct got_error *err;
2239 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2240 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2242 0adb7196 2019-08-11 stsp *id = NULL;
2243 0adb7196 2019-08-11 stsp *label = NULL;
2245 01073a5d 2019-08-22 stsp if (resolve_tags) {
2246 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2248 01073a5d 2019-08-22 stsp if (err == NULL) {
2249 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2250 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2251 01073a5d 2019-08-22 stsp if (*id == NULL)
2252 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2253 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2254 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2255 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2256 32f0ab81 2019-08-28 hiltjo free(*id);
2257 01073a5d 2019-08-22 stsp *id = NULL;
2259 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2260 01073a5d 2019-08-22 stsp return err;
2261 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2262 01073a5d 2019-08-22 stsp return err;
2265 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2266 0adb7196 2019-08-11 stsp if (err) {
2267 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2268 0adb7196 2019-08-11 stsp return err;
2269 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2270 0adb7196 2019-08-11 stsp if (err != NULL)
2271 0adb7196 2019-08-11 stsp goto done;
2272 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2273 0adb7196 2019-08-11 stsp if (*label == NULL) {
2274 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2275 0adb7196 2019-08-11 stsp goto done;
2277 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2279 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2280 0adb7196 2019-08-11 stsp if (*label == NULL) {
2281 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2282 0adb7196 2019-08-11 stsp goto done;
2287 0adb7196 2019-08-11 stsp got_ref_close(ref);
2288 0adb7196 2019-08-11 stsp return err;
2292 0adb7196 2019-08-11 stsp static const struct got_error *
2293 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2295 b00d56cd 2018-04-01 stsp const struct got_error *error;
2296 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2297 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2298 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2299 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2300 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2301 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2302 15a94983 2018-12-23 stsp int type1, type2;
2303 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2304 c0cc5c62 2018-10-18 stsp const char *errstr;
2305 927df6b7 2019-02-10 stsp char *path = NULL;
2307 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2308 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2309 25eccc22 2019-01-04 stsp NULL) == -1)
2310 b00d56cd 2018-04-01 stsp err(1, "pledge");
2313 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2314 b00d56cd 2018-04-01 stsp switch (ch) {
2316 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2318 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2319 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2322 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2323 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2324 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2326 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2329 98eaaa12 2019-08-03 stsp diff_staged = 1;
2332 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2335 2deda0b9 2019-03-07 stsp usage_diff();
2336 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2340 b00d56cd 2018-04-01 stsp argc -= optind;
2341 b00d56cd 2018-04-01 stsp argv += optind;
2343 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2344 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2345 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2346 b72f483a 2019-02-05 stsp goto done;
2348 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2349 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2350 927df6b7 2019-02-10 stsp goto done;
2351 927df6b7 2019-02-10 stsp if (argc <= 1) {
2352 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2353 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2354 927df6b7 2019-02-10 stsp goto done;
2356 b72f483a 2019-02-05 stsp if (repo_path)
2358 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2359 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2360 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2362 927df6b7 2019-02-10 stsp goto done;
2364 927df6b7 2019-02-10 stsp if (argc == 1) {
2365 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2367 927df6b7 2019-02-10 stsp if (error)
2368 927df6b7 2019-02-10 stsp goto done;
2370 927df6b7 2019-02-10 stsp path = strdup("");
2371 927df6b7 2019-02-10 stsp if (path == NULL) {
2372 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2373 927df6b7 2019-02-10 stsp goto done;
2376 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2377 98eaaa12 2019-08-03 stsp if (diff_staged)
2378 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2379 98eaaa12 2019-08-03 stsp "objects in repository");
2380 15a94983 2018-12-23 stsp id_str1 = argv[0];
2381 15a94983 2018-12-23 stsp id_str2 = argv[1];
2382 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2383 30db809c 2019-06-05 stsp repo_path =
2384 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2385 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2386 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2387 30db809c 2019-06-05 stsp goto done;
2391 b00d56cd 2018-04-01 stsp usage_diff();
2393 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2394 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2395 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2396 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2399 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2400 76089277 2018-04-01 stsp free(repo_path);
2401 b00d56cd 2018-04-01 stsp if (error != NULL)
2402 b00d56cd 2018-04-01 stsp goto done;
2404 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2405 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2406 c02c541e 2019-03-29 stsp if (error)
2407 c02c541e 2019-03-29 stsp goto done;
2409 30db809c 2019-06-05 stsp if (argc <= 1) {
2410 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2411 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2412 b72f483a 2019-02-05 stsp char *id_str;
2414 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2416 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2417 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2418 b72f483a 2019-02-05 stsp if (error)
2419 b72f483a 2019-02-05 stsp goto done;
2420 b72f483a 2019-02-05 stsp arg.repo = repo;
2421 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2422 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2423 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2424 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2425 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2426 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2428 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2429 72ea6654 2019-07-27 stsp if (error)
2430 72ea6654 2019-07-27 stsp goto done;
2432 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2433 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2434 b72f483a 2019-02-05 stsp free(id_str);
2435 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2436 b72f483a 2019-02-05 stsp goto done;
2439 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2441 0adb7196 2019-08-11 stsp if (error)
2442 0adb7196 2019-08-11 stsp goto done;
2444 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2446 0adb7196 2019-08-11 stsp if (error)
2447 0adb7196 2019-08-11 stsp goto done;
2449 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2450 15a94983 2018-12-23 stsp if (error)
2451 15a94983 2018-12-23 stsp goto done;
2453 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2454 15a94983 2018-12-23 stsp if (error)
2455 15a94983 2018-12-23 stsp goto done;
2457 15a94983 2018-12-23 stsp if (type1 != type2) {
2458 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2459 b00d56cd 2018-04-01 stsp goto done;
2462 15a94983 2018-12-23 stsp switch (type1) {
2463 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2464 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2465 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2467 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2468 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2469 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2471 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2472 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2473 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2474 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2477 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2481 5e70831e 2019-05-28 stsp free(label1);
2482 5e70831e 2019-05-28 stsp free(label2);
2483 15a94983 2018-12-23 stsp free(id1);
2484 15a94983 2018-12-23 stsp free(id2);
2485 927df6b7 2019-02-10 stsp free(path);
2486 b72f483a 2019-02-05 stsp if (worktree)
2487 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2488 ad242220 2018-09-08 stsp if (repo) {
2489 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2490 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2491 ad242220 2018-09-08 stsp if (error == NULL)
2492 ad242220 2018-09-08 stsp error = repo_error;
2494 b00d56cd 2018-04-01 stsp return error;
2497 404c43c4 2018-06-21 stsp __dead static void
2498 404c43c4 2018-06-21 stsp usage_blame(void)
2500 9270e621 2019-02-05 stsp fprintf(stderr,
2501 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2502 404c43c4 2018-06-21 stsp getprogname());
2506 28315671 2019-08-14 stsp struct blame_line {
2507 28315671 2019-08-14 stsp int annotated;
2508 28315671 2019-08-14 stsp char *id_str;
2509 82f6abb8 2019-08-14 stsp char *committer;
2510 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2513 28315671 2019-08-14 stsp struct blame_cb_args {
2514 28315671 2019-08-14 stsp struct blame_line *lines;
2515 28315671 2019-08-14 stsp int nlines;
2516 7ef28ff8 2019-08-14 stsp int nlines_prec;
2517 28315671 2019-08-14 stsp int lineno_cur;
2518 28315671 2019-08-14 stsp off_t *line_offsets;
2520 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2523 404c43c4 2018-06-21 stsp static const struct got_error *
2524 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2526 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2527 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2528 28315671 2019-08-14 stsp struct blame_line *bline;
2529 82f6abb8 2019-08-14 stsp char *line = NULL;
2530 28315671 2019-08-14 stsp size_t linesize = 0;
2531 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2532 28315671 2019-08-14 stsp off_t offset;
2533 bcb49d15 2019-08-14 stsp struct tm tm;
2534 bcb49d15 2019-08-14 stsp time_t committer_time;
2536 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2537 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2538 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2540 28315671 2019-08-14 stsp if (sigint_received)
2541 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2543 2839f8b9 2019-08-18 stsp if (lineno == -1)
2544 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2546 28315671 2019-08-14 stsp /* Annotate this line. */
2547 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2548 28315671 2019-08-14 stsp if (bline->annotated)
2549 28315671 2019-08-14 stsp return NULL;
2550 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2552 28315671 2019-08-14 stsp return err;
2554 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2556 82f6abb8 2019-08-14 stsp goto done;
2558 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2559 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2560 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2561 bcb49d15 2019-08-14 stsp goto done;
2564 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2565 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2566 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2567 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2568 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2569 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2570 82f6abb8 2019-08-14 stsp goto done;
2572 28315671 2019-08-14 stsp bline->annotated = 1;
2574 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2575 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2576 28315671 2019-08-14 stsp if (!bline->annotated)
2577 82f6abb8 2019-08-14 stsp goto done;
2579 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2580 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2581 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2582 82f6abb8 2019-08-14 stsp goto done;
2585 28315671 2019-08-14 stsp while (bline->annotated) {
2586 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2587 82f6abb8 2019-08-14 stsp size_t len;
2589 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2590 28315671 2019-08-14 stsp if (ferror(a->f))
2591 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2595 82f6abb8 2019-08-14 stsp committer = bline->committer;
2596 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2597 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2598 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2599 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2601 82f6abb8 2019-08-14 stsp *at = '\0';
2602 82f6abb8 2019-08-14 stsp len = strlen(committer);
2603 82f6abb8 2019-08-14 stsp if (len >= 9)
2604 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2606 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2608 28315671 2019-08-14 stsp *nl = '\0';
2609 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2610 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2612 28315671 2019-08-14 stsp a->lineno_cur++;
2613 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2616 82f6abb8 2019-08-14 stsp if (commit)
2617 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2618 28315671 2019-08-14 stsp free(line);
2619 28315671 2019-08-14 stsp return err;
2622 28315671 2019-08-14 stsp static const struct got_error *
2623 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2625 404c43c4 2018-06-21 stsp const struct got_error *error;
2626 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2627 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2628 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2629 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2630 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2631 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2632 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2633 28315671 2019-08-14 stsp struct blame_cb_args bca;
2634 28315671 2019-08-14 stsp int ch, obj_type, i;
2635 b02560ec 2019-08-19 stsp size_t filesize;
2637 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2639 404c43c4 2018-06-21 stsp #ifndef PROFILE
2640 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2641 36e2fb66 2019-01-04 stsp NULL) == -1)
2642 404c43c4 2018-06-21 stsp err(1, "pledge");
2645 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2646 404c43c4 2018-06-21 stsp switch (ch) {
2648 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2651 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2652 66bea077 2018-08-02 stsp if (repo_path == NULL)
2653 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2655 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2658 2deda0b9 2019-03-07 stsp usage_blame();
2659 404c43c4 2018-06-21 stsp /* NOTREACHED */
2663 404c43c4 2018-06-21 stsp argc -= optind;
2664 404c43c4 2018-06-21 stsp argv += optind;
2666 a39318fd 2018-08-02 stsp if (argc == 1)
2667 404c43c4 2018-06-21 stsp path = argv[0];
2669 404c43c4 2018-06-21 stsp usage_blame();
2671 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2672 66bea077 2018-08-02 stsp if (cwd == NULL) {
2673 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2674 66bea077 2018-08-02 stsp goto done;
2676 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2677 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2678 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2679 66bea077 2018-08-02 stsp goto done;
2681 0c06baac 2019-02-05 stsp error = NULL;
2682 0c06baac 2019-02-05 stsp if (worktree) {
2683 0c06baac 2019-02-05 stsp repo_path =
2684 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2685 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2686 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2687 0c06baac 2019-02-05 stsp if (error)
2688 0c06baac 2019-02-05 stsp goto done;
2690 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2691 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2692 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2693 0c06baac 2019-02-05 stsp goto done;
2698 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2699 c02c541e 2019-03-29 stsp if (error != NULL)
2700 36e2fb66 2019-01-04 stsp goto done;
2702 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2703 c02c541e 2019-03-29 stsp if (error)
2704 404c43c4 2018-06-21 stsp goto done;
2706 0c06baac 2019-02-05 stsp if (worktree) {
2707 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2708 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2709 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2710 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2711 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2712 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2713 6efaaa2d 2019-02-05 stsp path) == -1) {
2714 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2715 0c06baac 2019-02-05 stsp goto done;
2717 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2720 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2722 0c06baac 2019-02-05 stsp if (error)
2723 66bea077 2018-08-02 stsp goto done;
2725 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2726 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2727 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2728 404c43c4 2018-06-21 stsp if (error != NULL)
2729 66bea077 2018-08-02 stsp goto done;
2730 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2731 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2732 404c43c4 2018-06-21 stsp if (error != NULL)
2733 66bea077 2018-08-02 stsp goto done;
2735 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2736 30837e32 2019-07-25 stsp if (error)
2737 66bea077 2018-08-02 stsp goto done;
2740 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2741 28315671 2019-08-14 stsp if (error)
2742 28315671 2019-08-14 stsp goto done;
2743 28315671 2019-08-14 stsp if (obj_id == NULL) {
2744 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2745 28315671 2019-08-14 stsp goto done;
2748 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2749 28315671 2019-08-14 stsp if (error)
2750 28315671 2019-08-14 stsp goto done;
2752 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2753 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2754 28315671 2019-08-14 stsp goto done;
2757 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2758 28315671 2019-08-14 stsp if (error)
2759 28315671 2019-08-14 stsp goto done;
2760 28315671 2019-08-14 stsp bca.f = got_opentemp();
2761 28315671 2019-08-14 stsp if (bca.f == NULL) {
2762 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2763 28315671 2019-08-14 stsp goto done;
2765 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2766 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2767 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2768 28315671 2019-08-14 stsp goto done;
2770 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2771 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2772 b02560ec 2019-08-19 stsp bca.nlines--;
2774 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2775 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2776 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2777 28315671 2019-08-14 stsp goto done;
2779 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2780 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2781 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2782 7ef28ff8 2019-08-14 stsp while (i > 0) {
2784 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2786 82f6abb8 2019-08-14 stsp bca.repo = repo;
2788 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2789 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2790 28315671 2019-08-14 stsp if (error)
2791 28315671 2019-08-14 stsp goto done;
2793 66bea077 2018-08-02 stsp free(in_repo_path);
2794 66bea077 2018-08-02 stsp free(repo_path);
2795 66bea077 2018-08-02 stsp free(cwd);
2796 404c43c4 2018-06-21 stsp free(commit_id);
2797 28315671 2019-08-14 stsp free(obj_id);
2799 28315671 2019-08-14 stsp got_object_blob_close(blob);
2800 0c06baac 2019-02-05 stsp if (worktree)
2801 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2802 ad242220 2018-09-08 stsp if (repo) {
2803 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2804 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2805 ad242220 2018-09-08 stsp if (error == NULL)
2806 ad242220 2018-09-08 stsp error = repo_error;
2808 3affba96 2019-09-22 stsp if (bca.lines) {
2809 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2810 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2811 3affba96 2019-09-22 stsp free(bline->id_str);
2812 3affba96 2019-09-22 stsp free(bline->committer);
2814 3affba96 2019-09-22 stsp free(bca.lines);
2816 28315671 2019-08-14 stsp free(bca.line_offsets);
2817 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2818 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2819 404c43c4 2018-06-21 stsp return error;
2822 5de5890b 2018-10-18 stsp __dead static void
2823 5de5890b 2018-10-18 stsp usage_tree(void)
2825 c1669e2e 2019-01-09 stsp fprintf(stderr,
2826 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2827 5de5890b 2018-10-18 stsp getprogname());
2831 c1669e2e 2019-01-09 stsp static void
2832 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2833 c1669e2e 2019-01-09 stsp const char *root_path)
2835 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2836 848d6979 2019-08-12 stsp const char *modestr = "";
2837 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2839 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2840 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2843 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2844 63c5ca5d 2019-08-24 stsp modestr = "$";
2845 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2846 848d6979 2019-08-12 stsp modestr = "@";
2847 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2848 848d6979 2019-08-12 stsp modestr = "/";
2849 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2850 848d6979 2019-08-12 stsp modestr = "*";
2852 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2853 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2856 5de5890b 2018-10-18 stsp static const struct got_error *
2857 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2858 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2859 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2861 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2862 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2863 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2864 56e0773d 2019-11-28 stsp int nentries, i;
2866 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2868 5de5890b 2018-10-18 stsp goto done;
2870 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2872 5de5890b 2018-10-18 stsp goto done;
2873 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2874 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2875 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2876 5de5890b 2018-10-18 stsp char *id = NULL;
2878 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2881 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2882 5de5890b 2018-10-18 stsp if (show_ids) {
2883 5de5890b 2018-10-18 stsp char *id_str;
2884 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2885 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2887 5de5890b 2018-10-18 stsp goto done;
2888 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2889 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2890 5de5890b 2018-10-18 stsp free(id_str);
2891 5de5890b 2018-10-18 stsp goto done;
2893 5de5890b 2018-10-18 stsp free(id_str);
2895 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2898 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2899 c1669e2e 2019-01-09 stsp char *child_path;
2900 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2901 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2902 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2903 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2904 c1669e2e 2019-01-09 stsp goto done;
2906 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2907 c1669e2e 2019-01-09 stsp root_path, repo);
2908 c1669e2e 2019-01-09 stsp free(child_path);
2910 c1669e2e 2019-01-09 stsp goto done;
2915 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2916 5de5890b 2018-10-18 stsp free(tree_id);
2917 5de5890b 2018-10-18 stsp return err;
2920 5de5890b 2018-10-18 stsp static const struct got_error *
2921 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2923 5de5890b 2018-10-18 stsp const struct got_error *error;
2924 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2925 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2926 7a2c19d6 2019-02-05 stsp const char *path;
2927 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2928 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2929 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2930 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2933 5de5890b 2018-10-18 stsp #ifndef PROFILE
2934 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2935 0f8d269b 2019-01-04 stsp NULL) == -1)
2936 5de5890b 2018-10-18 stsp err(1, "pledge");
2939 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2940 5de5890b 2018-10-18 stsp switch (ch) {
2942 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2945 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2946 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2947 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2949 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2952 5de5890b 2018-10-18 stsp show_ids = 1;
2955 c1669e2e 2019-01-09 stsp recurse = 1;
2958 2deda0b9 2019-03-07 stsp usage_tree();
2959 5de5890b 2018-10-18 stsp /* NOTREACHED */
2963 5de5890b 2018-10-18 stsp argc -= optind;
2964 5de5890b 2018-10-18 stsp argv += optind;
2966 5de5890b 2018-10-18 stsp if (argc == 1)
2967 5de5890b 2018-10-18 stsp path = argv[0];
2968 5de5890b 2018-10-18 stsp else if (argc > 1)
2969 5de5890b 2018-10-18 stsp usage_tree();
2971 7a2c19d6 2019-02-05 stsp path = NULL;
2973 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2974 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2975 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2976 9bf7a39b 2019-02-05 stsp goto done;
2978 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2979 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2980 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2981 7a2c19d6 2019-02-05 stsp goto done;
2983 7a2c19d6 2019-02-05 stsp error = NULL;
2984 7a2c19d6 2019-02-05 stsp if (worktree) {
2985 7a2c19d6 2019-02-05 stsp repo_path =
2986 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2987 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2988 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2989 7a2c19d6 2019-02-05 stsp if (error)
2990 7a2c19d6 2019-02-05 stsp goto done;
2992 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2993 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2994 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2995 7a2c19d6 2019-02-05 stsp goto done;
3000 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3001 5de5890b 2018-10-18 stsp if (error != NULL)
3002 c02c541e 2019-03-29 stsp goto done;
3004 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3005 c02c541e 2019-03-29 stsp if (error)
3006 5de5890b 2018-10-18 stsp goto done;
3008 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3009 9bf7a39b 2019-02-05 stsp if (worktree) {
3010 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3011 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3012 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3013 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3014 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3015 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3016 9bf7a39b 2019-02-05 stsp goto done;
3018 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
3020 9bf7a39b 2019-02-05 stsp if (error)
3021 9bf7a39b 2019-02-05 stsp goto done;
3023 9bf7a39b 2019-02-05 stsp path = "/";
3025 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3026 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3027 9bf7a39b 2019-02-05 stsp if (error != NULL)
3028 9bf7a39b 2019-02-05 stsp goto done;
3031 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3032 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3033 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3034 5de5890b 2018-10-18 stsp if (error != NULL)
3035 5de5890b 2018-10-18 stsp goto done;
3036 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3037 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3038 5de5890b 2018-10-18 stsp if (error != NULL)
3039 5de5890b 2018-10-18 stsp goto done;
3041 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3042 30837e32 2019-07-25 stsp if (error)
3043 5de5890b 2018-10-18 stsp goto done;
3046 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3047 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3049 5de5890b 2018-10-18 stsp free(in_repo_path);
3050 5de5890b 2018-10-18 stsp free(repo_path);
3051 5de5890b 2018-10-18 stsp free(cwd);
3052 5de5890b 2018-10-18 stsp free(commit_id);
3053 7a2c19d6 2019-02-05 stsp if (worktree)
3054 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3055 5de5890b 2018-10-18 stsp if (repo) {
3056 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3057 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3058 5de5890b 2018-10-18 stsp if (error == NULL)
3059 5de5890b 2018-10-18 stsp error = repo_error;
3061 5de5890b 2018-10-18 stsp return error;
3064 6bad629b 2019-02-04 stsp __dead static void
3065 6bad629b 2019-02-04 stsp usage_status(void)
3067 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3071 b72f483a 2019-02-05 stsp static const struct got_error *
3072 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3073 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3074 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3075 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3077 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3078 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3079 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3080 b72f483a 2019-02-05 stsp return NULL;
3083 6bad629b 2019-02-04 stsp static const struct got_error *
3084 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3086 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3087 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3088 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3089 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3090 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3091 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3094 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3096 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3097 6bad629b 2019-02-04 stsp switch (ch) {
3099 2deda0b9 2019-03-07 stsp usage_status();
3100 6bad629b 2019-02-04 stsp /* NOTREACHED */
3104 6bad629b 2019-02-04 stsp argc -= optind;
3105 6bad629b 2019-02-04 stsp argv += optind;
3107 6bad629b 2019-02-04 stsp #ifndef PROFILE
3108 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3109 6bad629b 2019-02-04 stsp NULL) == -1)
3110 6bad629b 2019-02-04 stsp err(1, "pledge");
3112 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3113 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3114 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3115 927df6b7 2019-02-10 stsp goto done;
3118 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3119 927df6b7 2019-02-10 stsp if (error != NULL)
3120 a5edda0a 2019-07-27 stsp goto done;
3122 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3124 6bad629b 2019-02-04 stsp if (error != NULL)
3125 6bad629b 2019-02-04 stsp goto done;
3127 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3128 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3129 087fb88c 2019-08-04 stsp if (error)
3130 087fb88c 2019-08-04 stsp goto done;
3132 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3133 6bad629b 2019-02-04 stsp if (error)
3134 6bad629b 2019-02-04 stsp goto done;
3136 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3137 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3139 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3140 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3141 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3142 927df6b7 2019-02-10 stsp free(cwd);
3143 d0eebce4 2019-03-11 stsp return error;
3146 d0eebce4 2019-03-11 stsp __dead static void
3147 d0eebce4 2019-03-11 stsp usage_ref(void)
3149 d0eebce4 2019-03-11 stsp fprintf(stderr,
3150 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3151 d0eebce4 2019-03-11 stsp getprogname());
3155 d0eebce4 2019-03-11 stsp static const struct got_error *
3156 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3158 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3159 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3160 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3162 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3163 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3165 d0eebce4 2019-03-11 stsp return err;
3167 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3168 d0eebce4 2019-03-11 stsp char *refstr;
3169 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3170 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3171 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3172 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3173 d0eebce4 2019-03-11 stsp free(refstr);
3176 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3177 d0eebce4 2019-03-11 stsp return NULL;
3180 d0eebce4 2019-03-11 stsp static const struct got_error *
3181 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3183 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3184 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3186 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3188 d0eebce4 2019-03-11 stsp return err;
3190 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3191 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3192 d0eebce4 2019-03-11 stsp return err;
3195 d0eebce4 2019-03-11 stsp static const struct got_error *
3196 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3198 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3199 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3200 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3203 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3204 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3205 d1644381 2019-07-14 stsp * an unintended typo.
3207 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3208 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3210 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3212 d83d9d5c 2019-05-13 stsp if (err) {
3213 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3215 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3216 d83d9d5c 2019-05-13 stsp return err;
3217 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3219 d83d9d5c 2019-05-13 stsp return err;
3220 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3221 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3223 d83d9d5c 2019-05-13 stsp return err;
3226 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3228 d0eebce4 2019-03-11 stsp goto done;
3230 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3233 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3235 d1c1ae5f 2019-08-12 stsp return err;
3238 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3239 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3241 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3242 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3243 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3246 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3247 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3248 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3250 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3251 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3253 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3255 d1c1ae5f 2019-08-12 stsp return err;
3257 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3259 d1c1ae5f 2019-08-12 stsp goto done;
3261 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3263 d1c1ae5f 2019-08-12 stsp if (target_ref)
3264 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3266 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3267 d0eebce4 2019-03-11 stsp return err;
3270 d0eebce4 2019-03-11 stsp static const struct got_error *
3271 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3273 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3274 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3275 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3276 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3277 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3278 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3280 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3281 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3282 d0eebce4 2019-03-11 stsp switch (ch) {
3284 d0eebce4 2019-03-11 stsp delref = optarg;
3287 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3288 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3289 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3291 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3294 d0eebce4 2019-03-11 stsp do_list = 1;
3297 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3300 d0eebce4 2019-03-11 stsp usage_ref();
3301 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3305 d0eebce4 2019-03-11 stsp if (do_list && delref)
3306 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3308 d0eebce4 2019-03-11 stsp argc -= optind;
3309 d0eebce4 2019-03-11 stsp argv += optind;
3311 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3312 d1c1ae5f 2019-08-12 stsp if (create_symref)
3313 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3314 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3315 d0eebce4 2019-03-11 stsp if (argc > 0)
3316 d0eebce4 2019-03-11 stsp usage_ref();
3317 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3318 d0eebce4 2019-03-11 stsp usage_ref();
3320 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3321 e0b57350 2019-03-12 stsp if (do_list) {
3322 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3323 e0b57350 2019-03-12 stsp NULL) == -1)
3324 e0b57350 2019-03-12 stsp err(1, "pledge");
3326 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3327 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3328 e0b57350 2019-03-12 stsp err(1, "pledge");
3331 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3332 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3333 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3334 d0eebce4 2019-03-11 stsp goto done;
3337 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3338 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3339 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3340 d0eebce4 2019-03-11 stsp goto done;
3342 d0eebce4 2019-03-11 stsp error = NULL;
3343 d0eebce4 2019-03-11 stsp if (worktree) {
3344 d0eebce4 2019-03-11 stsp repo_path =
3345 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3346 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3347 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3348 d0eebce4 2019-03-11 stsp if (error)
3349 d0eebce4 2019-03-11 stsp goto done;
3351 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3352 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3353 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3354 d0eebce4 2019-03-11 stsp goto done;
3359 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3360 d0eebce4 2019-03-11 stsp if (error != NULL)
3361 d0eebce4 2019-03-11 stsp goto done;
3363 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3364 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3365 c02c541e 2019-03-29 stsp if (error)
3366 c02c541e 2019-03-29 stsp goto done;
3368 d0eebce4 2019-03-11 stsp if (do_list)
3369 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3370 d0eebce4 2019-03-11 stsp else if (delref)
3371 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3372 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3373 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3375 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3378 4e759de4 2019-06-26 stsp got_repo_close(repo);
3379 4e759de4 2019-06-26 stsp if (worktree)
3380 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3381 4e759de4 2019-06-26 stsp free(cwd);
3382 4e759de4 2019-06-26 stsp free(repo_path);
3383 4e759de4 2019-06-26 stsp return error;
3386 4e759de4 2019-06-26 stsp __dead static void
3387 4e759de4 2019-06-26 stsp usage_branch(void)
3389 4e759de4 2019-06-26 stsp fprintf(stderr,
3390 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3391 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3395 4e759de4 2019-06-26 stsp static const struct got_error *
3396 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3397 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3399 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3400 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3401 4e99b47e 2019-10-04 stsp char *refstr;
3403 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3404 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3405 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3406 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3408 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3410 4e99b47e 2019-10-04 stsp return err;
3411 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3412 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3413 4e99b47e 2019-10-04 stsp marker = "* ";
3415 4e99b47e 2019-10-04 stsp marker = "~ ";
3419 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3420 4e99b47e 2019-10-04 stsp refname += 11;
3421 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3422 4e99b47e 2019-10-04 stsp refname += 18;
3424 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3425 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3426 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3428 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3429 4e99b47e 2019-10-04 stsp free(refstr);
3430 4e99b47e 2019-10-04 stsp return NULL;
3433 4e99b47e 2019-10-04 stsp static const struct got_error *
3434 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3436 ad89fa31 2019-10-04 stsp const char *refname;
3438 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3439 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3441 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3443 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3444 ad89fa31 2019-10-04 stsp refname += 11;
3445 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3446 ad89fa31 2019-10-04 stsp refname += 18;
3448 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3450 ad89fa31 2019-10-04 stsp return NULL;
3453 ad89fa31 2019-10-04 stsp static const struct got_error *
3454 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3456 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3457 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3458 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3459 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3460 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3462 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3464 4e99b47e 2019-10-04 stsp if (worktree) {
3465 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3466 4e99b47e 2019-10-04 stsp worktree);
3468 4e99b47e 2019-10-04 stsp return err;
3470 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3471 4e99b47e 2019-10-04 stsp worktree);
3473 4e99b47e 2019-10-04 stsp return err;
3475 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3476 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3477 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3479 4e99b47e 2019-10-04 stsp return err;
3480 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3481 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3485 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3486 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3488 4e759de4 2019-06-26 stsp return err;
3490 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3491 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3493 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3494 4e759de4 2019-06-26 stsp return NULL;
3497 4e759de4 2019-06-26 stsp static const struct got_error *
3498 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3499 45cd4e47 2019-08-25 stsp const char *branch_name)
3501 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3502 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3503 4e759de4 2019-06-26 stsp char *refname;
3505 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3506 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3508 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3510 4e759de4 2019-06-26 stsp goto done;
3512 45cd4e47 2019-08-25 stsp if (worktree &&
3513 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3514 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3515 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3516 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3517 45cd4e47 2019-08-25 stsp goto done;
3520 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3523 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3524 4e759de4 2019-06-26 stsp free(refname);
3525 4e759de4 2019-06-26 stsp return err;
3528 4e759de4 2019-06-26 stsp static const struct got_error *
3529 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3530 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3532 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3533 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3534 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3537 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3538 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3539 d3f84d51 2019-07-11 stsp * an unintended typo.
3541 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3542 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3544 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3545 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3546 4e759de4 2019-06-26 stsp goto done;
3549 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3550 4e759de4 2019-06-26 stsp if (err == NULL) {
3551 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3552 4e759de4 2019-06-26 stsp goto done;
3553 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3554 4e759de4 2019-06-26 stsp goto done;
3556 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3558 4e759de4 2019-06-26 stsp goto done;
3560 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3563 4e759de4 2019-06-26 stsp got_ref_close(ref);
3564 4e759de4 2019-06-26 stsp free(base_refname);
3565 4e759de4 2019-06-26 stsp free(refname);
3566 4e759de4 2019-06-26 stsp return err;
3569 4e759de4 2019-06-26 stsp static const struct got_error *
3570 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3572 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3573 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3574 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3575 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3576 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3577 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3579 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3580 4e759de4 2019-06-26 stsp switch (ch) {
3582 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3585 4e759de4 2019-06-26 stsp delref = optarg;
3588 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3589 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3590 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3592 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3595 4e759de4 2019-06-26 stsp do_list = 1;
3598 4e759de4 2019-06-26 stsp usage_branch();
3599 4e759de4 2019-06-26 stsp /* NOTREACHED */
3603 4e759de4 2019-06-26 stsp if (do_list && delref)
3604 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3606 4e759de4 2019-06-26 stsp argc -= optind;
3607 4e759de4 2019-06-26 stsp argv += optind;
3609 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3610 ad89fa31 2019-10-04 stsp do_show = 1;
3612 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3613 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3615 4e759de4 2019-06-26 stsp if (do_list || delref) {
3616 4e759de4 2019-06-26 stsp if (argc > 0)
3617 4e759de4 2019-06-26 stsp usage_branch();
3618 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3619 4e759de4 2019-06-26 stsp usage_branch();
3621 4e759de4 2019-06-26 stsp #ifndef PROFILE
3622 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3623 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3624 4e759de4 2019-06-26 stsp NULL) == -1)
3625 4e759de4 2019-06-26 stsp err(1, "pledge");
3627 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3628 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3629 4e759de4 2019-06-26 stsp err(1, "pledge");
3632 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3633 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3634 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3635 4e759de4 2019-06-26 stsp goto done;
3638 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3639 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3640 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3641 4e759de4 2019-06-26 stsp goto done;
3643 4e759de4 2019-06-26 stsp error = NULL;
3644 4e759de4 2019-06-26 stsp if (worktree) {
3645 4e759de4 2019-06-26 stsp repo_path =
3646 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3647 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3648 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3649 4e759de4 2019-06-26 stsp if (error)
3650 4e759de4 2019-06-26 stsp goto done;
3652 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3653 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3654 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3655 4e759de4 2019-06-26 stsp goto done;
3660 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3661 4e759de4 2019-06-26 stsp if (error != NULL)
3662 4e759de4 2019-06-26 stsp goto done;
3664 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3665 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3666 4e759de4 2019-06-26 stsp if (error)
3667 4e759de4 2019-06-26 stsp goto done;
3669 ad89fa31 2019-10-04 stsp if (do_show)
3670 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3671 ad89fa31 2019-10-04 stsp else if (do_list)
3672 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3673 4e759de4 2019-06-26 stsp else if (delref)
3674 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3676 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3677 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3678 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3679 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3680 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3681 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3682 a74f7e83 2019-11-10 stsp if (error)
3683 a74f7e83 2019-11-10 stsp goto done;
3684 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3685 a74f7e83 2019-11-10 stsp free(commit_id);
3689 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3690 d0eebce4 2019-03-11 stsp if (worktree)
3691 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3692 d0eebce4 2019-03-11 stsp free(cwd);
3693 d0eebce4 2019-03-11 stsp free(repo_path);
3694 d00136be 2019-03-26 stsp return error;
3698 d00136be 2019-03-26 stsp __dead static void
3699 8e7bd50a 2019-08-22 stsp usage_tag(void)
3701 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3702 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3703 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3708 b8bad2ba 2019-08-23 stsp static const struct got_error *
3709 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3711 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3712 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3713 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3714 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3715 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3717 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3718 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3719 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3720 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3722 b8bad2ba 2019-08-23 stsp return err;
3723 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3726 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3729 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3730 b8bad2ba 2019-08-23 stsp free(re_id);
3733 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3734 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3737 b8bad2ba 2019-08-23 stsp while (se) {
3738 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3741 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3742 b8bad2ba 2019-08-23 stsp free(se_id);
3745 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3746 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3748 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3749 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3751 b8bad2ba 2019-08-23 stsp return err;
3752 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3755 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3760 b8bad2ba 2019-08-23 stsp return err;
3764 8e7bd50a 2019-08-22 stsp static const struct got_error *
3765 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3766 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3768 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3769 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3770 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3771 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3772 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3776 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3778 b8bad2ba 2019-08-23 stsp return err;
3779 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3781 b8bad2ba 2019-08-23 stsp goto done;
3783 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3785 b8bad2ba 2019-08-23 stsp goto done;
3786 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3788 b8bad2ba 2019-08-23 stsp goto done;
3790 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3791 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3793 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3794 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3796 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3797 b8bad2ba 2019-08-23 stsp *cmp = -1;
3799 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3801 b8bad2ba 2019-08-23 stsp free(id1);
3802 b8bad2ba 2019-08-23 stsp free(id2);
3804 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3806 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3807 b8bad2ba 2019-08-23 stsp return err;
3810 b8bad2ba 2019-08-23 stsp static const struct got_error *
3811 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3813 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3814 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3815 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3817 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3819 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3821 8e7bd50a 2019-08-22 stsp return err;
3823 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3824 8e7bd50a 2019-08-22 stsp const char *refname;
3825 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3826 8e7bd50a 2019-08-22 stsp char datebuf[26];
3827 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3828 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3829 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3831 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3832 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3834 8e7bd50a 2019-08-22 stsp refname += 10;
3835 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3836 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3837 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3840 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3841 8e7bd50a 2019-08-22 stsp free(refstr);
3843 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3846 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3850 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3851 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3852 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3853 2417344c 2019-08-23 stsp if (datestr)
3854 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3855 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3856 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3859 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3860 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3861 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3863 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3864 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3866 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3867 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3869 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3870 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3875 8e7bd50a 2019-08-22 stsp free(id_str);
3876 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3877 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3878 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3879 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3883 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3885 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3887 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3888 8e7bd50a 2019-08-22 stsp } while (line);
3889 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3892 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3893 8e7bd50a 2019-08-22 stsp return NULL;
3896 8e7bd50a 2019-08-22 stsp static const struct got_error *
3897 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3898 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3900 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3901 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3902 f372d5cd 2019-10-21 stsp char *editor = NULL;
3903 8e7bd50a 2019-08-22 stsp int fd = -1;
3905 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3906 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3907 8e7bd50a 2019-08-22 stsp goto done;
3910 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3911 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3912 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3913 8e7bd50a 2019-08-22 stsp goto done;
3916 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3918 8e7bd50a 2019-08-22 stsp goto done;
3920 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3921 8e7bd50a 2019-08-22 stsp close(fd);
3923 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3925 8e7bd50a 2019-08-22 stsp goto done;
3926 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3928 8e7bd50a 2019-08-22 stsp free(initial_content);
3929 8e7bd50a 2019-08-22 stsp free(template);
3930 8e7bd50a 2019-08-22 stsp free(editor);
3932 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3933 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3934 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3935 8e7bd50a 2019-08-22 stsp if (err) {
3936 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3937 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3940 8e7bd50a 2019-08-22 stsp return err;
3943 8e7bd50a 2019-08-22 stsp static const struct got_error *
3944 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3945 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3947 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3948 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3949 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3950 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3951 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3952 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3953 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3956 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3957 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3958 8e7bd50a 2019-08-22 stsp * an unintended typo.
3960 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3961 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3963 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3965 8e7bd50a 2019-08-22 stsp return err;
3967 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3968 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3970 8e7bd50a 2019-08-22 stsp goto done;
3972 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3974 8e7bd50a 2019-08-22 stsp goto done;
3976 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3977 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3978 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3979 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3980 8e7bd50a 2019-08-22 stsp goto done;
3982 8e7bd50a 2019-08-22 stsp tag_name += 10;
3983 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3984 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3985 8e7bd50a 2019-08-22 stsp goto done;
3988 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3989 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3990 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3991 8e7bd50a 2019-08-22 stsp goto done;
3992 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3993 8e7bd50a 2019-08-22 stsp goto done;
3995 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3996 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3997 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3998 f372d5cd 2019-10-21 stsp if (err) {
3999 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4000 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4001 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4002 8e7bd50a 2019-08-22 stsp goto done;
4006 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4007 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4008 f372d5cd 2019-10-21 stsp if (err) {
4009 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4010 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4011 8e7bd50a 2019-08-22 stsp goto done;
4014 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4015 f372d5cd 2019-10-21 stsp if (err) {
4016 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4017 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4018 8e7bd50a 2019-08-22 stsp goto done;
4021 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4022 f372d5cd 2019-10-21 stsp if (err) {
4023 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4024 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4025 f372d5cd 2019-10-21 stsp goto done;
4028 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4029 f372d5cd 2019-10-21 stsp if (err) {
4030 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4031 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4032 f372d5cd 2019-10-21 stsp goto done;
4034 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4036 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4037 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4038 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4039 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4040 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4041 f372d5cd 2019-10-21 stsp free(tag_id_str);
4043 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4044 8e7bd50a 2019-08-22 stsp free(commit_id);
4045 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4046 8e7bd50a 2019-08-22 stsp free(refname);
4047 8e7bd50a 2019-08-22 stsp free(tagmsg);
4048 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4049 aba9c984 2019-09-08 stsp free(tagger);
4050 8e7bd50a 2019-08-22 stsp return err;
4053 8e7bd50a 2019-08-22 stsp static const struct got_error *
4054 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4056 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4057 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4058 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4059 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4060 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4061 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4062 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4064 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4065 8e7bd50a 2019-08-22 stsp switch (ch) {
4067 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4070 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4071 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4072 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4074 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4077 8e7bd50a 2019-08-22 stsp do_list = 1;
4080 8e7bd50a 2019-08-22 stsp usage_tag();
4081 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4085 8e7bd50a 2019-08-22 stsp argc -= optind;
4086 8e7bd50a 2019-08-22 stsp argv += optind;
4088 8e7bd50a 2019-08-22 stsp if (do_list) {
4089 8e7bd50a 2019-08-22 stsp if (tagmsg)
4090 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
4091 8e7bd50a 2019-08-22 stsp if (argc > 0)
4092 8e7bd50a 2019-08-22 stsp usage_tag();
4093 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
4094 8e7bd50a 2019-08-22 stsp usage_tag();
4095 a2887370 2019-08-23 stsp else if (argc > 1)
4096 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
4097 a2887370 2019-08-23 stsp tag_name = argv[0];
4099 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4100 8e7bd50a 2019-08-22 stsp if (do_list) {
4101 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4102 8e7bd50a 2019-08-22 stsp NULL) == -1)
4103 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4105 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4106 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4107 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4110 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4111 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4112 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4113 8e7bd50a 2019-08-22 stsp goto done;
4116 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4117 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4118 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4119 8e7bd50a 2019-08-22 stsp goto done;
4121 8e7bd50a 2019-08-22 stsp error = NULL;
4122 8e7bd50a 2019-08-22 stsp if (worktree) {
4123 8e7bd50a 2019-08-22 stsp repo_path =
4124 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4125 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4126 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4127 8e7bd50a 2019-08-22 stsp if (error)
4128 8e7bd50a 2019-08-22 stsp goto done;
4130 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4131 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4132 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4133 8e7bd50a 2019-08-22 stsp goto done;
4138 8e7bd50a 2019-08-22 stsp if (do_list) {
4139 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4140 c9956ddf 2019-09-08 stsp if (error != NULL)
4141 c9956ddf 2019-09-08 stsp goto done;
4142 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4143 8e7bd50a 2019-08-22 stsp if (error)
4144 8e7bd50a 2019-08-22 stsp goto done;
4145 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4147 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4148 c9956ddf 2019-09-08 stsp if (error)
4149 c9956ddf 2019-09-08 stsp goto done;
4150 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4151 c9956ddf 2019-09-08 stsp if (error != NULL)
4152 c9956ddf 2019-09-08 stsp goto done;
4154 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4155 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4156 8e7bd50a 2019-08-22 stsp if (error)
4157 8e7bd50a 2019-08-22 stsp goto done;
4160 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4161 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4162 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4163 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4164 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4165 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4166 8e7bd50a 2019-08-22 stsp if (error)
4167 8e7bd50a 2019-08-22 stsp goto done;
4168 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4169 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4170 8e7bd50a 2019-08-22 stsp if (error)
4171 8e7bd50a 2019-08-22 stsp goto done;
4172 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4173 8e7bd50a 2019-08-22 stsp free(commit_id);
4174 8e7bd50a 2019-08-22 stsp if (error)
4175 8e7bd50a 2019-08-22 stsp goto done;
4178 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4179 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4183 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4184 8e7bd50a 2019-08-22 stsp if (worktree)
4185 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4186 8e7bd50a 2019-08-22 stsp free(cwd);
4187 8e7bd50a 2019-08-22 stsp free(repo_path);
4188 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4189 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4190 8e7bd50a 2019-08-22 stsp return error;
4193 8e7bd50a 2019-08-22 stsp __dead static void
4194 d00136be 2019-03-26 stsp usage_add(void)
4196 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4197 022fae89 2019-12-06 tracey getprogname());
4201 d00136be 2019-03-26 stsp static const struct got_error *
4202 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4204 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4206 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4207 4e68cba3 2019-11-23 stsp return NULL;
4210 4e68cba3 2019-11-23 stsp static const struct got_error *
4211 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4213 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4214 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4215 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4216 1dd54920 2019-05-11 stsp char *cwd = NULL;
4217 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4218 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4219 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4221 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4223 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4224 d00136be 2019-03-26 stsp switch (ch) {
4225 022fae89 2019-12-06 tracey case 'I':
4226 022fae89 2019-12-06 tracey no_ignores = 1;
4229 4e68cba3 2019-11-23 stsp can_recurse = 1;
4232 d00136be 2019-03-26 stsp usage_add();
4233 d00136be 2019-03-26 stsp /* NOTREACHED */
4237 d00136be 2019-03-26 stsp argc -= optind;
4238 d00136be 2019-03-26 stsp argv += optind;
4240 43012d58 2019-07-14 stsp #ifndef PROFILE
4241 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4242 43012d58 2019-07-14 stsp NULL) == -1)
4243 43012d58 2019-07-14 stsp err(1, "pledge");
4245 723c305c 2019-05-11 jcs if (argc < 1)
4246 d00136be 2019-03-26 stsp usage_add();
4248 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4249 d00136be 2019-03-26 stsp if (cwd == NULL) {
4250 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4251 d00136be 2019-03-26 stsp goto done;
4254 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4255 d00136be 2019-03-26 stsp if (error)
4256 d00136be 2019-03-26 stsp goto done;
4258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4260 031a5338 2019-03-26 stsp if (error != NULL)
4261 031a5338 2019-03-26 stsp goto done;
4263 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4264 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4265 d00136be 2019-03-26 stsp if (error)
4266 d00136be 2019-03-26 stsp goto done;
4268 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4269 6d022e97 2019-08-04 stsp if (error)
4270 022fae89 2019-12-06 tracey goto done;
4272 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4273 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4274 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4275 6d022e97 2019-08-04 stsp goto done;
4279 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4280 4e68cba3 2019-11-23 stsp char *ondisk_path;
4281 4e68cba3 2019-11-23 stsp struct stat sb;
4282 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4283 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4284 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4285 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4286 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4287 4e68cba3 2019-11-23 stsp goto done;
4289 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4290 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4291 4e68cba3 2019-11-23 stsp free(ondisk_path);
4294 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4295 4e68cba3 2019-11-23 stsp ondisk_path);
4296 4e68cba3 2019-11-23 stsp free(ondisk_path);
4297 4e68cba3 2019-11-23 stsp goto done;
4299 4e68cba3 2019-11-23 stsp free(ondisk_path);
4300 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4301 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4302 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4303 4e68cba3 2019-11-23 stsp goto done;
4308 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4309 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4312 031a5338 2019-03-26 stsp got_repo_close(repo);
4313 d00136be 2019-03-26 stsp if (worktree)
4314 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4315 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4316 1dd54920 2019-05-11 stsp free((char *)pe->path);
4317 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4318 2ec1f75b 2019-03-26 stsp free(cwd);
4319 2ec1f75b 2019-03-26 stsp return error;
4322 2ec1f75b 2019-03-26 stsp __dead static void
4323 648e4ef7 2019-07-09 stsp usage_remove(void)
4325 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4326 f2a9dc41 2019-12-13 tracey getprogname());
4330 2a06fe5f 2019-08-24 stsp static const struct got_error *
4331 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4332 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4334 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4336 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4337 2a06fe5f 2019-08-24 stsp return NULL;
4338 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4339 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4340 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4341 2a06fe5f 2019-08-24 stsp return NULL;
4344 2ec1f75b 2019-03-26 stsp static const struct got_error *
4345 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4347 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4348 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4349 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4350 17ed4618 2019-06-02 stsp char *cwd = NULL;
4351 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4352 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4353 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4355 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4357 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4358 2ec1f75b 2019-03-26 stsp switch (ch) {
4360 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4362 70e3e7f5 2019-12-13 tracey case 'k':
4363 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4365 f2a9dc41 2019-12-13 tracey case 'R':
4366 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4369 f2a9dc41 2019-12-13 tracey usage_remove();
4370 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4374 2ec1f75b 2019-03-26 stsp argc -= optind;
4375 2ec1f75b 2019-03-26 stsp argv += optind;
4377 43012d58 2019-07-14 stsp #ifndef PROFILE
4378 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4379 43012d58 2019-07-14 stsp NULL) == -1)
4380 43012d58 2019-07-14 stsp err(1, "pledge");
4382 17ed4618 2019-06-02 stsp if (argc < 1)
4383 648e4ef7 2019-07-09 stsp usage_remove();
4385 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4386 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4387 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4388 2ec1f75b 2019-03-26 stsp goto done;
4390 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4391 2ec1f75b 2019-03-26 stsp if (error)
4392 2ec1f75b 2019-03-26 stsp goto done;
4394 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4397 2ec1f75b 2019-03-26 stsp goto done;
4399 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4400 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4401 2ec1f75b 2019-03-26 stsp if (error)
4402 2ec1f75b 2019-03-26 stsp goto done;
4404 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4405 6d022e97 2019-08-04 stsp if (error)
4406 6d022e97 2019-08-04 stsp goto done;
4408 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4409 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4410 f2a9dc41 2019-12-13 tracey struct stat sb;
4411 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4412 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4413 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4414 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4415 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4416 f2a9dc41 2019-12-13 tracey goto done;
4418 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4419 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4420 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4421 f2a9dc41 2019-12-13 tracey continue;
4423 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4424 f2a9dc41 2019-12-13 tracey ondisk_path);
4425 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4426 f2a9dc41 2019-12-13 tracey goto done;
4428 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4429 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4430 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4431 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4432 f2a9dc41 2019-12-13 tracey goto done;
4437 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4438 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4439 a129376b 2019-03-28 stsp if (error)
4440 a129376b 2019-03-28 stsp goto done;
4443 a129376b 2019-03-28 stsp got_repo_close(repo);
4444 a129376b 2019-03-28 stsp if (worktree)
4445 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4446 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4447 17ed4618 2019-06-02 stsp free((char *)pe->path);
4448 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4449 a129376b 2019-03-28 stsp free(cwd);
4450 a129376b 2019-03-28 stsp return error;
4453 a129376b 2019-03-28 stsp __dead static void
4454 a129376b 2019-03-28 stsp usage_revert(void)
4456 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4457 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4461 1ee397ad 2019-07-12 stsp static const struct got_error *
4462 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4464 a129376b 2019-03-28 stsp while (path[0] == '/')
4466 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4467 33aa809d 2019-08-08 stsp return NULL;
4470 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4471 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4472 33aa809d 2019-08-08 stsp const char *action;
4475 33aa809d 2019-08-08 stsp static const struct got_error *
4476 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4477 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4479 33aa809d 2019-08-08 stsp char *line = NULL;
4480 33aa809d 2019-08-08 stsp size_t linesize = 0;
4481 33aa809d 2019-08-08 stsp ssize_t linelen;
4483 33aa809d 2019-08-08 stsp switch (status) {
4484 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4485 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4487 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4488 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4490 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4491 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4492 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4493 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4494 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4495 33aa809d 2019-08-08 stsp printf("%s", line);
4496 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4497 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4498 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4499 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4500 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4503 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4506 33aa809d 2019-08-08 stsp return NULL;
4509 33aa809d 2019-08-08 stsp static const struct got_error *
4510 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4511 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4513 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4514 33aa809d 2019-08-08 stsp char *line = NULL;
4515 33aa809d 2019-08-08 stsp size_t linesize = 0;
4516 33aa809d 2019-08-08 stsp ssize_t linelen;
4517 33aa809d 2019-08-08 stsp int resp = ' ';
4518 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4520 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4522 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4524 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4525 33aa809d 2019-08-08 stsp a->action);
4527 33aa809d 2019-08-08 stsp return err;
4528 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4529 33aa809d 2019-08-08 stsp if (linelen == -1) {
4530 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4531 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4532 33aa809d 2019-08-08 stsp return NULL;
4534 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4536 33aa809d 2019-08-08 stsp *nl = '\0';
4537 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4538 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4539 33aa809d 2019-08-08 stsp printf("y\n");
4540 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4541 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4542 33aa809d 2019-08-08 stsp printf("n\n");
4543 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4544 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4545 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4546 33aa809d 2019-08-08 stsp printf("q\n");
4548 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4549 33aa809d 2019-08-08 stsp free(line);
4550 33aa809d 2019-08-08 stsp return NULL;
4553 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4554 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4555 33aa809d 2019-08-08 stsp a->action);
4557 33aa809d 2019-08-08 stsp return err;
4558 33aa809d 2019-08-08 stsp resp = getchar();
4559 33aa809d 2019-08-08 stsp if (resp == '\n')
4560 33aa809d 2019-08-08 stsp resp = getchar();
4561 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4562 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4563 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4564 33aa809d 2019-08-08 stsp resp = ' ';
4566 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4567 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4568 33aa809d 2019-08-08 stsp resp = ' ';
4572 33aa809d 2019-08-08 stsp if (resp == 'y')
4573 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4574 33aa809d 2019-08-08 stsp else if (resp == 'n')
4575 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4576 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4577 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4579 1ee397ad 2019-07-12 stsp return NULL;
4583 a129376b 2019-03-28 stsp static const struct got_error *
4584 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4586 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4587 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4588 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4589 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4590 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4591 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4592 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4593 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4594 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4595 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4597 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4599 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4600 a129376b 2019-03-28 stsp switch (ch) {
4602 33aa809d 2019-08-08 stsp pflag = 1;
4605 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4608 0f6d7415 2019-08-08 stsp can_recurse = 1;
4611 a129376b 2019-03-28 stsp usage_revert();
4612 a129376b 2019-03-28 stsp /* NOTREACHED */
4616 a129376b 2019-03-28 stsp argc -= optind;
4617 a129376b 2019-03-28 stsp argv += optind;
4619 43012d58 2019-07-14 stsp #ifndef PROFILE
4620 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4621 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4622 43012d58 2019-07-14 stsp err(1, "pledge");
4624 e20a8b6f 2019-06-04 stsp if (argc < 1)
4625 a129376b 2019-03-28 stsp usage_revert();
4626 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4627 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4629 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4630 a129376b 2019-03-28 stsp if (cwd == NULL) {
4631 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4632 a129376b 2019-03-28 stsp goto done;
4634 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4635 2ec1f75b 2019-03-26 stsp if (error)
4636 2ec1f75b 2019-03-26 stsp goto done;
4638 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4640 a129376b 2019-03-28 stsp if (error != NULL)
4641 a129376b 2019-03-28 stsp goto done;
4643 33aa809d 2019-08-08 stsp if (patch_script_path) {
4644 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4645 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4646 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4647 33aa809d 2019-08-08 stsp patch_script_path);
4648 33aa809d 2019-08-08 stsp goto done;
4651 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4652 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4653 a129376b 2019-03-28 stsp if (error)
4654 a129376b 2019-03-28 stsp goto done;
4656 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4657 6d022e97 2019-08-04 stsp if (error)
4658 6d022e97 2019-08-04 stsp goto done;
4660 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4661 0f6d7415 2019-08-08 stsp char *ondisk_path;
4662 0f6d7415 2019-08-08 stsp struct stat sb;
4663 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4664 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4665 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4666 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4667 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4668 0f6d7415 2019-08-08 stsp goto done;
4670 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4671 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4672 0f6d7415 2019-08-08 stsp free(ondisk_path);
4675 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4676 0f6d7415 2019-08-08 stsp ondisk_path);
4677 0f6d7415 2019-08-08 stsp free(ondisk_path);
4678 0f6d7415 2019-08-08 stsp goto done;
4680 0f6d7415 2019-08-08 stsp free(ondisk_path);
4681 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4682 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4683 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4684 0f6d7415 2019-08-08 stsp goto done;
4689 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4690 33aa809d 2019-08-08 stsp cpa.action = "revert";
4691 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4692 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4693 a129376b 2019-03-28 stsp if (error)
4694 a129376b 2019-03-28 stsp goto done;
4696 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4697 33aa809d 2019-08-08 stsp error == NULL)
4698 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4700 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4701 2ec1f75b 2019-03-26 stsp if (worktree)
4702 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4703 2ec1f75b 2019-03-26 stsp free(path);
4704 d00136be 2019-03-26 stsp free(cwd);
4705 6bad629b 2019-02-04 stsp return error;
4708 c4296144 2019-05-09 stsp __dead static void
4709 c4296144 2019-05-09 stsp usage_commit(void)
4711 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4712 5c1e53bc 2019-07-28 stsp getprogname());
4716 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4717 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4718 e2ba3d07 2019-05-13 stsp const char *editor;
4719 e0870e44 2019-05-13 stsp const char *worktree_path;
4720 76d98825 2019-06-03 stsp const char *branch_name;
4721 314a6357 2019-05-13 stsp const char *repo_path;
4722 e0870e44 2019-05-13 stsp char *logmsg_path;
4726 33ad4cbe 2019-05-12 jcs static const struct got_error *
4727 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4730 76d98825 2019-06-03 stsp char *initial_content = NULL;
4731 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4732 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4733 e0870e44 2019-05-13 stsp char *template = NULL;
4734 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4736 33ad4cbe 2019-05-12 jcs size_t len;
4738 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4739 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4740 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4741 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4742 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4743 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4744 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4745 33ad4cbe 2019-05-12 jcs return NULL;
4748 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4749 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4751 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4752 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4753 76d98825 2019-06-03 stsp a->branch_name) == -1)
4754 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4756 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4758 e0870e44 2019-05-13 stsp goto done;
4760 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4762 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4764 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4765 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4766 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4770 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4772 76d98825 2019-06-03 stsp free(initial_content);
4773 e0870e44 2019-05-13 stsp free(template);
4775 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4776 3ce1b845 2019-07-15 stsp if (err == NULL) {
4777 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4778 3ce1b845 2019-07-15 stsp if (err) {
4779 3ce1b845 2019-07-15 stsp free(*logmsg);
4780 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4783 33ad4cbe 2019-05-12 jcs return err;
4786 c4296144 2019-05-09 stsp static const struct got_error *
4787 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4789 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4790 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4791 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4792 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4793 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4794 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4795 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4796 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4797 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4798 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4800 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4801 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4803 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4804 c4296144 2019-05-09 stsp switch (ch) {
4806 c4296144 2019-05-09 stsp logmsg = optarg;
4809 c4296144 2019-05-09 stsp usage_commit();
4810 c4296144 2019-05-09 stsp /* NOTREACHED */
4814 c4296144 2019-05-09 stsp argc -= optind;
4815 c4296144 2019-05-09 stsp argv += optind;
4817 43012d58 2019-07-14 stsp #ifndef PROFILE
4818 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4819 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4820 43012d58 2019-07-14 stsp err(1, "pledge");
4822 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4823 c4296144 2019-05-09 stsp if (cwd == NULL) {
4824 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4825 c4296144 2019-05-09 stsp goto done;
4827 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4828 7d5807f4 2019-07-11 stsp if (error)
4829 7d5807f4 2019-07-11 stsp goto done;
4831 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4832 c4296144 2019-05-09 stsp if (error)
4833 a698f62e 2019-07-25 stsp goto done;
4834 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4835 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4836 c4296144 2019-05-09 stsp goto done;
4839 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4840 916f288c 2019-07-30 stsp worktree);
4841 5c1e53bc 2019-07-28 stsp if (error)
4842 5c1e53bc 2019-07-28 stsp goto done;
4844 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4845 c9956ddf 2019-09-08 stsp if (error)
4846 c9956ddf 2019-09-08 stsp goto done;
4847 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4848 c9956ddf 2019-09-08 stsp gitconfig_path);
4849 c4296144 2019-05-09 stsp if (error != NULL)
4850 c4296144 2019-05-09 stsp goto done;
4852 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4853 aba9c984 2019-09-08 stsp if (error)
4854 aba9c984 2019-09-08 stsp return error;
4857 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4858 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4860 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4861 314a6357 2019-05-13 stsp error = get_editor(&editor);
4863 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4864 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4865 c4296144 2019-05-09 stsp if (error)
4866 c4296144 2019-05-09 stsp goto done;
4868 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4869 087fb88c 2019-08-04 stsp if (error)
4870 087fb88c 2019-08-04 stsp goto done;
4872 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4873 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4874 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4875 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4876 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4877 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4878 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4879 916f288c 2019-07-30 stsp goto done;
4881 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4883 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4884 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4885 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4886 e0870e44 2019-05-13 stsp if (error) {
4887 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4888 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4889 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4890 c4296144 2019-05-09 stsp goto done;
4893 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4894 c4296144 2019-05-09 stsp if (error)
4895 c4296144 2019-05-09 stsp goto done;
4896 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4898 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4899 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4900 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4901 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4902 7266f21f 2019-10-21 stsp error == NULL)
4903 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4904 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4906 c4296144 2019-05-09 stsp got_repo_close(repo);
4907 c4296144 2019-05-09 stsp if (worktree)
4908 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4909 c4296144 2019-05-09 stsp free(cwd);
4910 c4296144 2019-05-09 stsp free(id_str);
4911 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4912 e2ba3d07 2019-05-13 stsp free(editor);
4913 aba9c984 2019-09-08 stsp free(author);
4914 234035bc 2019-06-01 stsp return error;
4917 234035bc 2019-06-01 stsp __dead static void
4918 234035bc 2019-06-01 stsp usage_cherrypick(void)
4920 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4924 234035bc 2019-06-01 stsp static const struct got_error *
4925 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4927 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4928 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4929 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4930 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4931 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4932 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4933 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4934 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4935 234035bc 2019-06-01 stsp int ch, did_something = 0;
4937 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4938 234035bc 2019-06-01 stsp switch (ch) {
4940 234035bc 2019-06-01 stsp usage_cherrypick();
4941 234035bc 2019-06-01 stsp /* NOTREACHED */
4945 234035bc 2019-06-01 stsp argc -= optind;
4946 234035bc 2019-06-01 stsp argv += optind;
4948 43012d58 2019-07-14 stsp #ifndef PROFILE
4949 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4950 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4951 43012d58 2019-07-14 stsp err(1, "pledge");
4953 234035bc 2019-06-01 stsp if (argc != 1)
4954 234035bc 2019-06-01 stsp usage_cherrypick();
4956 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4957 234035bc 2019-06-01 stsp if (cwd == NULL) {
4958 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4959 234035bc 2019-06-01 stsp goto done;
4961 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4962 234035bc 2019-06-01 stsp if (error)
4963 234035bc 2019-06-01 stsp goto done;
4965 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4967 234035bc 2019-06-01 stsp if (error != NULL)
4968 234035bc 2019-06-01 stsp goto done;
4970 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4971 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4972 234035bc 2019-06-01 stsp if (error)
4973 234035bc 2019-06-01 stsp goto done;
4975 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4976 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4977 234035bc 2019-06-01 stsp if (error != NULL) {
4978 234035bc 2019-06-01 stsp struct got_reference *ref;
4979 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4980 234035bc 2019-06-01 stsp goto done;
4981 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4982 234035bc 2019-06-01 stsp if (error != NULL)
4983 234035bc 2019-06-01 stsp goto done;
4984 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4985 234035bc 2019-06-01 stsp got_ref_close(ref);
4986 234035bc 2019-06-01 stsp if (error != NULL)
4987 234035bc 2019-06-01 stsp goto done;
4989 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4990 234035bc 2019-06-01 stsp if (error)
4991 234035bc 2019-06-01 stsp goto done;
4993 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4994 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4995 234035bc 2019-06-01 stsp if (error != NULL)
4996 234035bc 2019-06-01 stsp goto done;
4998 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4999 234035bc 2019-06-01 stsp if (error) {
5000 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5001 234035bc 2019-06-01 stsp goto done;
5002 234035bc 2019-06-01 stsp error = NULL;
5004 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5005 234035bc 2019-06-01 stsp goto done;
5008 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5009 234035bc 2019-06-01 stsp if (error)
5010 234035bc 2019-06-01 stsp goto done;
5011 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5012 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5013 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5015 234035bc 2019-06-01 stsp if (error != NULL)
5016 234035bc 2019-06-01 stsp goto done;
5018 234035bc 2019-06-01 stsp if (did_something)
5019 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5021 234035bc 2019-06-01 stsp if (commit)
5022 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5023 234035bc 2019-06-01 stsp free(commit_id_str);
5024 234035bc 2019-06-01 stsp if (head_ref)
5025 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5026 234035bc 2019-06-01 stsp if (worktree)
5027 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5029 234035bc 2019-06-01 stsp got_repo_close(repo);
5030 c4296144 2019-05-09 stsp return error;
5033 5ef14e63 2019-06-02 stsp __dead static void
5034 5ef14e63 2019-06-02 stsp usage_backout(void)
5036 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5040 5ef14e63 2019-06-02 stsp static const struct got_error *
5041 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5043 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5044 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5045 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5046 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5047 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5048 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5049 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5050 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5051 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5053 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5054 5ef14e63 2019-06-02 stsp switch (ch) {
5056 5ef14e63 2019-06-02 stsp usage_backout();
5057 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5061 5ef14e63 2019-06-02 stsp argc -= optind;
5062 5ef14e63 2019-06-02 stsp argv += optind;
5064 43012d58 2019-07-14 stsp #ifndef PROFILE
5065 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5066 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5067 43012d58 2019-07-14 stsp err(1, "pledge");
5069 5ef14e63 2019-06-02 stsp if (argc != 1)
5070 5ef14e63 2019-06-02 stsp usage_backout();
5072 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5073 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5074 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5075 5ef14e63 2019-06-02 stsp goto done;
5077 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5078 5ef14e63 2019-06-02 stsp if (error)
5079 5ef14e63 2019-06-02 stsp goto done;
5081 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5083 5ef14e63 2019-06-02 stsp if (error != NULL)
5084 5ef14e63 2019-06-02 stsp goto done;
5086 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5087 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5088 5ef14e63 2019-06-02 stsp if (error)
5089 5ef14e63 2019-06-02 stsp goto done;
5091 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5092 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5093 5ef14e63 2019-06-02 stsp if (error != NULL) {
5094 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5095 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5096 5ef14e63 2019-06-02 stsp goto done;
5097 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5098 5ef14e63 2019-06-02 stsp if (error != NULL)
5099 5ef14e63 2019-06-02 stsp goto done;
5100 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5101 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5102 5ef14e63 2019-06-02 stsp if (error != NULL)
5103 5ef14e63 2019-06-02 stsp goto done;
5105 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5106 5ef14e63 2019-06-02 stsp if (error)
5107 5ef14e63 2019-06-02 stsp goto done;
5109 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5110 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5111 5ef14e63 2019-06-02 stsp if (error != NULL)
5112 5ef14e63 2019-06-02 stsp goto done;
5114 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5115 5ef14e63 2019-06-02 stsp if (error)
5116 5ef14e63 2019-06-02 stsp goto done;
5118 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5119 5ef14e63 2019-06-02 stsp if (error)
5120 5ef14e63 2019-06-02 stsp goto done;
5121 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5122 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5123 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5124 5ef14e63 2019-06-02 stsp goto done;
5127 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5128 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5129 5ef14e63 2019-06-02 stsp if (error != NULL)
5130 5ef14e63 2019-06-02 stsp goto done;
5132 5ef14e63 2019-06-02 stsp if (did_something)
5133 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5135 5ef14e63 2019-06-02 stsp if (commit)
5136 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5137 5ef14e63 2019-06-02 stsp free(commit_id_str);
5138 5ef14e63 2019-06-02 stsp if (head_ref)
5139 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5140 818c7501 2019-07-11 stsp if (worktree)
5141 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5143 818c7501 2019-07-11 stsp got_repo_close(repo);
5144 818c7501 2019-07-11 stsp return error;
5147 818c7501 2019-07-11 stsp __dead static void
5148 818c7501 2019-07-11 stsp usage_rebase(void)
5150 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5151 af54c8f8 2019-07-11 stsp getprogname());
5156 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5159 0ebf8283 2019-07-24 stsp size_t len;
5161 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5162 0ebf8283 2019-07-24 stsp if (len > limit)
5163 0ebf8283 2019-07-24 stsp len = limit;
5164 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5165 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5167 0ebf8283 2019-07-24 stsp *nl = '\0';
5170 0ebf8283 2019-07-24 stsp static const struct got_error *
5171 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5173 5943eee2 2019-08-13 stsp const struct got_error *err;
5174 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5175 5943eee2 2019-08-13 stsp const char *s;
5177 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5179 5943eee2 2019-08-13 stsp return err;
5181 5943eee2 2019-08-13 stsp s = logmsg0;
5182 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5185 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5186 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5187 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5188 5943eee2 2019-08-13 stsp goto done;
5191 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5193 5943eee2 2019-08-13 stsp free(logmsg0);
5194 5943eee2 2019-08-13 stsp return err;
5197 818c7501 2019-07-11 stsp static const struct got_error *
5198 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5199 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5201 818c7501 2019-07-11 stsp const struct got_error *err;
5202 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5204 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5206 818c7501 2019-07-11 stsp goto done;
5208 ff0d2220 2019-07-11 stsp if (new_id) {
5209 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5211 ff0d2220 2019-07-11 stsp goto done;
5214 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5215 ff0d2220 2019-07-11 stsp if (new_id_str)
5216 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5218 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5220 0ebf8283 2019-07-24 stsp goto done;
5222 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5223 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5225 818c7501 2019-07-11 stsp free(old_id_str);
5226 818c7501 2019-07-11 stsp free(new_id_str);
5227 818c7501 2019-07-11 stsp return err;
5230 1ee397ad 2019-07-12 stsp static const struct got_error *
5231 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5233 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5235 818c7501 2019-07-11 stsp while (path[0] == '/')
5237 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5239 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5240 1ee397ad 2019-07-12 stsp return NULL;
5241 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5242 818c7501 2019-07-11 stsp *rebase_status = status;
5243 1ee397ad 2019-07-12 stsp return NULL;
5246 818c7501 2019-07-11 stsp static const struct got_error *
5247 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5248 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5249 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5251 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5252 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5253 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5256 818c7501 2019-07-11 stsp static const struct got_error *
5257 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5258 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5259 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5260 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5262 ff0d2220 2019-07-11 stsp const struct got_error *error;
5263 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5264 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5266 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5267 ff0d2220 2019-07-11 stsp if (error)
5268 ff0d2220 2019-07-11 stsp return error;
5270 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5271 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5272 ff0d2220 2019-07-11 stsp if (error) {
5273 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5274 ff0d2220 2019-07-11 stsp goto done;
5275 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5277 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5278 ff0d2220 2019-07-11 stsp free(new_commit_id);
5281 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5282 ff0d2220 2019-07-11 stsp return error;
5285 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5286 64c6d990 2019-07-11 stsp const char *path_prefix;
5287 64c6d990 2019-07-11 stsp size_t len;
5288 8ca9bd68 2019-07-25 stsp int errcode;
5291 64c6d990 2019-07-11 stsp static const struct got_error *
5292 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5293 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5294 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5295 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5297 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5299 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5300 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5301 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5303 64c6d990 2019-07-11 stsp return NULL;
5306 64c6d990 2019-07-11 stsp static const struct got_error *
5307 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5308 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5309 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5311 64c6d990 2019-07-11 stsp const struct got_error *err;
5312 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5313 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5314 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5316 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5317 64c6d990 2019-07-11 stsp return NULL;
5319 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5321 64c6d990 2019-07-11 stsp goto done;
5323 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5325 64c6d990 2019-07-11 stsp goto done;
5327 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5328 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5330 64c6d990 2019-07-11 stsp goto done;
5332 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5333 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5335 64c6d990 2019-07-11 stsp goto done;
5337 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5338 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5339 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5340 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5341 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5342 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5343 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5345 64c6d990 2019-07-11 stsp if (tree1)
5346 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5347 64c6d990 2019-07-11 stsp if (tree2)
5348 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5349 64c6d990 2019-07-11 stsp if (commit)
5350 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5351 64c6d990 2019-07-11 stsp if (parent_commit)
5352 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5353 64c6d990 2019-07-11 stsp return err;
5356 ff0d2220 2019-07-11 stsp static const struct got_error *
5357 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5358 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5359 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5360 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5361 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5363 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5364 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5365 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5366 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5367 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5369 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5371 af61c510 2019-07-19 stsp return err;
5373 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5374 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5376 af61c510 2019-07-19 stsp goto done;
5377 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5378 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5379 af61c510 2019-07-19 stsp if (err) {
5380 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5381 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5382 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5383 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5384 af61c510 2019-07-19 stsp "been reached?!?");
5385 af61c510 2019-07-19 stsp goto done;
5386 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5387 af61c510 2019-07-19 stsp goto done;
5388 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5389 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5391 af61c510 2019-07-19 stsp goto done;
5393 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5394 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5396 af61c510 2019-07-19 stsp goto done;
5398 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5400 af61c510 2019-07-19 stsp goto done;
5401 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5402 af61c510 2019-07-19 stsp commit_id = parent_id;
5406 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5407 af61c510 2019-07-19 stsp return err;
5410 af61c510 2019-07-19 stsp static const struct got_error *
5411 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5413 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5414 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5415 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5416 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5417 818c7501 2019-07-11 stsp char *cwd = NULL;
5418 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5419 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5420 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5421 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5422 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5423 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5424 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5425 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5426 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5427 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5428 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5429 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5431 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5432 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5434 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5435 818c7501 2019-07-11 stsp switch (ch) {
5437 818c7501 2019-07-11 stsp abort_rebase = 1;
5440 818c7501 2019-07-11 stsp continue_rebase = 1;
5443 818c7501 2019-07-11 stsp usage_rebase();
5444 818c7501 2019-07-11 stsp /* NOTREACHED */
5448 818c7501 2019-07-11 stsp argc -= optind;
5449 818c7501 2019-07-11 stsp argv += optind;
5451 43012d58 2019-07-14 stsp #ifndef PROFILE
5452 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5453 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5454 43012d58 2019-07-14 stsp err(1, "pledge");
5456 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5457 818c7501 2019-07-11 stsp usage_rebase();
5458 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5459 818c7501 2019-07-11 stsp if (argc != 0)
5460 818c7501 2019-07-11 stsp usage_rebase();
5461 818c7501 2019-07-11 stsp } else if (argc != 1)
5462 818c7501 2019-07-11 stsp usage_rebase();
5464 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5465 818c7501 2019-07-11 stsp if (cwd == NULL) {
5466 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5467 818c7501 2019-07-11 stsp goto done;
5469 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5470 818c7501 2019-07-11 stsp if (error)
5471 818c7501 2019-07-11 stsp goto done;
5473 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5475 818c7501 2019-07-11 stsp if (error != NULL)
5476 818c7501 2019-07-11 stsp goto done;
5478 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5479 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5480 818c7501 2019-07-11 stsp if (error)
5481 818c7501 2019-07-11 stsp goto done;
5483 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5484 818c7501 2019-07-11 stsp if (error)
5485 818c7501 2019-07-11 stsp goto done;
5487 f6794adc 2019-07-23 stsp if (abort_rebase) {
5488 818c7501 2019-07-11 stsp int did_something;
5489 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5490 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5491 f6794adc 2019-07-23 stsp goto done;
5493 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5494 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5495 3e3a69f1 2019-07-25 stsp worktree, repo);
5496 818c7501 2019-07-11 stsp if (error)
5497 818c7501 2019-07-11 stsp goto done;
5498 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5499 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5500 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5501 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5502 818c7501 2019-07-11 stsp if (error)
5503 818c7501 2019-07-11 stsp goto done;
5504 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5505 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5508 818c7501 2019-07-11 stsp if (continue_rebase) {
5509 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5510 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5511 f6794adc 2019-07-23 stsp goto done;
5513 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5514 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5515 3e3a69f1 2019-07-25 stsp worktree, repo);
5516 818c7501 2019-07-11 stsp if (error)
5517 818c7501 2019-07-11 stsp goto done;
5519 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5520 01757395 2019-07-12 stsp resume_commit_id, repo);
5521 818c7501 2019-07-11 stsp if (error)
5522 818c7501 2019-07-11 stsp goto done;
5524 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5525 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5526 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5527 818c7501 2019-07-11 stsp goto done;
5530 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5531 818c7501 2019-07-11 stsp if (error != NULL)
5532 818c7501 2019-07-11 stsp goto done;
5535 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5536 ff0d2220 2019-07-11 stsp if (error)
5537 ff0d2220 2019-07-11 stsp goto done;
5539 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5540 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5542 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5543 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5544 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5545 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5546 818c7501 2019-07-11 stsp if (error)
5547 818c7501 2019-07-11 stsp goto done;
5548 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5549 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5550 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5551 818c7501 2019-07-11 stsp "with work tree's branch");
5552 818c7501 2019-07-11 stsp goto done;
5555 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5556 a51a74b3 2019-07-27 stsp if (error) {
5557 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5558 a51a74b3 2019-07-27 stsp goto done;
5559 a51a74b3 2019-07-27 stsp error = NULL;
5561 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5562 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5563 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5564 a51a74b3 2019-07-27 stsp goto done;
5566 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5567 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5568 818c7501 2019-07-11 stsp if (error)
5569 818c7501 2019-07-11 stsp goto done;
5572 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5573 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5574 818c7501 2019-07-11 stsp if (error)
5575 818c7501 2019-07-11 stsp goto done;
5577 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5578 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5579 fc66b545 2019-08-12 stsp if (pid == NULL) {
5580 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5581 fc66b545 2019-08-12 stsp int did_something;
5582 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5583 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5584 fc66b545 2019-08-12 stsp &did_something);
5585 fc66b545 2019-08-12 stsp if (error)
5586 fc66b545 2019-08-12 stsp goto done;
5587 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5588 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5590 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5591 fc66b545 2019-08-12 stsp goto done;
5593 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5594 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5595 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5596 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5597 818c7501 2019-07-11 stsp commit = NULL;
5598 818c7501 2019-07-11 stsp if (error)
5599 818c7501 2019-07-11 stsp goto done;
5601 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5602 38b0338b 2019-11-29 stsp if (continue_rebase) {
5603 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5604 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5605 38b0338b 2019-11-29 stsp goto done;
5607 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5608 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5609 38b0338b 2019-11-29 stsp char *id_str;
5610 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5611 38b0338b 2019-11-29 stsp new_base_branch);
5612 38b0338b 2019-11-29 stsp if (error)
5613 38b0338b 2019-11-29 stsp goto done;
5614 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5615 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5616 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5617 38b0338b 2019-11-29 stsp free(id_str);
5618 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5619 38b0338b 2019-11-29 stsp new_head_commit_id);
5620 38b0338b 2019-11-29 stsp if (error)
5621 38b0338b 2019-11-29 stsp goto done;
5625 818c7501 2019-07-11 stsp pid = NULL;
5626 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5627 818c7501 2019-07-11 stsp commit_id = qid->id;
5628 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5629 818c7501 2019-07-11 stsp pid = qid;
5631 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5632 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5633 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5634 818c7501 2019-07-11 stsp if (error)
5635 818c7501 2019-07-11 stsp goto done;
5637 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5638 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5642 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5643 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5644 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5645 818c7501 2019-07-11 stsp if (error)
5646 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5650 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5651 818c7501 2019-07-11 stsp if (error)
5652 818c7501 2019-07-11 stsp goto done;
5653 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5654 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5656 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5657 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5659 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5660 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5661 818c7501 2019-07-11 stsp free(resume_commit_id);
5662 818c7501 2019-07-11 stsp free(yca_id);
5663 818c7501 2019-07-11 stsp if (commit)
5664 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5665 818c7501 2019-07-11 stsp if (branch)
5666 818c7501 2019-07-11 stsp got_ref_close(branch);
5667 818c7501 2019-07-11 stsp if (new_base_branch)
5668 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5669 818c7501 2019-07-11 stsp if (tmp_branch)
5670 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5671 5ef14e63 2019-06-02 stsp if (worktree)
5672 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5674 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5675 5ef14e63 2019-06-02 stsp return error;
5678 0ebf8283 2019-07-24 stsp __dead static void
5679 0ebf8283 2019-07-24 stsp usage_histedit(void)
5681 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5682 0ebf8283 2019-07-24 stsp getprogname());
5686 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5687 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5688 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5689 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5690 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5692 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5693 0ebf8283 2019-07-24 stsp unsigned char code;
5694 0ebf8283 2019-07-24 stsp const char *name;
5695 0ebf8283 2019-07-24 stsp const char *desc;
5696 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5697 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5698 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5699 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5700 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5701 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5702 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5705 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5706 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5707 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5708 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5709 0ebf8283 2019-07-24 stsp char *logmsg;
5711 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5713 0ebf8283 2019-07-24 stsp static const struct got_error *
5714 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5715 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5717 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5718 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5719 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5722 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5724 0ebf8283 2019-07-24 stsp goto done;
5726 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5728 0ebf8283 2019-07-24 stsp goto done;
5730 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5732 0ebf8283 2019-07-24 stsp goto done;
5734 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5735 0ebf8283 2019-07-24 stsp if (n < 0)
5736 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5738 0ebf8283 2019-07-24 stsp if (commit)
5739 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5740 0ebf8283 2019-07-24 stsp free(id_str);
5741 0ebf8283 2019-07-24 stsp free(logmsg);
5742 0ebf8283 2019-07-24 stsp return err;
5745 0ebf8283 2019-07-24 stsp static const struct got_error *
5746 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5747 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5749 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5750 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5752 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5753 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5755 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5756 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5762 0ebf8283 2019-07-24 stsp return err;
5765 0ebf8283 2019-07-24 stsp static const struct got_error *
5766 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5768 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5771 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5772 0ebf8283 2019-07-24 stsp if (n < 0)
5773 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5775 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5776 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5777 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5778 0ebf8283 2019-07-24 stsp cmd->desc);
5779 0ebf8283 2019-07-24 stsp if (n < 0) {
5780 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5784 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5785 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5786 0ebf8283 2019-07-24 stsp if (n < 0)
5787 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5788 0ebf8283 2019-07-24 stsp return err;
5791 0ebf8283 2019-07-24 stsp static const struct got_error *
5792 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5794 0ebf8283 2019-07-24 stsp static char msg[42];
5797 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5799 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5800 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5802 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5805 0ebf8283 2019-07-24 stsp static const struct got_error *
5806 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5807 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5809 0ebf8283 2019-07-24 stsp const struct got_error *err;
5810 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5811 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5813 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5815 0ebf8283 2019-07-24 stsp return err;
5817 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5819 0ebf8283 2019-07-24 stsp goto done;
5821 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5823 5943eee2 2019-08-13 stsp goto done;
5824 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5825 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5826 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5827 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5828 0ebf8283 2019-07-24 stsp goto done;
5831 0ebf8283 2019-07-24 stsp if (folded_commit)
5832 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5833 0ebf8283 2019-07-24 stsp free(id_str);
5834 5943eee2 2019-08-13 stsp free(folded_logmsg);
5835 0ebf8283 2019-07-24 stsp return err;
5838 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5839 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5841 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5843 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5844 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5845 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5846 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5847 3f9de99f 2019-07-24 stsp folded = prev;
5848 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5851 0ebf8283 2019-07-24 stsp return folded;
5854 0ebf8283 2019-07-24 stsp static const struct got_error *
5855 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5856 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5858 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5859 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5860 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5861 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5863 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5865 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5867 0ebf8283 2019-07-24 stsp return err;
5869 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5870 0ebf8283 2019-07-24 stsp if (folded) {
5871 0ebf8283 2019-07-24 stsp while (folded != hle) {
5872 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5873 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5876 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5877 0ebf8283 2019-07-24 stsp logmsg, repo);
5879 0ebf8283 2019-07-24 stsp goto done;
5880 0ebf8283 2019-07-24 stsp free(logmsg);
5881 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5882 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5886 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5888 0ebf8283 2019-07-24 stsp goto done;
5889 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5891 5943eee2 2019-08-13 stsp goto done;
5892 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5893 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5894 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5895 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5896 0ebf8283 2019-07-24 stsp goto done;
5898 0ebf8283 2019-07-24 stsp free(logmsg);
5899 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5901 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5903 0ebf8283 2019-07-24 stsp goto done;
5905 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5907 0ebf8283 2019-07-24 stsp goto done;
5909 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5910 0ebf8283 2019-07-24 stsp close(fd);
5912 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5914 0ebf8283 2019-07-24 stsp goto done;
5916 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5917 0ebf8283 2019-07-24 stsp if (err) {
5918 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5919 0ebf8283 2019-07-24 stsp goto done;
5920 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5923 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5924 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5925 0ebf8283 2019-07-24 stsp free(logmsg_path);
5926 0ebf8283 2019-07-24 stsp free(logmsg);
5927 5943eee2 2019-08-13 stsp free(orig_logmsg);
5928 0ebf8283 2019-07-24 stsp free(editor);
5929 0ebf8283 2019-07-24 stsp if (commit)
5930 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5931 0ebf8283 2019-07-24 stsp return err;
5934 0ebf8283 2019-07-24 stsp static const struct got_error *
5935 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5936 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5938 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5939 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5940 0ebf8283 2019-07-24 stsp size_t size;
5941 0ebf8283 2019-07-24 stsp ssize_t len;
5942 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5943 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5944 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5945 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5947 0ebf8283 2019-07-24 stsp for (;;) {
5948 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5949 0ebf8283 2019-07-24 stsp if (len == -1) {
5950 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5951 0ebf8283 2019-07-24 stsp if (feof(f))
5953 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5954 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5959 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5961 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5962 0ebf8283 2019-07-24 stsp free(line);
5963 0ebf8283 2019-07-24 stsp line = NULL;
5966 0ebf8283 2019-07-24 stsp cmd = NULL;
5967 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5968 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5969 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5970 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5971 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5974 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5979 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5980 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5983 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5985 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5986 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5987 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5990 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5991 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5995 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5996 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5997 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6001 0ebf8283 2019-07-24 stsp free(line);
6002 0ebf8283 2019-07-24 stsp line = NULL;
6006 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6008 0ebf8283 2019-07-24 stsp *end = '\0';
6010 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6011 0ebf8283 2019-07-24 stsp if (err) {
6012 0ebf8283 2019-07-24 stsp /* override error code */
6013 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6017 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6018 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6019 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6022 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6023 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6024 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6025 0ebf8283 2019-07-24 stsp commit_id = NULL;
6026 0ebf8283 2019-07-24 stsp free(line);
6027 0ebf8283 2019-07-24 stsp line = NULL;
6028 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6031 0ebf8283 2019-07-24 stsp free(line);
6032 0ebf8283 2019-07-24 stsp free(commit_id);
6033 0ebf8283 2019-07-24 stsp return err;
6036 0ebf8283 2019-07-24 stsp static const struct got_error *
6037 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6038 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6040 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6041 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6042 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6043 bfce7f83 2019-07-27 stsp static char msg[80];
6044 bfce7f83 2019-07-27 stsp char *id_str;
6046 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6047 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6048 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6049 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6050 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6052 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6053 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6054 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6057 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6058 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6060 bfce7f83 2019-07-27 stsp return err;
6061 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6062 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6063 bfce7f83 2019-07-27 stsp free(id_str);
6064 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6068 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6069 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6070 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6071 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6073 bfce7f83 2019-07-27 stsp return NULL;
6076 bfce7f83 2019-07-27 stsp static const struct got_error *
6077 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6078 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6079 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6081 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6082 0ebf8283 2019-07-24 stsp char *editor;
6083 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6085 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6087 0ebf8283 2019-07-24 stsp return err;
6089 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6090 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6091 0ebf8283 2019-07-24 stsp goto done;
6094 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6095 0ebf8283 2019-07-24 stsp if (f == NULL) {
6096 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6097 0ebf8283 2019-07-24 stsp goto done;
6099 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6101 bfce7f83 2019-07-27 stsp goto done;
6103 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6105 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6106 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6107 0ebf8283 2019-07-24 stsp free(editor);
6108 0ebf8283 2019-07-24 stsp return err;
6111 0ebf8283 2019-07-24 stsp static const struct got_error *
6112 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6113 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
6115 0ebf8283 2019-07-24 stsp static const struct got_error *
6116 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6117 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6119 0ebf8283 2019-07-24 stsp const struct got_error *err;
6120 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6121 0ebf8283 2019-07-24 stsp char *path = NULL;
6123 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6125 0ebf8283 2019-07-24 stsp return err;
6127 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
6129 0ebf8283 2019-07-24 stsp goto done;
6131 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
6133 0ebf8283 2019-07-24 stsp goto done;
6135 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
6136 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6137 0ebf8283 2019-07-24 stsp goto done;
6141 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6142 0ebf8283 2019-07-24 stsp if (err) {
6143 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6144 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6145 0ebf8283 2019-07-24 stsp goto done;
6146 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6147 0ebf8283 2019-07-24 stsp commits, path, repo);
6150 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6151 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6152 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6153 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6154 0ebf8283 2019-07-24 stsp free(path);
6155 0ebf8283 2019-07-24 stsp return err;
6158 0ebf8283 2019-07-24 stsp static const struct got_error *
6159 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6160 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6162 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6163 0ebf8283 2019-07-24 stsp char *path = NULL;
6164 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6165 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6166 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6168 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6170 0ebf8283 2019-07-24 stsp return err;
6172 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6173 0ebf8283 2019-07-24 stsp if (f == NULL) {
6174 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6175 0ebf8283 2019-07-24 stsp goto done;
6177 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6178 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6183 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6184 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6185 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6186 0ebf8283 2019-07-24 stsp if (n < 0) {
6187 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6193 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6194 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6195 0ebf8283 2019-07-24 stsp free(path);
6196 0ebf8283 2019-07-24 stsp if (commit)
6197 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6198 0ebf8283 2019-07-24 stsp return err;
6202 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6204 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6206 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6207 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6208 bfce7f83 2019-07-27 stsp free(hle);
6212 0ebf8283 2019-07-24 stsp static const struct got_error *
6213 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6214 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6216 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6217 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6219 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6220 0ebf8283 2019-07-24 stsp if (f == NULL) {
6221 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6222 0ebf8283 2019-07-24 stsp goto done;
6225 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6227 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6228 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6229 0ebf8283 2019-07-24 stsp return err;
6232 0ebf8283 2019-07-24 stsp static const struct got_error *
6233 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6234 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6235 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6237 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6238 0ebf8283 2019-07-24 stsp int resp = ' ';
6240 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6241 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6242 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6243 0ebf8283 2019-07-24 stsp resp = getchar();
6244 a61a4414 2019-08-07 stsp if (resp == '\n')
6245 a61a4414 2019-08-07 stsp resp = getchar();
6246 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6247 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6248 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6250 426ebf2e 2019-07-25 stsp if (err) {
6251 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6252 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6254 bfce7f83 2019-07-27 stsp prev_err = err;
6255 426ebf2e 2019-07-25 stsp resp = ' ';
6259 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6260 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6261 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6262 0ebf8283 2019-07-24 stsp commits, repo);
6263 426ebf2e 2019-07-25 stsp if (err) {
6264 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6265 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6267 bfce7f83 2019-07-27 stsp prev_err = err;
6268 426ebf2e 2019-07-25 stsp resp = ' ';
6272 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6273 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6276 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6279 0ebf8283 2019-07-24 stsp return err;
6282 0ebf8283 2019-07-24 stsp static const struct got_error *
6283 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6284 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6285 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6287 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6288 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6289 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6290 3e3a69f1 2019-07-25 stsp branch, repo);
6293 0ebf8283 2019-07-24 stsp static const struct got_error *
6294 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6295 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6297 0ebf8283 2019-07-24 stsp const struct got_error *err;
6298 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6300 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6302 0ebf8283 2019-07-24 stsp goto done;
6304 0ebf8283 2019-07-24 stsp if (new_id) {
6305 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6307 0ebf8283 2019-07-24 stsp goto done;
6310 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6311 0ebf8283 2019-07-24 stsp if (new_id_str)
6312 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6314 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6315 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6316 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6317 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6318 0ebf8283 2019-07-24 stsp goto done;
6320 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6322 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6324 0ebf8283 2019-07-24 stsp goto done;
6327 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6328 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6329 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6330 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6331 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6333 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6334 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6335 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6343 0ebf8283 2019-07-24 stsp free(old_id_str);
6344 0ebf8283 2019-07-24 stsp free(new_id_str);
6345 0ebf8283 2019-07-24 stsp return err;
6348 0ebf8283 2019-07-24 stsp static const struct got_error *
6349 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6350 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6351 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6352 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6354 0ebf8283 2019-07-24 stsp const struct got_error *err;
6355 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6356 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6358 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6359 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6360 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6362 0ebf8283 2019-07-24 stsp return err;
6365 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6367 0ebf8283 2019-07-24 stsp return err;
6369 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6370 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6371 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6372 0ebf8283 2019-07-24 stsp if (err) {
6373 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6374 0ebf8283 2019-07-24 stsp goto done;
6375 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6377 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6378 0ebf8283 2019-07-24 stsp free(new_commit_id);
6381 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6382 0ebf8283 2019-07-24 stsp return err;
6385 0ebf8283 2019-07-24 stsp static const struct got_error *
6386 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6387 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6389 0ebf8283 2019-07-24 stsp const struct got_error *error;
6390 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6392 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6394 0ebf8283 2019-07-24 stsp if (error)
6395 0ebf8283 2019-07-24 stsp return error;
6397 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6398 0ebf8283 2019-07-24 stsp if (error)
6399 0ebf8283 2019-07-24 stsp return error;
6401 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6402 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6403 0ebf8283 2019-07-24 stsp return error;
6406 0ebf8283 2019-07-24 stsp static const struct got_error *
6407 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6409 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6410 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6411 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6412 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6413 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6414 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6415 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6416 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6417 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6418 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6419 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6420 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6421 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6422 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6423 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6424 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6425 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6426 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6427 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6428 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6429 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6431 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6432 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6433 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6435 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6436 0ebf8283 2019-07-24 stsp switch (ch) {
6438 0ebf8283 2019-07-24 stsp abort_edit = 1;
6441 0ebf8283 2019-07-24 stsp continue_edit = 1;
6444 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6447 0ebf8283 2019-07-24 stsp usage_histedit();
6448 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6452 0ebf8283 2019-07-24 stsp argc -= optind;
6453 0ebf8283 2019-07-24 stsp argv += optind;
6455 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6456 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6457 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6458 0ebf8283 2019-07-24 stsp err(1, "pledge");
6460 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6461 0ebf8283 2019-07-24 stsp usage_histedit();
6462 0ebf8283 2019-07-24 stsp if (argc != 0)
6463 0ebf8283 2019-07-24 stsp usage_histedit();
6466 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6467 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6468 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6469 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6470 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6471 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6474 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6475 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6476 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6477 0ebf8283 2019-07-24 stsp goto done;
6479 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6480 0ebf8283 2019-07-24 stsp if (error)
6481 0ebf8283 2019-07-24 stsp goto done;
6483 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6485 0ebf8283 2019-07-24 stsp if (error != NULL)
6486 0ebf8283 2019-07-24 stsp goto done;
6488 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6489 0ebf8283 2019-07-24 stsp if (error)
6490 0ebf8283 2019-07-24 stsp goto done;
6491 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6492 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6493 0ebf8283 2019-07-24 stsp goto done;
6496 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6497 0ebf8283 2019-07-24 stsp if (error)
6498 0ebf8283 2019-07-24 stsp goto done;
6500 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6501 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6502 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6503 3e3a69f1 2019-07-25 stsp worktree, repo);
6504 0ebf8283 2019-07-24 stsp if (error)
6505 0ebf8283 2019-07-24 stsp goto done;
6506 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6507 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6508 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6509 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6510 0ebf8283 2019-07-24 stsp if (error)
6511 0ebf8283 2019-07-24 stsp goto done;
6512 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6513 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6514 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6515 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6516 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6517 0ebf8283 2019-07-24 stsp goto done;
6520 0ebf8283 2019-07-24 stsp if (continue_edit) {
6521 0ebf8283 2019-07-24 stsp char *path;
6523 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6524 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6525 0ebf8283 2019-07-24 stsp goto done;
6528 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6529 0ebf8283 2019-07-24 stsp if (error)
6530 0ebf8283 2019-07-24 stsp goto done;
6532 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6533 0ebf8283 2019-07-24 stsp free(path);
6534 0ebf8283 2019-07-24 stsp if (error)
6535 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6538 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6539 3e3a69f1 2019-07-25 stsp worktree, repo);
6540 0ebf8283 2019-07-24 stsp if (error)
6541 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6544 0ebf8283 2019-07-24 stsp if (error)
6545 0ebf8283 2019-07-24 stsp goto done;
6547 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6548 0ebf8283 2019-07-24 stsp head_commit_id);
6549 0ebf8283 2019-07-24 stsp if (error)
6550 0ebf8283 2019-07-24 stsp goto done;
6551 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6552 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6553 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6554 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6555 3aac7cf7 2019-07-25 stsp goto done;
6557 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6558 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6559 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6560 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6561 0ebf8283 2019-07-24 stsp commit = NULL;
6562 0ebf8283 2019-07-24 stsp if (error)
6563 0ebf8283 2019-07-24 stsp goto done;
6565 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6566 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6567 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6571 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6572 0ebf8283 2019-07-24 stsp if (error != NULL)
6573 0ebf8283 2019-07-24 stsp goto done;
6575 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6576 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6577 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6578 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6579 c7d20a3f 2019-07-30 stsp goto done;
6582 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6583 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6584 bfce7f83 2019-07-27 stsp branch = NULL;
6585 0ebf8283 2019-07-24 stsp if (error)
6586 0ebf8283 2019-07-24 stsp goto done;
6588 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6589 0ebf8283 2019-07-24 stsp head_commit_id);
6590 0ebf8283 2019-07-24 stsp if (error)
6591 0ebf8283 2019-07-24 stsp goto done;
6592 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6593 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6594 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6595 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6596 3aac7cf7 2019-07-25 stsp goto done;
6598 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6599 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6600 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6601 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6602 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6603 0ebf8283 2019-07-24 stsp commit = NULL;
6604 0ebf8283 2019-07-24 stsp if (error)
6605 0ebf8283 2019-07-24 stsp goto done;
6607 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6608 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6609 bfce7f83 2019-07-27 stsp if (error)
6610 bfce7f83 2019-07-27 stsp goto done;
6612 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6613 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6614 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6615 6ba2bea2 2019-07-27 stsp if (error) {
6616 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6617 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6618 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6619 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6624 6ba2bea2 2019-07-27 stsp if (error) {
6625 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6626 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6627 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6628 0ebf8283 2019-07-24 stsp goto done;
6633 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6635 6ba2bea2 2019-07-27 stsp if (error) {
6636 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6637 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6638 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6639 0ebf8283 2019-07-24 stsp goto done;
6644 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6645 4ec14e60 2019-08-28 hiltjo if (error)
6646 0ebf8283 2019-07-24 stsp goto done;
6648 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6649 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6650 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6651 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6654 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6655 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6656 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6657 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6660 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6661 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6663 0ebf8283 2019-07-24 stsp if (error)
6664 0ebf8283 2019-07-24 stsp goto done;
6668 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6669 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6670 0ebf8283 2019-07-24 stsp if (error)
6671 0ebf8283 2019-07-24 stsp goto done;
6675 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6676 0ebf8283 2019-07-24 stsp hle->commit_id);
6677 0ebf8283 2019-07-24 stsp if (error)
6678 0ebf8283 2019-07-24 stsp goto done;
6679 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6680 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6682 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6683 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6684 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6685 0ebf8283 2019-07-24 stsp if (error)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6688 0ebf8283 2019-07-24 stsp commit = NULL;
6690 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6691 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6695 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6696 0ebf8283 2019-07-24 stsp char *id_str;
6697 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6698 0ebf8283 2019-07-24 stsp if (error)
6699 0ebf8283 2019-07-24 stsp goto done;
6700 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6702 0ebf8283 2019-07-24 stsp free(id_str);
6703 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6704 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6705 3e3a69f1 2019-07-25 stsp fileindex);
6706 0ebf8283 2019-07-24 stsp goto done;
6709 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6710 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6711 0ebf8283 2019-07-24 stsp if (error)
6712 0ebf8283 2019-07-24 stsp goto done;
6716 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6717 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6718 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6719 0ebf8283 2019-07-24 stsp if (error)
6720 0ebf8283 2019-07-24 stsp goto done;
6723 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6724 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6725 0ebf8283 2019-07-24 stsp if (error)
6726 0ebf8283 2019-07-24 stsp goto done;
6727 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6728 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6730 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6731 3e3a69f1 2019-07-25 stsp branch, repo);
6733 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6734 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6735 0ebf8283 2019-07-24 stsp free(head_commit_id);
6736 0ebf8283 2019-07-24 stsp free(base_commit_id);
6737 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6738 0ebf8283 2019-07-24 stsp if (commit)
6739 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6740 0ebf8283 2019-07-24 stsp if (branch)
6741 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6742 0ebf8283 2019-07-24 stsp if (tmp_branch)
6743 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6744 0ebf8283 2019-07-24 stsp if (worktree)
6745 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6747 2822a352 2019-10-15 stsp got_repo_close(repo);
6748 2822a352 2019-10-15 stsp return error;
6751 2822a352 2019-10-15 stsp __dead static void
6752 2822a352 2019-10-15 stsp usage_integrate(void)
6754 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6758 2822a352 2019-10-15 stsp static const struct got_error *
6759 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6761 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6762 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6763 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6764 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6765 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6766 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6767 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6768 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6769 2822a352 2019-10-15 stsp int ch, did_something = 0;
6771 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6772 2822a352 2019-10-15 stsp switch (ch) {
6774 2822a352 2019-10-15 stsp usage_integrate();
6775 2822a352 2019-10-15 stsp /* NOTREACHED */
6779 2822a352 2019-10-15 stsp argc -= optind;
6780 2822a352 2019-10-15 stsp argv += optind;
6782 2822a352 2019-10-15 stsp if (argc != 1)
6783 2822a352 2019-10-15 stsp usage_integrate();
6784 2822a352 2019-10-15 stsp branch_arg = argv[0];
6786 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6787 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6788 2822a352 2019-10-15 stsp err(1, "pledge");
6790 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6791 2822a352 2019-10-15 stsp if (cwd == NULL) {
6792 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6793 2822a352 2019-10-15 stsp goto done;
6796 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6797 2822a352 2019-10-15 stsp if (error)
6798 2822a352 2019-10-15 stsp goto done;
6800 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6801 2822a352 2019-10-15 stsp if (error)
6802 2822a352 2019-10-15 stsp goto done;
6804 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6806 2822a352 2019-10-15 stsp if (error != NULL)
6807 2822a352 2019-10-15 stsp goto done;
6809 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6810 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6811 2822a352 2019-10-15 stsp if (error)
6812 2822a352 2019-10-15 stsp goto done;
6814 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6815 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6816 2822a352 2019-10-15 stsp goto done;
6819 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6820 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6821 2822a352 2019-10-15 stsp if (error)
6822 2822a352 2019-10-15 stsp goto done;
6824 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6825 2822a352 2019-10-15 stsp if (refname == NULL) {
6826 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6827 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6828 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6829 2822a352 2019-10-15 stsp goto done;
6831 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6832 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6833 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6834 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6835 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6836 2822a352 2019-10-15 stsp goto done;
6839 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6840 2822a352 2019-10-15 stsp if (error)
6841 2822a352 2019-10-15 stsp goto done;
6843 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6844 2822a352 2019-10-15 stsp if (error)
6845 2822a352 2019-10-15 stsp goto done;
6847 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6848 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6849 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6850 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6851 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6852 2822a352 2019-10-15 stsp goto done;
6855 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6856 2822a352 2019-10-15 stsp if (error) {
6857 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6858 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6859 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6860 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6861 2822a352 2019-10-15 stsp goto done;
6864 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6865 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6866 2822a352 2019-10-15 stsp check_cancelled, NULL);
6867 2822a352 2019-10-15 stsp if (error)
6868 2822a352 2019-10-15 stsp goto done;
6870 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6873 715dc77e 2019-08-03 stsp got_repo_close(repo);
6874 2822a352 2019-10-15 stsp if (worktree)
6875 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6876 2822a352 2019-10-15 stsp free(cwd);
6877 2822a352 2019-10-15 stsp free(base_commit_id);
6878 2822a352 2019-10-15 stsp free(commit_id);
6879 2822a352 2019-10-15 stsp free(refname);
6880 2822a352 2019-10-15 stsp free(base_refname);
6881 715dc77e 2019-08-03 stsp return error;
6884 715dc77e 2019-08-03 stsp __dead static void
6885 715dc77e 2019-08-03 stsp usage_stage(void)
6887 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6888 f3055044 2019-08-07 stsp "[file-path ...]\n",
6889 715dc77e 2019-08-03 stsp getprogname());
6893 715dc77e 2019-08-03 stsp static const struct got_error *
6894 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6895 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6896 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6897 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6899 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6900 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6902 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6903 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6904 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6905 408b4ebc 2019-08-03 stsp return NULL;
6907 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6908 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6909 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6911 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6913 408b4ebc 2019-08-03 stsp return err;
6915 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6916 408b4ebc 2019-08-03 stsp free(id_str);
6917 dc424a06 2019-08-07 stsp return NULL;
6920 dc424a06 2019-08-07 stsp static const struct got_error *
6921 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6923 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6924 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6925 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6926 715dc77e 2019-08-03 stsp char *cwd = NULL;
6927 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6928 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6929 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6930 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6931 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6932 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6934 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6936 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6937 715dc77e 2019-08-03 stsp switch (ch) {
6939 408b4ebc 2019-08-03 stsp list_stage = 1;
6942 dc424a06 2019-08-07 stsp pflag = 1;
6945 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6948 715dc77e 2019-08-03 stsp usage_stage();
6949 715dc77e 2019-08-03 stsp /* NOTREACHED */
6953 715dc77e 2019-08-03 stsp argc -= optind;
6954 715dc77e 2019-08-03 stsp argv += optind;
6956 715dc77e 2019-08-03 stsp #ifndef PROFILE
6957 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6958 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6959 715dc77e 2019-08-03 stsp err(1, "pledge");
6961 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6962 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6963 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6964 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6966 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6967 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6968 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6969 715dc77e 2019-08-03 stsp goto done;
6972 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6973 715dc77e 2019-08-03 stsp if (error)
6974 715dc77e 2019-08-03 stsp goto done;
6976 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6978 715dc77e 2019-08-03 stsp if (error != NULL)
6979 715dc77e 2019-08-03 stsp goto done;
6981 dc424a06 2019-08-07 stsp if (patch_script_path) {
6982 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6983 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6984 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6985 dc424a06 2019-08-07 stsp patch_script_path);
6986 dc424a06 2019-08-07 stsp goto done;
6989 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6990 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6991 715dc77e 2019-08-03 stsp if (error)
6992 715dc77e 2019-08-03 stsp goto done;
6994 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6995 6d022e97 2019-08-04 stsp if (error)
6996 6d022e97 2019-08-04 stsp goto done;
6998 6d022e97 2019-08-04 stsp if (list_stage)
6999 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7000 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7002 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7003 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7004 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7005 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7006 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7009 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7010 2e1f37b0 2019-08-08 stsp error == NULL)
7011 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7013 ad493afc 2019-08-03 stsp got_repo_close(repo);
7014 ad493afc 2019-08-03 stsp if (worktree)
7015 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7016 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7017 ad493afc 2019-08-03 stsp free((char *)pe->path);
7018 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7019 ad493afc 2019-08-03 stsp free(cwd);
7020 ad493afc 2019-08-03 stsp return error;
7023 ad493afc 2019-08-03 stsp __dead static void
7024 ad493afc 2019-08-03 stsp usage_unstage(void)
7026 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7027 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7028 ad493afc 2019-08-03 stsp getprogname());
7033 ad493afc 2019-08-03 stsp static const struct got_error *
7034 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7036 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7037 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7038 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7039 ad493afc 2019-08-03 stsp char *cwd = NULL;
7040 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7041 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7042 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7043 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7044 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7045 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7047 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7049 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7050 ad493afc 2019-08-03 stsp switch (ch) {
7052 2e1f37b0 2019-08-08 stsp pflag = 1;
7055 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7058 ad493afc 2019-08-03 stsp usage_unstage();
7059 ad493afc 2019-08-03 stsp /* NOTREACHED */
7063 ad493afc 2019-08-03 stsp argc -= optind;
7064 ad493afc 2019-08-03 stsp argv += optind;
7066 ad493afc 2019-08-03 stsp #ifndef PROFILE
7067 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7068 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7069 ad493afc 2019-08-03 stsp err(1, "pledge");
7071 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7072 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7074 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7075 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7076 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7077 ad493afc 2019-08-03 stsp goto done;
7080 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7081 ad493afc 2019-08-03 stsp if (error)
7082 ad493afc 2019-08-03 stsp goto done;
7084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7086 ad493afc 2019-08-03 stsp if (error != NULL)
7087 ad493afc 2019-08-03 stsp goto done;
7089 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7090 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7091 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7092 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7093 2e1f37b0 2019-08-08 stsp patch_script_path);
7094 2e1f37b0 2019-08-08 stsp goto done;
7098 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7099 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7100 ad493afc 2019-08-03 stsp if (error)
7101 ad493afc 2019-08-03 stsp goto done;
7103 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7104 ad493afc 2019-08-03 stsp if (error)
7105 ad493afc 2019-08-03 stsp goto done;
7107 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7108 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7109 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7110 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7112 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7113 2e1f37b0 2019-08-08 stsp error == NULL)
7114 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7116 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7117 715dc77e 2019-08-03 stsp if (worktree)
7118 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7119 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7120 715dc77e 2019-08-03 stsp free((char *)pe->path);
7121 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7122 715dc77e 2019-08-03 stsp free(cwd);
7123 01073a5d 2019-08-22 stsp return error;
7126 01073a5d 2019-08-22 stsp __dead static void
7127 01073a5d 2019-08-22 stsp usage_cat(void)
7129 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7130 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7134 01073a5d 2019-08-22 stsp static const struct got_error *
7135 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7137 01073a5d 2019-08-22 stsp const struct got_error *err;
7138 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7140 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7142 01073a5d 2019-08-22 stsp return err;
7144 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7145 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7146 01073a5d 2019-08-22 stsp return err;
7149 01073a5d 2019-08-22 stsp static const struct got_error *
7150 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7152 01073a5d 2019-08-22 stsp const struct got_error *err;
7153 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7154 56e0773d 2019-11-28 stsp int nentries, i;
7156 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7158 01073a5d 2019-08-22 stsp return err;
7160 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7161 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7162 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7163 01073a5d 2019-08-22 stsp char *id_str;
7164 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7166 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7167 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7170 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7171 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7172 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7173 01073a5d 2019-08-22 stsp free(id_str);
7176 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7177 01073a5d 2019-08-22 stsp return err;
7180 01073a5d 2019-08-22 stsp static const struct got_error *
7181 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7183 01073a5d 2019-08-22 stsp const struct got_error *err;
7184 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7185 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7186 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7187 01073a5d 2019-08-22 stsp char *id_str = NULL;
7188 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7190 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7192 01073a5d 2019-08-22 stsp return err;
7194 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7196 01073a5d 2019-08-22 stsp goto done;
7198 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7199 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7200 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7201 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7202 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7203 01073a5d 2019-08-22 stsp char *pid_str;
7204 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7206 01073a5d 2019-08-22 stsp goto done;
7207 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7208 01073a5d 2019-08-22 stsp free(pid_str);
7210 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7211 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7212 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7214 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7215 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7216 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7218 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7219 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7220 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7222 01073a5d 2019-08-22 stsp free(id_str);
7223 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7224 01073a5d 2019-08-22 stsp return err;
7227 01073a5d 2019-08-22 stsp static const struct got_error *
7228 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7230 01073a5d 2019-08-22 stsp const struct got_error *err;
7231 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7232 01073a5d 2019-08-22 stsp char *id_str = NULL;
7233 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7235 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7237 01073a5d 2019-08-22 stsp return err;
7239 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7241 01073a5d 2019-08-22 stsp goto done;
7243 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7245 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7246 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7247 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7248 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7250 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7251 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7252 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7254 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7255 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7256 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7258 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7259 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7260 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7266 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7267 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7269 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7270 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7271 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7273 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7274 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7275 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7277 01073a5d 2019-08-22 stsp free(id_str);
7278 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7279 01073a5d 2019-08-22 stsp return err;
7282 01073a5d 2019-08-22 stsp static const struct got_error *
7283 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7285 01073a5d 2019-08-22 stsp const struct got_error *error;
7286 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7287 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7288 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7289 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7290 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7291 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7293 01073a5d 2019-08-22 stsp #ifndef PROFILE
7294 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7295 01073a5d 2019-08-22 stsp NULL) == -1)
7296 01073a5d 2019-08-22 stsp err(1, "pledge");
7299 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7300 01073a5d 2019-08-22 stsp switch (ch) {
7302 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7305 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7306 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7307 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7309 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7312 896e9b6f 2019-08-26 stsp force_path = 1;
7315 01073a5d 2019-08-22 stsp usage_cat();
7316 01073a5d 2019-08-22 stsp /* NOTREACHED */
7320 01073a5d 2019-08-22 stsp argc -= optind;
7321 01073a5d 2019-08-22 stsp argv += optind;
7323 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7324 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7325 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7326 01073a5d 2019-08-22 stsp goto done;
7328 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7329 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7330 01073a5d 2019-08-22 stsp goto done;
7331 01073a5d 2019-08-22 stsp if (worktree) {
7332 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7333 01073a5d 2019-08-22 stsp repo_path = strdup(
7334 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7335 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7336 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7337 01073a5d 2019-08-22 stsp goto done;
7342 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7343 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7344 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7345 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7348 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7349 01073a5d 2019-08-22 stsp free(repo_path);
7350 01073a5d 2019-08-22 stsp if (error != NULL)
7351 01073a5d 2019-08-22 stsp goto done;
7353 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7354 896e9b6f 2019-08-26 stsp if (error)
7355 896e9b6f 2019-08-26 stsp goto done;
7357 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7358 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7359 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7360 01073a5d 2019-08-22 stsp if (error)
7361 01073a5d 2019-08-22 stsp goto done;
7363 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7364 896e9b6f 2019-08-26 stsp if (force_path) {
7365 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7367 896e9b6f 2019-08-26 stsp if (error)
7370 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7371 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7372 896e9b6f 2019-08-26 stsp if (error) {
7373 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7374 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7376 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7377 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7378 896e9b6f 2019-08-26 stsp if (error)
7383 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7384 01073a5d 2019-08-22 stsp if (error)
7387 01073a5d 2019-08-22 stsp switch (obj_type) {
7388 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7389 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7391 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7392 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7394 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7395 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7397 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7398 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7401 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7404 01073a5d 2019-08-22 stsp if (error)
7406 01073a5d 2019-08-22 stsp free(label);
7407 01073a5d 2019-08-22 stsp label = NULL;
7409 01073a5d 2019-08-22 stsp id = NULL;
7413 01073a5d 2019-08-22 stsp free(label);
7415 896e9b6f 2019-08-26 stsp free(commit_id);
7416 01073a5d 2019-08-22 stsp if (worktree)
7417 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7418 01073a5d 2019-08-22 stsp if (repo) {
7419 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7420 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7421 01073a5d 2019-08-22 stsp if (error == NULL)
7422 01073a5d 2019-08-22 stsp error = repo_error;
7424 0ebf8283 2019-07-24 stsp return error;