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 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
60 99437157 2018-11-11 stsp catch_sigint(int signo)
62 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
68 99437157 2018-11-11 stsp sigpipe_received = 1;
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
92 d00136be 2019-03-26 stsp __dead static void usage_add(void);
93 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
94 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
95 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
96 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
97 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
98 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
99 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
100 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
101 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
102 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
103 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
105 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
106 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
108 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
111 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
112 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
114 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
115 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
116 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
117 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
118 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
119 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
120 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
121 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
122 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
123 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
124 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
125 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
126 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
127 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
128 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
130 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
131 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
132 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
133 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
134 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
135 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
136 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
137 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
138 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
139 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
140 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
141 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
142 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
143 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
144 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
145 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
146 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
147 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
149 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
150 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
151 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
152 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
153 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
154 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
157 ce5b7c56 2019-07-09 stsp static void
158 ce5b7c56 2019-07-09 stsp list_commands(void)
162 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
163 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
164 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
167 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
171 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
173 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
174 5c860e29 2018-03-12 stsp unsigned int i;
176 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
178 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
180 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
181 5c860e29 2018-03-12 stsp switch (ch) {
189 ce5b7c56 2019-07-09 stsp usage(hflag);
190 5c860e29 2018-03-12 stsp /* NOTREACHED */
194 5c860e29 2018-03-12 stsp argc -= optind;
195 5c860e29 2018-03-12 stsp argv += optind;
196 1e70621d 2018-03-27 stsp optind = 0;
198 53ccebc2 2019-07-30 stsp if (Vflag) {
199 53ccebc2 2019-07-30 stsp got_version_print_str();
203 5c860e29 2018-03-12 stsp if (argc <= 0)
204 ce5b7c56 2019-07-09 stsp usage(hflag);
206 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
207 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
209 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
210 d7d4f210 2018-03-12 stsp const struct got_error *error;
212 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
214 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
219 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
221 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
222 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
223 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
231 ce5b7c56 2019-07-09 stsp list_commands();
235 4ed7e80c 2018-05-20 stsp __dead static void
236 ce5b7c56 2019-07-09 stsp usage(int hflag)
238 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 53ccebc2 2019-07-30 stsp getprogname());
241 ce5b7c56 2019-07-09 stsp list_commands();
245 0266afb7 2019-01-04 stsp static const struct got_error *
246 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
248 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
249 e2ba3d07 2019-05-13 stsp const char *editor;
251 8920fa04 2019-08-18 stsp *abspath = NULL;
253 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
254 e2ba3d07 2019-05-13 stsp if (editor == NULL)
255 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
257 0ee7065d 2019-05-13 stsp if (editor) {
258 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
260 0ee7065d 2019-05-13 stsp return err;
263 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
264 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
265 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
266 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
269 e2ba3d07 2019-05-13 stsp return NULL;
272 e2ba3d07 2019-05-13 stsp static const struct got_error *
273 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
274 c530dc23 2019-07-23 stsp const char *worktree_path)
276 163ce85a 2019-05-13 stsp const struct got_error *err;
278 37c06ea4 2019-07-15 stsp #ifdef PROFILE
279 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
280 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
282 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
285 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
288 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
289 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
291 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
292 163ce85a 2019-05-13 stsp if (err != NULL)
293 163ce85a 2019-05-13 stsp return err;
295 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
296 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
298 0266afb7 2019-01-04 stsp return NULL;
301 2c7829a4 2019-06-17 stsp __dead static void
302 2c7829a4 2019-06-17 stsp usage_init(void)
304 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
308 2c7829a4 2019-06-17 stsp static const struct got_error *
309 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
311 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
312 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
315 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
316 2c7829a4 2019-06-17 stsp switch (ch) {
318 2c7829a4 2019-06-17 stsp usage_init();
319 2c7829a4 2019-06-17 stsp /* NOTREACHED */
323 2c7829a4 2019-06-17 stsp argc -= optind;
324 2c7829a4 2019-06-17 stsp argv += optind;
326 2c7829a4 2019-06-17 stsp #ifndef PROFILE
327 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
328 2c7829a4 2019-06-17 stsp err(1, "pledge");
330 2c7829a4 2019-06-17 stsp if (argc != 1)
331 bc20e173 2019-06-17 stsp usage_init();
333 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
334 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
335 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
337 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
339 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
340 2c7829a4 2019-06-17 stsp if (error &&
341 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
344 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
348 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
349 2c7829a4 2019-06-17 stsp if (error != NULL)
353 3ce1b845 2019-07-15 stsp free(repo_path);
354 3ce1b845 2019-07-15 stsp return error;
357 3ce1b845 2019-07-15 stsp __dead static void
358 3ce1b845 2019-07-15 stsp usage_import(void)
360 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
366 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
369 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
370 3ce1b845 2019-07-15 stsp int st = -1;
372 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
373 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
374 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
376 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
378 3ce1b845 2019-07-15 stsp goto doneediting;
380 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
381 3ce1b845 2019-07-15 stsp _exit(127);
384 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
385 3ce1b845 2019-07-15 stsp if (errno != EINTR)
388 3ce1b845 2019-07-15 stsp doneediting:
389 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
390 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
391 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
393 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
394 3ce1b845 2019-07-15 stsp errno = EINTR;
398 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
401 3ce1b845 2019-07-15 stsp static const struct got_error *
402 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 3ce1b845 2019-07-15 stsp const char *initial_content)
405 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
406 3ce1b845 2019-07-15 stsp char buf[1024];
407 3ce1b845 2019-07-15 stsp struct stat st, st2;
409 3ce1b845 2019-07-15 stsp int content_changed = 0;
410 3ce1b845 2019-07-15 stsp size_t len;
412 3ce1b845 2019-07-15 stsp *logmsg = NULL;
414 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
415 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
417 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
418 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
420 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
421 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
423 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
427 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
428 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
430 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
433 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
434 3ce1b845 2019-07-15 stsp if (fp == NULL) {
435 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
438 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
439 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
440 3ce1b845 2019-07-15 stsp content_changed = 1;
441 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
443 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
445 3ce1b845 2019-07-15 stsp fclose(fp);
447 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
452 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
453 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
457 3ce1b845 2019-07-15 stsp free(*logmsg);
458 3ce1b845 2019-07-15 stsp *logmsg = NULL;
460 3ce1b845 2019-07-15 stsp return err;
463 3ce1b845 2019-07-15 stsp static const struct got_error *
464 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
465 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
467 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
468 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
471 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
472 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
473 3ce1b845 2019-07-15 stsp branch_name) == -1)
474 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
476 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
480 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
483 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
485 3ce1b845 2019-07-15 stsp free(initial_content);
486 3ce1b845 2019-07-15 stsp return err;
489 3ce1b845 2019-07-15 stsp static const struct got_error *
490 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
492 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
493 3ce1b845 2019-07-15 stsp return NULL;
496 3ce1b845 2019-07-15 stsp static const struct got_error *
497 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
499 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
500 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
502 84792843 2019-08-09 stsp *author = NULL;
504 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
505 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
506 c9956ddf 2019-09-08 stsp if (name && email) {
507 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
508 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
509 aba9c984 2019-09-08 stsp return NULL;
512 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
513 84792843 2019-08-09 stsp if (got_author == NULL) {
514 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
515 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
516 c9956ddf 2019-09-08 stsp if (name && email) {
517 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
518 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
519 c9956ddf 2019-09-08 stsp return NULL;
521 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
522 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
525 aba9c984 2019-09-08 stsp *author = strdup(got_author);
526 aba9c984 2019-09-08 stsp if (*author == NULL)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
530 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
531 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
533 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
534 84792843 2019-08-09 stsp got_author++;
535 aba9c984 2019-09-08 stsp if (*got_author != '<') {
536 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
539 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
540 84792843 2019-08-09 stsp got_author++;
541 aba9c984 2019-09-08 stsp if (*got_author != '@') {
542 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
545 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
546 84792843 2019-08-09 stsp got_author++;
547 84792843 2019-08-09 stsp if (*got_author != '>')
548 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
551 aba9c984 2019-09-08 stsp free(*author);
552 aba9c984 2019-09-08 stsp *author = NULL;
554 aba9c984 2019-09-08 stsp return err;
557 c9956ddf 2019-09-08 stsp static const struct got_error *
558 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
560 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
562 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
563 c9956ddf 2019-09-08 stsp if (homedir) {
564 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
565 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
568 c9956ddf 2019-09-08 stsp return NULL;
571 84792843 2019-08-09 stsp static const struct got_error *
572 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
574 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
575 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
576 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
577 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
578 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
579 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
580 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
581 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
583 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
584 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
585 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
587 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
589 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
590 3ce1b845 2019-07-15 stsp switch (ch) {
592 3ce1b845 2019-07-15 stsp branch_name = optarg;
595 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
596 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
597 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
602 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
603 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
604 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
610 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
612 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
618 b2b65d18 2019-08-22 stsp usage_import();
619 3ce1b845 2019-07-15 stsp /* NOTREACHED */
623 3ce1b845 2019-07-15 stsp argc -= optind;
624 3ce1b845 2019-07-15 stsp argv += optind;
626 3ce1b845 2019-07-15 stsp #ifndef PROFILE
627 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
629 3ce1b845 2019-07-15 stsp NULL) == -1)
630 3ce1b845 2019-07-15 stsp err(1, "pledge");
632 3ce1b845 2019-07-15 stsp if (argc != 1)
633 3ce1b845 2019-07-15 stsp usage_import();
635 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
636 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
637 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
638 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
640 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
641 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
644 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
648 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
650 aba9c984 2019-09-08 stsp return error;
652 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
653 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
657 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
658 3ce1b845 2019-07-15 stsp if (error) {
659 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
662 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
663 3ce1b845 2019-07-15 stsp "import target branch already exists");
667 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
668 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
669 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
672 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
675 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
676 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
678 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
679 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
682 8e158b01 2019-09-22 stsp free(logmsg);
683 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
684 ef293bdd 2019-10-21 stsp path_dir, refname);
685 ef293bdd 2019-10-21 stsp if (error) {
686 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
687 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
688 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
693 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
694 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
695 ef293bdd 2019-10-21 stsp if (logmsg_path)
696 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
700 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
701 ef293bdd 2019-10-21 stsp if (error) {
702 ef293bdd 2019-10-21 stsp if (logmsg_path)
703 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
707 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
708 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
709 ef293bdd 2019-10-21 stsp if (error) {
710 ef293bdd 2019-10-21 stsp if (logmsg_path)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
715 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
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_ref_write(branch_ref, repo);
723 ef293bdd 2019-10-21 stsp if (error) {
724 ef293bdd 2019-10-21 stsp if (logmsg_path)
725 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
729 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
730 ef293bdd 2019-10-21 stsp if (error) {
731 ef293bdd 2019-10-21 stsp if (logmsg_path)
732 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
736 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
737 3ce1b845 2019-07-15 stsp if (error) {
738 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
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_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
745 3ce1b845 2019-07-15 stsp branch_ref);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
752 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
761 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
763 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
764 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
765 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
766 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
767 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
768 8e158b01 2019-09-22 stsp free(logmsg);
769 ef293bdd 2019-10-21 stsp free(logmsg_path);
770 2c7829a4 2019-06-17 stsp free(repo_path);
771 3ce1b845 2019-07-15 stsp free(editor);
772 3ce1b845 2019-07-15 stsp free(refname);
773 3ce1b845 2019-07-15 stsp free(new_commit_id);
774 3ce1b845 2019-07-15 stsp free(id_str);
775 aba9c984 2019-09-08 stsp free(author);
776 c9956ddf 2019-09-08 stsp free(gitconfig_path);
777 3ce1b845 2019-07-15 stsp if (branch_ref)
778 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
779 3ce1b845 2019-07-15 stsp if (head_ref)
780 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
781 2c7829a4 2019-06-17 stsp return error;
784 4ed7e80c 2018-05-20 stsp __dead static void
785 c09a553d 2018-03-12 stsp usage_checkout(void)
787 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
788 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
792 1ee397ad 2019-07-12 stsp static const struct got_error *
793 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
795 92a684f4 2018-03-12 stsp char *worktree_path = arg;
797 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
798 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
799 1ee397ad 2019-07-12 stsp return NULL;
801 92a684f4 2018-03-12 stsp while (path[0] == '/')
804 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
805 1ee397ad 2019-07-12 stsp return NULL;
808 99437157 2018-11-11 stsp static const struct got_error *
809 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
811 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
812 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
813 99437157 2018-11-11 stsp return NULL;
816 8069f636 2019-01-12 stsp static const struct got_error *
817 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
818 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
819 3aef623b 2019-10-15 stsp struct got_repository *repo)
821 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
822 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
824 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
825 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
827 36a38700 2019-05-10 stsp return err;
829 d5bea539 2019-05-13 stsp if (yca_id == NULL)
830 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
833 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
834 d5bea539 2019-05-13 stsp * and the work tree's base commit.
836 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
838 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
846 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
847 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
848 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
850 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
851 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
852 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
853 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
854 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
855 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
857 d5bea539 2019-05-13 stsp free(yca_id);
858 d5bea539 2019-05-13 stsp return NULL;
861 a367ff0f 2019-05-14 stsp static const struct got_error *
862 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
863 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
864 a51a74b3 2019-07-27 stsp struct got_repository *repo)
866 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
867 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
868 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
869 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
871 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
875 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
876 a51a74b3 2019-07-27 stsp is_same_branch = 1;
879 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
880 a51a74b3 2019-07-27 stsp is_same_branch = 1;
884 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
888 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
889 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
894 a367ff0f 2019-05-14 stsp struct got_object_id *id;
895 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
897 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
898 a367ff0f 2019-05-14 stsp err = NULL;
900 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
902 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
903 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
909 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
911 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
912 a367ff0f 2019-05-14 stsp is_same_branch = 1;
919 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
920 a367ff0f 2019-05-14 stsp free(head_commit_id);
921 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
922 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
923 04f57cb3 2019-07-25 stsp return err;
926 04f57cb3 2019-07-25 stsp static const struct got_error *
927 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
928 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
930 04f57cb3 2019-07-25 stsp const struct got_error *err;
931 04f57cb3 2019-07-25 stsp struct got_reference *ref;
932 303e2782 2019-08-09 stsp struct got_tag_object *tag;
934 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
935 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
936 303e2782 2019-08-09 stsp if (err == NULL) {
937 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
938 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
939 303e2782 2019-08-09 stsp if (*commit_id == NULL)
940 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
941 303e2782 2019-08-09 stsp got_object_tag_close(tag);
942 303e2782 2019-08-09 stsp return err;
943 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
944 303e2782 2019-08-09 stsp return err;
946 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
947 04f57cb3 2019-07-25 stsp if (err == NULL) {
948 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
949 04f57cb3 2019-07-25 stsp got_ref_close(ref);
951 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
952 04f57cb3 2019-07-25 stsp return err;
953 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
954 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
956 a367ff0f 2019-05-14 stsp return err;
959 4ed7e80c 2018-05-20 stsp static const struct got_error *
960 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
962 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
963 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
964 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
965 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
966 c09a553d 2018-03-12 stsp char *repo_path = NULL;
967 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
968 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
969 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
970 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
971 72151b04 2019-05-11 stsp int ch, same_path_prefix;
972 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
974 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
976 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
977 0bb8a95e 2018-03-12 stsp switch (ch) {
979 08573d5b 2019-05-14 stsp branch_name = optarg;
982 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
983 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
984 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
987 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
990 2deda0b9 2019-03-07 stsp usage_checkout();
991 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
995 0bb8a95e 2018-03-12 stsp argc -= optind;
996 0bb8a95e 2018-03-12 stsp argv += optind;
998 6715a751 2018-03-16 stsp #ifndef PROFILE
999 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1000 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1001 c09a553d 2018-03-12 stsp err(1, "pledge");
1003 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1004 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1005 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1006 76089277 2018-04-01 stsp if (repo_path == NULL)
1007 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1008 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1009 76089277 2018-04-01 stsp if (cwd == NULL) {
1010 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1011 76089277 2018-04-01 stsp goto done;
1013 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1014 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1015 230a42bd 2019-05-11 jcs if (base == NULL) {
1016 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1017 230a42bd 2019-05-11 jcs path_prefix);
1021 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1022 230a42bd 2019-05-11 jcs if (base == NULL) {
1023 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1024 230a42bd 2019-05-11 jcs repo_path);
1028 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1029 c09a553d 2018-03-12 stsp if (dotgit)
1030 c09a553d 2018-03-12 stsp *dotgit = '\0';
1031 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1032 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1033 c09a553d 2018-03-12 stsp free(cwd);
1034 76089277 2018-04-01 stsp goto done;
1036 c09a553d 2018-03-12 stsp free(cwd);
1037 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1038 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1039 76089277 2018-04-01 stsp if (repo_path == NULL) {
1040 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1041 76089277 2018-04-01 stsp goto done;
1043 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1044 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1045 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1046 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1048 b4b3a7dd 2019-07-22 stsp goto done;
1050 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1051 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1052 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1053 b4b3a7dd 2019-07-22 stsp goto done;
1057 c09a553d 2018-03-12 stsp usage_checkout();
1059 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1060 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1062 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1063 c09a553d 2018-03-12 stsp if (error != NULL)
1064 c02c541e 2019-03-29 stsp goto done;
1066 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1067 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1068 c530dc23 2019-07-23 stsp if (error) {
1069 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1070 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1071 c530dc23 2019-07-23 stsp goto done;
1072 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1073 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1074 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1075 c530dc23 2019-07-23 stsp goto done;
1079 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1080 c02c541e 2019-03-29 stsp if (error)
1081 c09a553d 2018-03-12 stsp goto done;
1083 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1084 c09a553d 2018-03-12 stsp if (error != NULL)
1085 c09a553d 2018-03-12 stsp goto done;
1087 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1088 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1089 c09a553d 2018-03-12 stsp goto done;
1091 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1092 c09a553d 2018-03-12 stsp if (error != NULL)
1093 c09a553d 2018-03-12 stsp goto done;
1095 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1096 e5dc7198 2018-12-29 stsp path_prefix);
1097 e5dc7198 2018-12-29 stsp if (error != NULL)
1098 e5dc7198 2018-12-29 stsp goto done;
1099 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1100 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1101 49520a32 2018-12-29 stsp goto done;
1104 8069f636 2019-01-12 stsp if (commit_id_str) {
1105 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1106 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1107 30837e32 2019-07-25 stsp if (error)
1108 8069f636 2019-01-12 stsp goto done;
1109 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1110 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1111 8069f636 2019-01-12 stsp if (error != NULL) {
1112 8069f636 2019-01-12 stsp free(commit_id);
1113 8069f636 2019-01-12 stsp goto done;
1115 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1116 45d344f6 2019-05-14 stsp if (error)
1117 45d344f6 2019-05-14 stsp goto done;
1118 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1119 8069f636 2019-01-12 stsp commit_id);
1120 8069f636 2019-01-12 stsp free(commit_id);
1121 8069f636 2019-01-12 stsp if (error)
1122 8069f636 2019-01-12 stsp goto done;
1125 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1126 f2ea84fa 2019-07-27 stsp if (error)
1127 f2ea84fa 2019-07-27 stsp goto done;
1128 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1129 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1130 c09a553d 2018-03-12 stsp if (error != NULL)
1131 c09a553d 2018-03-12 stsp goto done;
1133 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1136 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1137 8069f636 2019-01-12 stsp free(commit_id_str);
1138 76089277 2018-04-01 stsp free(repo_path);
1139 507dc3bb 2018-12-29 stsp free(worktree_path);
1140 507dc3bb 2018-12-29 stsp return error;
1143 507dc3bb 2018-12-29 stsp __dead static void
1144 507dc3bb 2018-12-29 stsp usage_update(void)
1146 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1147 507dc3bb 2018-12-29 stsp getprogname());
1151 1ee397ad 2019-07-12 stsp static const struct got_error *
1152 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1154 784955db 2019-01-12 stsp int *did_something = arg;
1156 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1157 1ee397ad 2019-07-12 stsp return NULL;
1159 1545c615 2019-02-10 stsp *did_something = 1;
1161 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1162 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1163 1ee397ad 2019-07-12 stsp return NULL;
1165 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1167 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1168 1ee397ad 2019-07-12 stsp return NULL;
1171 be7061eb 2018-12-30 stsp static const struct got_error *
1172 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1173 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1174 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1176 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1177 a1fb16d8 2019-05-24 stsp char *base_id_str;
1178 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1180 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1181 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1182 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1183 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1184 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1185 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1186 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1189 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1190 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1191 a1fb16d8 2019-05-24 stsp if (err) {
1192 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1193 a1fb16d8 2019-05-24 stsp return err;
1194 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1196 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1197 a1fb16d8 2019-05-24 stsp return NULL;
1199 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1200 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1201 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1203 a1fb16d8 2019-05-24 stsp return err;
1204 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1205 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1206 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1207 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1208 0ebf8283 2019-07-24 stsp return NULL;
1211 0ebf8283 2019-07-24 stsp static const struct got_error *
1212 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1214 0ebf8283 2019-07-24 stsp const struct got_error *err;
1215 0ebf8283 2019-07-24 stsp int in_progress;
1217 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1219 0ebf8283 2019-07-24 stsp return err;
1220 0ebf8283 2019-07-24 stsp if (in_progress)
1221 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1223 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1225 0ebf8283 2019-07-24 stsp return err;
1226 0ebf8283 2019-07-24 stsp if (in_progress)
1227 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1229 a1fb16d8 2019-05-24 stsp return NULL;
1232 a5edda0a 2019-07-27 stsp static const struct got_error *
1233 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1234 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1236 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1237 a5edda0a 2019-07-27 stsp char *path;
1240 a5edda0a 2019-07-27 stsp if (argc == 0) {
1241 a5edda0a 2019-07-27 stsp path = strdup("");
1242 a5edda0a 2019-07-27 stsp if (path == NULL)
1243 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1244 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1247 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1248 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1251 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1252 a5edda0a 2019-07-27 stsp if (err) {
1253 a5edda0a 2019-07-27 stsp free(path);
1258 a5edda0a 2019-07-27 stsp return err;
1261 a1fb16d8 2019-05-24 stsp static const struct got_error *
1262 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1264 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1265 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1266 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1267 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1268 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1269 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1270 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1271 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1272 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1273 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1274 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1276 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1278 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1279 507dc3bb 2018-12-29 stsp switch (ch) {
1281 024e9686 2019-05-14 stsp branch_name = optarg;
1284 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1285 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1286 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1289 2deda0b9 2019-03-07 stsp usage_update();
1290 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1294 507dc3bb 2018-12-29 stsp argc -= optind;
1295 507dc3bb 2018-12-29 stsp argv += optind;
1297 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1298 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1299 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1300 507dc3bb 2018-12-29 stsp err(1, "pledge");
1302 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1303 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1304 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1305 c4cdcb68 2019-04-03 stsp goto done;
1307 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1308 7d5807f4 2019-07-11 stsp if (error)
1309 7d5807f4 2019-07-11 stsp goto done;
1311 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1312 f2ea84fa 2019-07-27 stsp if (error)
1313 f2ea84fa 2019-07-27 stsp goto done;
1315 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1317 507dc3bb 2018-12-29 stsp if (error != NULL)
1318 507dc3bb 2018-12-29 stsp goto done;
1320 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1321 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1322 087fb88c 2019-08-04 stsp if (error)
1323 087fb88c 2019-08-04 stsp goto done;
1325 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1326 0266afb7 2019-01-04 stsp if (error)
1327 0266afb7 2019-01-04 stsp goto done;
1329 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1330 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1331 024e9686 2019-05-14 stsp if (error != NULL)
1332 024e9686 2019-05-14 stsp goto done;
1333 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1334 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1335 9c4b8182 2019-01-02 stsp if (error != NULL)
1336 9c4b8182 2019-01-02 stsp goto done;
1337 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1338 507dc3bb 2018-12-29 stsp if (error != NULL)
1339 507dc3bb 2018-12-29 stsp goto done;
1341 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1342 04f57cb3 2019-07-25 stsp free(commit_id_str);
1343 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1344 30837e32 2019-07-25 stsp if (error)
1345 a297e751 2019-07-11 stsp goto done;
1346 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1347 a297e751 2019-07-11 stsp if (error)
1348 507dc3bb 2018-12-29 stsp goto done;
1351 a1fb16d8 2019-05-24 stsp if (branch_name) {
1352 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1353 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1354 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1356 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1357 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1358 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1359 024e9686 2019-05-14 stsp goto done;
1361 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1362 024e9686 2019-05-14 stsp if (error)
1363 024e9686 2019-05-14 stsp goto done;
1364 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1366 a367ff0f 2019-05-14 stsp free(head_commit_id);
1367 024e9686 2019-05-14 stsp if (error != NULL)
1368 024e9686 2019-05-14 stsp goto done;
1369 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1370 a367ff0f 2019-05-14 stsp if (error)
1371 a367ff0f 2019-05-14 stsp goto done;
1372 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1373 024e9686 2019-05-14 stsp if (error)
1374 024e9686 2019-05-14 stsp goto done;
1376 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1377 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1378 a367ff0f 2019-05-14 stsp if (error != NULL) {
1379 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1380 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1381 024e9686 2019-05-14 stsp goto done;
1383 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 a367ff0f 2019-05-14 stsp if (error)
1385 a367ff0f 2019-05-14 stsp goto done;
1388 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1389 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1390 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1391 507dc3bb 2018-12-29 stsp commit_id);
1392 507dc3bb 2018-12-29 stsp if (error)
1393 507dc3bb 2018-12-29 stsp goto done;
1396 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1397 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1398 507dc3bb 2018-12-29 stsp if (error != NULL)
1399 507dc3bb 2018-12-29 stsp goto done;
1401 784955db 2019-01-12 stsp if (did_something)
1402 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1404 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1406 c09a553d 2018-03-12 stsp free(worktree_path);
1407 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1408 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1409 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1410 507dc3bb 2018-12-29 stsp free(commit_id);
1411 9c4b8182 2019-01-02 stsp free(commit_id_str);
1412 c09a553d 2018-03-12 stsp return error;
1415 f42b1b34 2018-03-12 stsp static const struct got_error *
1416 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1417 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1418 63035f9f 2019-10-06 stsp struct got_repository *repo)
1420 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1421 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1423 44392932 2019-08-25 stsp if (blob_id1) {
1424 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1426 44392932 2019-08-25 stsp goto done;
1429 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1431 44392932 2019-08-25 stsp goto done;
1433 44392932 2019-08-25 stsp while (path[0] == '/')
1435 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1436 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1438 44392932 2019-08-25 stsp if (blob1)
1439 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1440 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1441 44392932 2019-08-25 stsp return err;
1444 44392932 2019-08-25 stsp static const struct got_error *
1445 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1446 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1447 63035f9f 2019-10-06 stsp struct got_repository *repo)
1449 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1450 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1453 44392932 2019-08-25 stsp if (tree_id1) {
1454 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1456 44392932 2019-08-25 stsp goto done;
1459 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1461 0f2b3dca 2018-12-22 stsp goto done;
1463 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1464 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1465 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1466 44392932 2019-08-25 stsp while (path[0] == '/')
1468 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1469 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1471 79109fed 2018-03-27 stsp if (tree1)
1472 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1473 366e0a5f 2019-10-10 stsp if (tree2)
1474 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1475 44392932 2019-08-25 stsp return err;
1478 44392932 2019-08-25 stsp static const struct got_error *
1479 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1480 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1482 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1483 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1484 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1485 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1486 44392932 2019-08-25 stsp struct got_object_qid *qid;
1488 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1489 44392932 2019-08-25 stsp if (qid != NULL) {
1490 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1493 44392932 2019-08-25 stsp return err;
1496 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1497 44392932 2019-08-25 stsp int obj_type;
1498 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1500 44392932 2019-08-25 stsp goto done;
1501 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1502 44392932 2019-08-25 stsp if (err) {
1503 44392932 2019-08-25 stsp free(obj_id2);
1504 44392932 2019-08-25 stsp goto done;
1506 44392932 2019-08-25 stsp if (pcommit) {
1507 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1508 44392932 2019-08-25 stsp qid->id, path);
1509 44392932 2019-08-25 stsp if (err) {
1510 44392932 2019-08-25 stsp free(obj_id2);
1511 44392932 2019-08-25 stsp goto done;
1513 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1514 44392932 2019-08-25 stsp if (err) {
1515 44392932 2019-08-25 stsp free(obj_id2);
1516 44392932 2019-08-25 stsp goto done;
1519 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1520 44392932 2019-08-25 stsp if (err) {
1521 44392932 2019-08-25 stsp free(obj_id2);
1522 44392932 2019-08-25 stsp goto done;
1524 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1525 44392932 2019-08-25 stsp switch (obj_type) {
1526 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1527 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1530 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1531 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1535 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1538 44392932 2019-08-25 stsp free(obj_id1);
1539 44392932 2019-08-25 stsp free(obj_id2);
1541 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1542 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1544 44392932 2019-08-25 stsp goto done;
1545 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1546 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1548 44392932 2019-08-25 stsp goto done;
1549 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1550 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1554 0f2b3dca 2018-12-22 stsp free(id_str1);
1555 0f2b3dca 2018-12-22 stsp free(id_str2);
1556 44392932 2019-08-25 stsp if (pcommit)
1557 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1558 79109fed 2018-03-27 stsp return err;
1561 4bb494d5 2018-06-16 stsp static char *
1562 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1564 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1565 09867e48 2019-08-13 stsp char *p, *s;
1567 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1568 09867e48 2019-08-13 stsp if (tm == NULL)
1569 09867e48 2019-08-13 stsp return NULL;
1570 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1571 09867e48 2019-08-13 stsp if (s == NULL)
1572 09867e48 2019-08-13 stsp return NULL;
1573 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1575 6c281f94 2018-06-11 stsp *p = '\0';
1579 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1581 79109fed 2018-03-27 stsp static const struct got_error *
1582 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1583 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1584 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1586 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1587 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1588 6c281f94 2018-06-11 stsp char datebuf[26];
1589 45d799e2 2018-12-23 stsp time_t committer_time;
1590 45d799e2 2018-12-23 stsp const char *author, *committer;
1591 199a4027 2019-02-02 stsp char *refs_str = NULL;
1592 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1594 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1596 199a4027 2019-02-02 stsp const char *name;
1597 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1600 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1601 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1603 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1604 199a4027 2019-02-02 stsp name += 5;
1605 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1607 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1608 e34f9ed6 2019-02-02 stsp name += 6;
1609 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1610 141c2bff 2019-02-04 stsp name += 8;
1611 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1612 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1613 5d844a1e 2019-08-13 stsp if (err) {
1614 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1615 5d844a1e 2019-08-13 stsp return err;
1616 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1617 5d844a1e 2019-08-13 stsp err = NULL;
1618 5d844a1e 2019-08-13 stsp tag = NULL;
1621 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1622 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1624 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1625 a436ad14 2019-08-13 stsp if (cmp != 0)
1627 199a4027 2019-02-02 stsp s = refs_str;
1628 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1629 199a4027 2019-02-02 stsp name) == -1) {
1630 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1632 34ca4898 2019-08-13 stsp return err;
1636 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1638 8bf5b3c9 2018-03-17 stsp return err;
1640 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1641 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1642 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1643 832c249c 2018-06-10 stsp free(id_str);
1644 d3d493d7 2019-02-21 stsp id_str = NULL;
1645 d3d493d7 2019-02-21 stsp free(refs_str);
1646 d3d493d7 2019-02-21 stsp refs_str = NULL;
1647 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1648 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1649 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1650 09867e48 2019-08-13 stsp if (datestr)
1651 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1652 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1653 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1654 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1655 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1656 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1657 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1658 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1659 3fe1abad 2018-06-10 stsp int n = 1;
1660 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1661 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1662 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1664 a0603db2 2018-06-10 stsp return err;
1665 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1666 a0603db2 2018-06-10 stsp free(id_str);
1670 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1672 5943eee2 2019-08-13 stsp return err;
1674 621015ac 2018-07-23 stsp logmsg = logmsg0;
1676 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1678 832c249c 2018-06-10 stsp printf(" %s\n", line);
1679 832c249c 2018-06-10 stsp } while (line);
1680 621015ac 2018-07-23 stsp free(logmsg0);
1682 971751ac 2018-03-27 stsp if (show_patch) {
1683 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1684 971751ac 2018-03-27 stsp if (err == 0)
1685 971751ac 2018-03-27 stsp printf("\n");
1688 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1689 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1690 07862c20 2018-09-15 stsp return err;
1693 f42b1b34 2018-03-12 stsp static const struct got_error *
1694 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1695 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1696 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1698 f42b1b34 2018-03-12 stsp const struct got_error *err;
1699 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1701 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1702 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1704 f42b1b34 2018-03-12 stsp return err;
1705 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1706 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1708 fcc85cad 2018-07-22 stsp goto done;
1710 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1711 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1713 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1716 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1717 372ccdbb 2018-06-10 stsp if (err) {
1718 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1719 9ba79e04 2018-06-11 stsp err = NULL;
1722 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1724 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1725 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1731 b43fbaa0 2018-06-11 stsp if (id == NULL)
1734 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1737 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1738 44392932 2019-08-25 stsp diff_context, refs);
1739 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1740 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1744 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1745 f42b1b34 2018-03-12 stsp return err;
1748 4ed7e80c 2018-05-20 stsp __dead static void
1749 6f3d1eb0 2018-03-12 stsp usage_log(void)
1751 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1752 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1756 b1ebc001 2019-08-13 stsp static int
1757 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1759 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1760 b1ebc001 2019-08-13 stsp long long n;
1761 b1ebc001 2019-08-13 stsp const char *errstr;
1763 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1764 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1766 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1767 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1772 4ed7e80c 2018-05-20 stsp static const struct got_error *
1773 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1775 f42b1b34 2018-03-12 stsp const struct got_error *error;
1776 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1777 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1778 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1779 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1780 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1781 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1782 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1783 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1784 64a96a6d 2018-04-01 stsp const char *errstr;
1785 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1787 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1789 6715a751 2018-03-16 stsp #ifndef PROFILE
1790 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1793 f42b1b34 2018-03-12 stsp err(1, "pledge");
1796 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1798 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1799 79109fed 2018-03-27 stsp switch (ch) {
1801 79109fed 2018-03-27 stsp show_patch = 1;
1804 d142fc45 2018-04-01 stsp start_commit = optarg;
1807 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1809 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1810 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1813 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1814 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1815 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1818 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1821 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1822 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1823 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1825 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1828 2deda0b9 2019-03-07 stsp usage_log();
1829 79109fed 2018-03-27 stsp /* NOTREACHED */
1833 79109fed 2018-03-27 stsp argc -= optind;
1834 79109fed 2018-03-27 stsp argv += optind;
1836 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1837 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1838 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1839 04ca23f4 2018-07-16 stsp goto done;
1842 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1843 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1844 cffc0aa4 2019-02-05 stsp goto done;
1845 cffc0aa4 2019-02-05 stsp error = NULL;
1847 e7301579 2019-03-18 stsp if (argc == 0) {
1848 cbd1af7a 2019-03-18 stsp path = strdup("");
1849 e7301579 2019-03-18 stsp if (path == NULL) {
1850 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1851 cbd1af7a 2019-03-18 stsp goto done;
1853 e7301579 2019-03-18 stsp } else if (argc == 1) {
1854 e7301579 2019-03-18 stsp if (worktree) {
1855 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1857 e7301579 2019-03-18 stsp if (error)
1858 e7301579 2019-03-18 stsp goto done;
1860 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1861 e7301579 2019-03-18 stsp if (path == NULL) {
1862 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1863 e7301579 2019-03-18 stsp goto done;
1867 cbd1af7a 2019-03-18 stsp usage_log();
1869 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1870 5486daa2 2019-05-11 stsp repo_path = worktree ?
1871 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1873 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1874 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1875 cffc0aa4 2019-02-05 stsp goto done;
1878 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1879 d7d4f210 2018-03-12 stsp if (error != NULL)
1880 04ca23f4 2018-07-16 stsp goto done;
1882 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1883 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1884 c02c541e 2019-03-29 stsp if (error)
1885 c02c541e 2019-03-29 stsp goto done;
1887 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1888 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1889 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1890 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1891 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1892 3235492e 2018-04-01 stsp if (error != NULL)
1893 3235492e 2018-04-01 stsp return error;
1894 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1895 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1896 3235492e 2018-04-01 stsp if (error != NULL)
1897 3235492e 2018-04-01 stsp return error;
1898 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1900 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1901 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1902 3235492e 2018-04-01 stsp if (error == NULL) {
1903 1caad61b 2019-02-01 stsp int obj_type;
1904 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1905 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1906 d1f2edc9 2018-06-13 stsp if (error != NULL)
1907 1caad61b 2019-02-01 stsp goto done;
1908 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1909 1caad61b 2019-02-01 stsp if (error != NULL)
1910 1caad61b 2019-02-01 stsp goto done;
1911 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1912 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1913 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1914 1caad61b 2019-02-01 stsp if (error != NULL)
1915 1caad61b 2019-02-01 stsp goto done;
1916 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1917 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1918 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1919 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1920 1caad61b 2019-02-01 stsp goto done;
1923 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1924 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1925 1caad61b 2019-02-01 stsp if (id == NULL)
1926 638f9024 2019-05-13 stsp error = got_error_from_errno(
1927 230a42bd 2019-05-11 jcs "got_object_id_dup");
1928 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1929 1caad61b 2019-02-01 stsp if (error)
1930 1caad61b 2019-02-01 stsp goto done;
1931 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1932 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1933 1caad61b 2019-02-01 stsp goto done;
1935 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1936 d1f2edc9 2018-06-13 stsp if (error != NULL)
1937 1caad61b 2019-02-01 stsp goto done;
1939 15a94983 2018-12-23 stsp if (commit == NULL) {
1940 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1941 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1942 d1f2edc9 2018-06-13 stsp if (error != NULL)
1943 d1f2edc9 2018-06-13 stsp return error;
1946 d7d4f210 2018-03-12 stsp if (error != NULL)
1947 04ca23f4 2018-07-16 stsp goto done;
1949 deeabeae 2019-08-27 stsp if (worktree) {
1950 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1952 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
1953 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1954 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
1955 deeabeae 2019-08-27 stsp goto done;
1957 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1960 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1961 04ca23f4 2018-07-16 stsp if (error != NULL)
1962 04ca23f4 2018-07-16 stsp goto done;
1963 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1964 04ca23f4 2018-07-16 stsp free(path);
1965 04ca23f4 2018-07-16 stsp path = in_repo_path;
1968 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1969 199a4027 2019-02-02 stsp if (error)
1970 199a4027 2019-02-02 stsp goto done;
1972 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1973 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1975 04ca23f4 2018-07-16 stsp free(path);
1976 04ca23f4 2018-07-16 stsp free(repo_path);
1977 04ca23f4 2018-07-16 stsp free(cwd);
1979 cffc0aa4 2019-02-05 stsp if (worktree)
1980 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1981 ad242220 2018-09-08 stsp if (repo) {
1982 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1983 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1984 ad242220 2018-09-08 stsp if (error == NULL)
1985 ad242220 2018-09-08 stsp error = repo_error;
1987 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1988 8bf5b3c9 2018-03-17 stsp return error;
1991 4ed7e80c 2018-05-20 stsp __dead static void
1992 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1994 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1995 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
1999 b72f483a 2019-02-05 stsp struct print_diff_arg {
2000 b72f483a 2019-02-05 stsp struct got_repository *repo;
2001 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2002 b72f483a 2019-02-05 stsp int diff_context;
2003 3fc0c068 2019-02-10 stsp const char *id_str;
2004 3fc0c068 2019-02-10 stsp int header_shown;
2005 98eaaa12 2019-08-03 stsp int diff_staged;
2006 63035f9f 2019-10-06 stsp int ignore_whitespace;
2009 4ed7e80c 2018-05-20 stsp static const struct got_error *
2010 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2011 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2012 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2014 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2015 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2016 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2017 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2018 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2019 b72f483a 2019-02-05 stsp struct stat sb;
2021 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2022 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2023 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2024 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2025 98eaaa12 2019-08-03 stsp return NULL;
2027 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2028 98eaaa12 2019-08-03 stsp return NULL;
2029 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2030 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2031 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2032 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2033 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2034 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2035 98eaaa12 2019-08-03 stsp return NULL;
2038 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2039 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2040 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2041 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2042 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2045 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2046 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2047 98eaaa12 2019-08-03 stsp switch (staged_status) {
2048 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2049 98eaaa12 2019-08-03 stsp label1 = path;
2050 98eaaa12 2019-08-03 stsp label2 = path;
2052 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2053 98eaaa12 2019-08-03 stsp label2 = path;
2055 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2056 98eaaa12 2019-08-03 stsp label1 = path;
2059 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2061 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2062 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2063 63035f9f 2019-10-06 stsp a->repo, stdout);
2066 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2067 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2068 4ce46740 2019-08-08 stsp char *id_str;
2069 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2072 4ce46740 2019-08-08 stsp goto done;
2073 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2075 4ce46740 2019-08-08 stsp goto done;
2076 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2077 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2078 4ce46740 2019-08-08 stsp free(id_str);
2079 4ce46740 2019-08-08 stsp goto done;
2081 4ce46740 2019-08-08 stsp free(id_str);
2082 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2083 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2085 4ce46740 2019-08-08 stsp goto done;
2088 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2089 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2090 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2091 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2092 2ec1f75b 2019-03-26 stsp goto done;
2095 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
2096 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
2097 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
2098 2ec1f75b 2019-03-26 stsp goto done;
2100 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
2101 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
2102 2ec1f75b 2019-03-26 stsp goto done;
2105 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2107 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2108 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2110 b72f483a 2019-02-05 stsp if (blob1)
2111 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2112 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2113 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2114 b72f483a 2019-02-05 stsp free(abspath);
2115 927df6b7 2019-02-10 stsp return err;
2118 927df6b7 2019-02-10 stsp static const struct got_error *
2119 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2120 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2121 01073a5d 2019-08-22 stsp struct got_repository *repo)
2123 0adb7196 2019-08-11 stsp const struct got_error *err;
2124 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2125 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2127 0adb7196 2019-08-11 stsp *id = NULL;
2128 0adb7196 2019-08-11 stsp *label = NULL;
2130 01073a5d 2019-08-22 stsp if (resolve_tags) {
2131 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2133 01073a5d 2019-08-22 stsp if (err == NULL) {
2134 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2135 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2136 01073a5d 2019-08-22 stsp if (*id == NULL)
2137 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2138 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2139 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2140 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2141 32f0ab81 2019-08-28 hiltjo free(*id);
2142 01073a5d 2019-08-22 stsp *id = NULL;
2144 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2145 01073a5d 2019-08-22 stsp return err;
2146 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2147 01073a5d 2019-08-22 stsp return err;
2150 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2151 0adb7196 2019-08-11 stsp if (err) {
2152 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2153 0adb7196 2019-08-11 stsp return err;
2154 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2155 0adb7196 2019-08-11 stsp if (err != NULL)
2156 0adb7196 2019-08-11 stsp goto done;
2157 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2158 0adb7196 2019-08-11 stsp if (*label == NULL) {
2159 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2160 0adb7196 2019-08-11 stsp goto done;
2162 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2164 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2165 0adb7196 2019-08-11 stsp if (*label == NULL) {
2166 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2167 0adb7196 2019-08-11 stsp goto done;
2172 0adb7196 2019-08-11 stsp got_ref_close(ref);
2173 0adb7196 2019-08-11 stsp return err;
2177 0adb7196 2019-08-11 stsp static const struct got_error *
2178 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2180 b00d56cd 2018-04-01 stsp const struct got_error *error;
2181 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2182 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2183 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2184 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2185 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2186 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2187 15a94983 2018-12-23 stsp int type1, type2;
2188 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2189 c0cc5c62 2018-10-18 stsp const char *errstr;
2190 927df6b7 2019-02-10 stsp char *path = NULL;
2192 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2193 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2194 25eccc22 2019-01-04 stsp NULL) == -1)
2195 b00d56cd 2018-04-01 stsp err(1, "pledge");
2198 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2199 b00d56cd 2018-04-01 stsp switch (ch) {
2201 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2202 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2203 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2206 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2207 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2208 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2210 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2213 98eaaa12 2019-08-03 stsp diff_staged = 1;
2216 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2219 2deda0b9 2019-03-07 stsp usage_diff();
2220 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2224 b00d56cd 2018-04-01 stsp argc -= optind;
2225 b00d56cd 2018-04-01 stsp argv += optind;
2227 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2228 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2229 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2230 b72f483a 2019-02-05 stsp goto done;
2232 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2233 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2234 927df6b7 2019-02-10 stsp goto done;
2235 927df6b7 2019-02-10 stsp if (argc <= 1) {
2236 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2237 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2238 927df6b7 2019-02-10 stsp goto done;
2240 b72f483a 2019-02-05 stsp if (repo_path)
2242 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2243 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2244 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2245 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2246 927df6b7 2019-02-10 stsp goto done;
2248 927df6b7 2019-02-10 stsp if (argc == 1) {
2249 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2251 927df6b7 2019-02-10 stsp if (error)
2252 927df6b7 2019-02-10 stsp goto done;
2254 927df6b7 2019-02-10 stsp path = strdup("");
2255 927df6b7 2019-02-10 stsp if (path == NULL) {
2256 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2257 927df6b7 2019-02-10 stsp goto done;
2260 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2261 98eaaa12 2019-08-03 stsp if (diff_staged)
2262 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2263 98eaaa12 2019-08-03 stsp "objects in repository");
2264 15a94983 2018-12-23 stsp id_str1 = argv[0];
2265 15a94983 2018-12-23 stsp id_str2 = argv[1];
2266 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2267 30db809c 2019-06-05 stsp repo_path =
2268 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2269 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2270 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2271 30db809c 2019-06-05 stsp goto done;
2275 b00d56cd 2018-04-01 stsp usage_diff();
2277 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2278 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2279 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2280 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2283 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2284 76089277 2018-04-01 stsp free(repo_path);
2285 b00d56cd 2018-04-01 stsp if (error != NULL)
2286 b00d56cd 2018-04-01 stsp goto done;
2288 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2289 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2290 c02c541e 2019-03-29 stsp if (error)
2291 c02c541e 2019-03-29 stsp goto done;
2293 30db809c 2019-06-05 stsp if (argc <= 1) {
2294 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2295 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2296 b72f483a 2019-02-05 stsp char *id_str;
2298 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2300 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2301 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2302 b72f483a 2019-02-05 stsp if (error)
2303 b72f483a 2019-02-05 stsp goto done;
2304 b72f483a 2019-02-05 stsp arg.repo = repo;
2305 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2306 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2307 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2308 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2309 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2310 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2312 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2313 72ea6654 2019-07-27 stsp if (error)
2314 72ea6654 2019-07-27 stsp goto done;
2316 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2317 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2318 b72f483a 2019-02-05 stsp free(id_str);
2319 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2320 b72f483a 2019-02-05 stsp goto done;
2323 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2325 0adb7196 2019-08-11 stsp if (error)
2326 0adb7196 2019-08-11 stsp goto done;
2328 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2330 0adb7196 2019-08-11 stsp if (error)
2331 0adb7196 2019-08-11 stsp goto done;
2333 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2334 15a94983 2018-12-23 stsp if (error)
2335 15a94983 2018-12-23 stsp goto done;
2337 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2338 15a94983 2018-12-23 stsp if (error)
2339 15a94983 2018-12-23 stsp goto done;
2341 15a94983 2018-12-23 stsp if (type1 != type2) {
2342 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2343 b00d56cd 2018-04-01 stsp goto done;
2346 15a94983 2018-12-23 stsp switch (type1) {
2347 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2348 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2349 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2351 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2352 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2353 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2355 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2356 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2357 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2358 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2361 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2365 5e70831e 2019-05-28 stsp free(label1);
2366 5e70831e 2019-05-28 stsp free(label2);
2367 15a94983 2018-12-23 stsp free(id1);
2368 15a94983 2018-12-23 stsp free(id2);
2369 927df6b7 2019-02-10 stsp free(path);
2370 b72f483a 2019-02-05 stsp if (worktree)
2371 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2372 ad242220 2018-09-08 stsp if (repo) {
2373 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2374 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2375 ad242220 2018-09-08 stsp if (error == NULL)
2376 ad242220 2018-09-08 stsp error = repo_error;
2378 b00d56cd 2018-04-01 stsp return error;
2381 404c43c4 2018-06-21 stsp __dead static void
2382 404c43c4 2018-06-21 stsp usage_blame(void)
2384 9270e621 2019-02-05 stsp fprintf(stderr,
2385 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2386 404c43c4 2018-06-21 stsp getprogname());
2390 28315671 2019-08-14 stsp struct blame_line {
2391 28315671 2019-08-14 stsp int annotated;
2392 28315671 2019-08-14 stsp char *id_str;
2393 82f6abb8 2019-08-14 stsp char *committer;
2394 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2397 28315671 2019-08-14 stsp struct blame_cb_args {
2398 28315671 2019-08-14 stsp struct blame_line *lines;
2399 28315671 2019-08-14 stsp int nlines;
2400 7ef28ff8 2019-08-14 stsp int nlines_prec;
2401 28315671 2019-08-14 stsp int lineno_cur;
2402 28315671 2019-08-14 stsp off_t *line_offsets;
2404 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2407 404c43c4 2018-06-21 stsp static const struct got_error *
2408 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2410 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2411 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2412 28315671 2019-08-14 stsp struct blame_line *bline;
2413 82f6abb8 2019-08-14 stsp char *line = NULL;
2414 28315671 2019-08-14 stsp size_t linesize = 0;
2415 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2416 28315671 2019-08-14 stsp off_t offset;
2417 bcb49d15 2019-08-14 stsp struct tm tm;
2418 bcb49d15 2019-08-14 stsp time_t committer_time;
2420 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2421 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2422 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2424 28315671 2019-08-14 stsp if (sigint_received)
2425 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2427 2839f8b9 2019-08-18 stsp if (lineno == -1)
2428 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2430 28315671 2019-08-14 stsp /* Annotate this line. */
2431 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2432 28315671 2019-08-14 stsp if (bline->annotated)
2433 28315671 2019-08-14 stsp return NULL;
2434 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2436 28315671 2019-08-14 stsp return err;
2438 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2440 82f6abb8 2019-08-14 stsp goto done;
2442 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2443 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2444 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2445 bcb49d15 2019-08-14 stsp goto done;
2448 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2449 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2450 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2451 11db6024 2019-10-21 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2452 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2453 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2454 82f6abb8 2019-08-14 stsp goto done;
2456 28315671 2019-08-14 stsp bline->annotated = 1;
2458 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2459 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2460 28315671 2019-08-14 stsp if (!bline->annotated)
2461 82f6abb8 2019-08-14 stsp goto done;
2463 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2464 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2465 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2466 82f6abb8 2019-08-14 stsp goto done;
2469 28315671 2019-08-14 stsp while (bline->annotated) {
2470 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2471 82f6abb8 2019-08-14 stsp size_t len;
2473 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2474 28315671 2019-08-14 stsp if (ferror(a->f))
2475 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2479 82f6abb8 2019-08-14 stsp committer = bline->committer;
2480 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2481 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2482 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2483 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2485 82f6abb8 2019-08-14 stsp *at = '\0';
2486 82f6abb8 2019-08-14 stsp len = strlen(committer);
2487 82f6abb8 2019-08-14 stsp if (len >= 9)
2488 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2490 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2492 28315671 2019-08-14 stsp *nl = '\0';
2493 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2494 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2496 28315671 2019-08-14 stsp a->lineno_cur++;
2497 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2500 82f6abb8 2019-08-14 stsp if (commit)
2501 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2502 28315671 2019-08-14 stsp free(line);
2503 28315671 2019-08-14 stsp return err;
2506 28315671 2019-08-14 stsp static const struct got_error *
2507 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2509 404c43c4 2018-06-21 stsp const struct got_error *error;
2510 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2511 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2512 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2513 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2514 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2515 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2516 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2517 28315671 2019-08-14 stsp struct blame_cb_args bca;
2518 28315671 2019-08-14 stsp int ch, obj_type, i;
2519 b02560ec 2019-08-19 stsp size_t filesize;
2521 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2523 404c43c4 2018-06-21 stsp #ifndef PROFILE
2524 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2525 36e2fb66 2019-01-04 stsp NULL) == -1)
2526 404c43c4 2018-06-21 stsp err(1, "pledge");
2529 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2530 404c43c4 2018-06-21 stsp switch (ch) {
2532 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2535 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2536 66bea077 2018-08-02 stsp if (repo_path == NULL)
2537 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2539 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2542 2deda0b9 2019-03-07 stsp usage_blame();
2543 404c43c4 2018-06-21 stsp /* NOTREACHED */
2547 404c43c4 2018-06-21 stsp argc -= optind;
2548 404c43c4 2018-06-21 stsp argv += optind;
2550 a39318fd 2018-08-02 stsp if (argc == 1)
2551 404c43c4 2018-06-21 stsp path = argv[0];
2553 404c43c4 2018-06-21 stsp usage_blame();
2555 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2556 66bea077 2018-08-02 stsp if (cwd == NULL) {
2557 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2558 66bea077 2018-08-02 stsp goto done;
2560 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2561 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2562 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2563 66bea077 2018-08-02 stsp goto done;
2565 0c06baac 2019-02-05 stsp error = NULL;
2566 0c06baac 2019-02-05 stsp if (worktree) {
2567 0c06baac 2019-02-05 stsp repo_path =
2568 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2569 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2570 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2571 0c06baac 2019-02-05 stsp if (error)
2572 0c06baac 2019-02-05 stsp goto done;
2574 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2575 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2576 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2577 0c06baac 2019-02-05 stsp goto done;
2582 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2583 c02c541e 2019-03-29 stsp if (error != NULL)
2584 36e2fb66 2019-01-04 stsp goto done;
2586 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2587 c02c541e 2019-03-29 stsp if (error)
2588 404c43c4 2018-06-21 stsp goto done;
2590 0c06baac 2019-02-05 stsp if (worktree) {
2591 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2592 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2593 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2594 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2595 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2596 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2597 6efaaa2d 2019-02-05 stsp path) == -1) {
2598 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2599 0c06baac 2019-02-05 stsp goto done;
2601 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2604 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2606 0c06baac 2019-02-05 stsp if (error)
2607 66bea077 2018-08-02 stsp goto done;
2609 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2610 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2611 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2612 404c43c4 2018-06-21 stsp if (error != NULL)
2613 66bea077 2018-08-02 stsp goto done;
2614 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2615 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2616 404c43c4 2018-06-21 stsp if (error != NULL)
2617 66bea077 2018-08-02 stsp goto done;
2619 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2620 30837e32 2019-07-25 stsp if (error)
2621 66bea077 2018-08-02 stsp goto done;
2624 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2625 28315671 2019-08-14 stsp if (error)
2626 28315671 2019-08-14 stsp goto done;
2627 28315671 2019-08-14 stsp if (obj_id == NULL) {
2628 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2629 28315671 2019-08-14 stsp goto done;
2632 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2633 28315671 2019-08-14 stsp if (error)
2634 28315671 2019-08-14 stsp goto done;
2636 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2637 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2638 28315671 2019-08-14 stsp goto done;
2641 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2642 28315671 2019-08-14 stsp if (error)
2643 28315671 2019-08-14 stsp goto done;
2644 28315671 2019-08-14 stsp bca.f = got_opentemp();
2645 28315671 2019-08-14 stsp if (bca.f == NULL) {
2646 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2647 28315671 2019-08-14 stsp goto done;
2649 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2650 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2651 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2652 28315671 2019-08-14 stsp goto done;
2654 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2655 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2656 b02560ec 2019-08-19 stsp bca.nlines--;
2658 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2659 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2660 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2661 28315671 2019-08-14 stsp goto done;
2663 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2664 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2665 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2666 7ef28ff8 2019-08-14 stsp while (i > 0) {
2668 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2670 82f6abb8 2019-08-14 stsp bca.repo = repo;
2672 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2673 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2674 28315671 2019-08-14 stsp if (error)
2675 28315671 2019-08-14 stsp goto done;
2677 66bea077 2018-08-02 stsp free(in_repo_path);
2678 66bea077 2018-08-02 stsp free(repo_path);
2679 66bea077 2018-08-02 stsp free(cwd);
2680 404c43c4 2018-06-21 stsp free(commit_id);
2681 28315671 2019-08-14 stsp free(obj_id);
2683 28315671 2019-08-14 stsp got_object_blob_close(blob);
2684 0c06baac 2019-02-05 stsp if (worktree)
2685 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2686 ad242220 2018-09-08 stsp if (repo) {
2687 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2688 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2689 ad242220 2018-09-08 stsp if (error == NULL)
2690 ad242220 2018-09-08 stsp error = repo_error;
2692 3affba96 2019-09-22 stsp if (bca.lines) {
2693 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2694 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2695 3affba96 2019-09-22 stsp free(bline->id_str);
2696 3affba96 2019-09-22 stsp free(bline->committer);
2698 3affba96 2019-09-22 stsp free(bca.lines);
2700 28315671 2019-08-14 stsp free(bca.line_offsets);
2701 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2702 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2703 404c43c4 2018-06-21 stsp return error;
2706 5de5890b 2018-10-18 stsp __dead static void
2707 5de5890b 2018-10-18 stsp usage_tree(void)
2709 c1669e2e 2019-01-09 stsp fprintf(stderr,
2710 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2711 5de5890b 2018-10-18 stsp getprogname());
2715 c1669e2e 2019-01-09 stsp static void
2716 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2717 c1669e2e 2019-01-09 stsp const char *root_path)
2719 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2720 848d6979 2019-08-12 stsp const char *modestr = "";
2722 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2723 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2726 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2727 63c5ca5d 2019-08-24 stsp modestr = "$";
2728 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
2729 848d6979 2019-08-12 stsp modestr = "@";
2730 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2731 848d6979 2019-08-12 stsp modestr = "/";
2732 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2733 848d6979 2019-08-12 stsp modestr = "*";
2735 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2736 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2739 5de5890b 2018-10-18 stsp static const struct got_error *
2740 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2741 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2742 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2744 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2745 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2746 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2747 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2748 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2750 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2752 5de5890b 2018-10-18 stsp goto done;
2754 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2756 5de5890b 2018-10-18 stsp goto done;
2757 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2758 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2759 5de5890b 2018-10-18 stsp while (te) {
2760 5de5890b 2018-10-18 stsp char *id = NULL;
2762 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2765 5de5890b 2018-10-18 stsp if (show_ids) {
2766 5de5890b 2018-10-18 stsp char *id_str;
2767 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2769 5de5890b 2018-10-18 stsp goto done;
2770 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2771 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2772 5de5890b 2018-10-18 stsp free(id_str);
2773 5de5890b 2018-10-18 stsp goto done;
2775 5de5890b 2018-10-18 stsp free(id_str);
2777 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2780 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2781 c1669e2e 2019-01-09 stsp char *child_path;
2782 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2783 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2784 c1669e2e 2019-01-09 stsp te->name) == -1) {
2785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2786 c1669e2e 2019-01-09 stsp goto done;
2788 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2789 c1669e2e 2019-01-09 stsp root_path, repo);
2790 c1669e2e 2019-01-09 stsp free(child_path);
2792 c1669e2e 2019-01-09 stsp goto done;
2795 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2799 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2800 5de5890b 2018-10-18 stsp free(tree_id);
2801 5de5890b 2018-10-18 stsp return err;
2804 5de5890b 2018-10-18 stsp static const struct got_error *
2805 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2807 5de5890b 2018-10-18 stsp const struct got_error *error;
2808 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2809 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2810 7a2c19d6 2019-02-05 stsp const char *path;
2811 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2812 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2813 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2814 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2817 5de5890b 2018-10-18 stsp #ifndef PROFILE
2818 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2819 0f8d269b 2019-01-04 stsp NULL) == -1)
2820 5de5890b 2018-10-18 stsp err(1, "pledge");
2823 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2824 5de5890b 2018-10-18 stsp switch (ch) {
2826 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2829 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2830 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2831 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2833 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2836 5de5890b 2018-10-18 stsp show_ids = 1;
2839 c1669e2e 2019-01-09 stsp recurse = 1;
2842 2deda0b9 2019-03-07 stsp usage_tree();
2843 5de5890b 2018-10-18 stsp /* NOTREACHED */
2847 5de5890b 2018-10-18 stsp argc -= optind;
2848 5de5890b 2018-10-18 stsp argv += optind;
2850 5de5890b 2018-10-18 stsp if (argc == 1)
2851 5de5890b 2018-10-18 stsp path = argv[0];
2852 5de5890b 2018-10-18 stsp else if (argc > 1)
2853 5de5890b 2018-10-18 stsp usage_tree();
2855 7a2c19d6 2019-02-05 stsp path = NULL;
2857 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2858 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2859 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2860 9bf7a39b 2019-02-05 stsp goto done;
2862 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2863 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2864 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2865 7a2c19d6 2019-02-05 stsp goto done;
2867 7a2c19d6 2019-02-05 stsp error = NULL;
2868 7a2c19d6 2019-02-05 stsp if (worktree) {
2869 7a2c19d6 2019-02-05 stsp repo_path =
2870 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2871 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2872 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2873 7a2c19d6 2019-02-05 stsp if (error)
2874 7a2c19d6 2019-02-05 stsp goto done;
2876 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2877 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2878 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2879 7a2c19d6 2019-02-05 stsp goto done;
2884 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2885 5de5890b 2018-10-18 stsp if (error != NULL)
2886 c02c541e 2019-03-29 stsp goto done;
2888 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2889 c02c541e 2019-03-29 stsp if (error)
2890 5de5890b 2018-10-18 stsp goto done;
2892 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2893 9bf7a39b 2019-02-05 stsp if (worktree) {
2894 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2895 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2896 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2897 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2898 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2899 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2900 9bf7a39b 2019-02-05 stsp goto done;
2902 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2904 9bf7a39b 2019-02-05 stsp if (error)
2905 9bf7a39b 2019-02-05 stsp goto done;
2907 9bf7a39b 2019-02-05 stsp path = "/";
2909 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2910 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2911 9bf7a39b 2019-02-05 stsp if (error != NULL)
2912 9bf7a39b 2019-02-05 stsp goto done;
2915 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2916 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2917 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2918 5de5890b 2018-10-18 stsp if (error != NULL)
2919 5de5890b 2018-10-18 stsp goto done;
2920 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2921 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2922 5de5890b 2018-10-18 stsp if (error != NULL)
2923 5de5890b 2018-10-18 stsp goto done;
2925 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2926 30837e32 2019-07-25 stsp if (error)
2927 5de5890b 2018-10-18 stsp goto done;
2930 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2931 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2933 5de5890b 2018-10-18 stsp free(in_repo_path);
2934 5de5890b 2018-10-18 stsp free(repo_path);
2935 5de5890b 2018-10-18 stsp free(cwd);
2936 5de5890b 2018-10-18 stsp free(commit_id);
2937 7a2c19d6 2019-02-05 stsp if (worktree)
2938 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2939 5de5890b 2018-10-18 stsp if (repo) {
2940 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2941 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2942 5de5890b 2018-10-18 stsp if (error == NULL)
2943 5de5890b 2018-10-18 stsp error = repo_error;
2945 5de5890b 2018-10-18 stsp return error;
2948 6bad629b 2019-02-04 stsp __dead static void
2949 6bad629b 2019-02-04 stsp usage_status(void)
2951 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2955 b72f483a 2019-02-05 stsp static const struct got_error *
2956 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2957 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2958 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2960 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2961 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2962 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2963 b72f483a 2019-02-05 stsp return NULL;
2966 6bad629b 2019-02-04 stsp static const struct got_error *
2967 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2969 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2970 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2971 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2972 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2973 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2974 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2977 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2979 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2980 6bad629b 2019-02-04 stsp switch (ch) {
2982 2deda0b9 2019-03-07 stsp usage_status();
2983 6bad629b 2019-02-04 stsp /* NOTREACHED */
2987 6bad629b 2019-02-04 stsp argc -= optind;
2988 6bad629b 2019-02-04 stsp argv += optind;
2990 6bad629b 2019-02-04 stsp #ifndef PROFILE
2991 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2992 6bad629b 2019-02-04 stsp NULL) == -1)
2993 6bad629b 2019-02-04 stsp err(1, "pledge");
2995 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2996 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2997 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2998 927df6b7 2019-02-10 stsp goto done;
3001 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3002 927df6b7 2019-02-10 stsp if (error != NULL)
3003 a5edda0a 2019-07-27 stsp goto done;
3005 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3007 6bad629b 2019-02-04 stsp if (error != NULL)
3008 6bad629b 2019-02-04 stsp goto done;
3010 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3011 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3012 087fb88c 2019-08-04 stsp if (error)
3013 087fb88c 2019-08-04 stsp goto done;
3015 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3016 6bad629b 2019-02-04 stsp if (error)
3017 6bad629b 2019-02-04 stsp goto done;
3019 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3020 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3022 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3023 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3024 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3025 927df6b7 2019-02-10 stsp free(cwd);
3026 d0eebce4 2019-03-11 stsp return error;
3029 d0eebce4 2019-03-11 stsp __dead static void
3030 d0eebce4 2019-03-11 stsp usage_ref(void)
3032 d0eebce4 2019-03-11 stsp fprintf(stderr,
3033 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3034 d0eebce4 2019-03-11 stsp getprogname());
3038 d0eebce4 2019-03-11 stsp static const struct got_error *
3039 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3041 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3042 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3043 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3045 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3046 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3048 d0eebce4 2019-03-11 stsp return err;
3050 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3051 d0eebce4 2019-03-11 stsp char *refstr;
3052 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3053 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3054 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3055 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3056 d0eebce4 2019-03-11 stsp free(refstr);
3059 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3060 d0eebce4 2019-03-11 stsp return NULL;
3063 d0eebce4 2019-03-11 stsp static const struct got_error *
3064 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3066 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3067 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3069 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3071 d0eebce4 2019-03-11 stsp return err;
3073 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3074 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3075 d0eebce4 2019-03-11 stsp return err;
3078 d0eebce4 2019-03-11 stsp static const struct got_error *
3079 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3081 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3082 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3083 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3086 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
3087 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3088 d1644381 2019-07-14 stsp * an unintended typo.
3090 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
3091 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3093 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3095 d83d9d5c 2019-05-13 stsp if (err) {
3096 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3098 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3099 d83d9d5c 2019-05-13 stsp return err;
3100 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3102 d83d9d5c 2019-05-13 stsp return err;
3103 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3104 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3106 d83d9d5c 2019-05-13 stsp return err;
3109 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3111 d0eebce4 2019-03-11 stsp goto done;
3113 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3116 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3118 d1c1ae5f 2019-08-12 stsp return err;
3121 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3122 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3124 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3125 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3126 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3129 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
3130 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3131 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3133 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
3134 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3136 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3138 d1c1ae5f 2019-08-12 stsp return err;
3140 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3142 d1c1ae5f 2019-08-12 stsp goto done;
3144 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3146 d1c1ae5f 2019-08-12 stsp if (target_ref)
3147 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3149 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3150 d0eebce4 2019-03-11 stsp return err;
3153 d0eebce4 2019-03-11 stsp static const struct got_error *
3154 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3156 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3157 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3158 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3159 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3160 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3161 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3163 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3164 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3165 d0eebce4 2019-03-11 stsp switch (ch) {
3167 d0eebce4 2019-03-11 stsp delref = optarg;
3170 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3171 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3172 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3174 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3177 d0eebce4 2019-03-11 stsp do_list = 1;
3180 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3183 d0eebce4 2019-03-11 stsp usage_ref();
3184 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3188 d0eebce4 2019-03-11 stsp if (do_list && delref)
3189 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3191 d0eebce4 2019-03-11 stsp argc -= optind;
3192 d0eebce4 2019-03-11 stsp argv += optind;
3194 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3195 d1c1ae5f 2019-08-12 stsp if (create_symref)
3196 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3197 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3198 d0eebce4 2019-03-11 stsp if (argc > 0)
3199 d0eebce4 2019-03-11 stsp usage_ref();
3200 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3201 d0eebce4 2019-03-11 stsp usage_ref();
3203 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3204 e0b57350 2019-03-12 stsp if (do_list) {
3205 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3206 e0b57350 2019-03-12 stsp NULL) == -1)
3207 e0b57350 2019-03-12 stsp err(1, "pledge");
3209 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3210 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3211 e0b57350 2019-03-12 stsp err(1, "pledge");
3214 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3215 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3216 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3217 d0eebce4 2019-03-11 stsp goto done;
3220 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3221 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3222 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3223 d0eebce4 2019-03-11 stsp goto done;
3225 d0eebce4 2019-03-11 stsp error = NULL;
3226 d0eebce4 2019-03-11 stsp if (worktree) {
3227 d0eebce4 2019-03-11 stsp repo_path =
3228 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3229 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3230 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3231 d0eebce4 2019-03-11 stsp if (error)
3232 d0eebce4 2019-03-11 stsp goto done;
3234 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3235 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3236 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3237 d0eebce4 2019-03-11 stsp goto done;
3242 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3243 d0eebce4 2019-03-11 stsp if (error != NULL)
3244 d0eebce4 2019-03-11 stsp goto done;
3246 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3247 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3248 c02c541e 2019-03-29 stsp if (error)
3249 c02c541e 2019-03-29 stsp goto done;
3251 d0eebce4 2019-03-11 stsp if (do_list)
3252 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3253 d0eebce4 2019-03-11 stsp else if (delref)
3254 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3255 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3256 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3258 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3261 4e759de4 2019-06-26 stsp got_repo_close(repo);
3262 4e759de4 2019-06-26 stsp if (worktree)
3263 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3264 4e759de4 2019-06-26 stsp free(cwd);
3265 4e759de4 2019-06-26 stsp free(repo_path);
3266 4e759de4 2019-06-26 stsp return error;
3269 4e759de4 2019-06-26 stsp __dead static void
3270 4e759de4 2019-06-26 stsp usage_branch(void)
3272 4e759de4 2019-06-26 stsp fprintf(stderr,
3273 ad89fa31 2019-10-04 stsp "usage: %s branch [-r repository] [-l] | -d name | "
3274 ad89fa31 2019-10-04 stsp "[name [commit]]\n", getprogname());
3278 4e759de4 2019-06-26 stsp static const struct got_error *
3279 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3280 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3282 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3283 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3284 4e99b47e 2019-10-04 stsp char *refstr;
3286 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3287 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3288 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3289 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3291 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3293 4e99b47e 2019-10-04 stsp return err;
3294 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3295 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3296 4e99b47e 2019-10-04 stsp marker = "* ";
3298 4e99b47e 2019-10-04 stsp marker = "~ ";
3302 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3303 4e99b47e 2019-10-04 stsp refname += 11;
3304 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3305 4e99b47e 2019-10-04 stsp refname += 18;
3307 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3308 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3309 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3311 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3312 4e99b47e 2019-10-04 stsp free(refstr);
3313 4e99b47e 2019-10-04 stsp return NULL;
3316 4e99b47e 2019-10-04 stsp static const struct got_error *
3317 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3319 ad89fa31 2019-10-04 stsp const char *refname;
3321 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3322 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3324 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3326 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3327 ad89fa31 2019-10-04 stsp refname += 11;
3328 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3329 ad89fa31 2019-10-04 stsp refname += 18;
3331 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3333 ad89fa31 2019-10-04 stsp return NULL;
3336 ad89fa31 2019-10-04 stsp static const struct got_error *
3337 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3339 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3340 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3341 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3342 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3343 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3345 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3347 4e99b47e 2019-10-04 stsp if (worktree) {
3348 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3349 4e99b47e 2019-10-04 stsp worktree);
3351 4e99b47e 2019-10-04 stsp return err;
3353 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3354 4e99b47e 2019-10-04 stsp worktree);
3356 4e99b47e 2019-10-04 stsp return err;
3358 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3359 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3360 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3362 4e99b47e 2019-10-04 stsp return err;
3363 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3364 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3368 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3369 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3371 4e759de4 2019-06-26 stsp return err;
3373 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3374 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3376 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3377 4e759de4 2019-06-26 stsp return NULL;
3380 4e759de4 2019-06-26 stsp static const struct got_error *
3381 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3382 45cd4e47 2019-08-25 stsp const char *branch_name)
3384 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3385 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3386 4e759de4 2019-06-26 stsp char *refname;
3388 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3389 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3391 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3393 4e759de4 2019-06-26 stsp goto done;
3395 45cd4e47 2019-08-25 stsp if (worktree &&
3396 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3397 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3398 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3399 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3400 45cd4e47 2019-08-25 stsp goto done;
3403 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3406 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3407 4e759de4 2019-06-26 stsp free(refname);
3408 4e759de4 2019-06-26 stsp return err;
3411 4e759de4 2019-06-26 stsp static const struct got_error *
3412 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3413 4e759de4 2019-06-26 stsp const char *base_branch)
3415 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3416 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3417 a4f89d48 2019-08-25 stsp char *label;
3418 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3419 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3422 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3423 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3424 d3f84d51 2019-07-11 stsp * an unintended typo.
3426 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3427 24b5452a 2019-10-09 stsp return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3429 a4f89d48 2019-08-25 stsp err = match_object_id(&id, &label, base_branch,
3430 a4f89d48 2019-08-25 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3432 a4f89d48 2019-08-25 stsp return err;
3434 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3435 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3436 4e759de4 2019-06-26 stsp goto done;
3439 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3440 4e759de4 2019-06-26 stsp if (err == NULL) {
3441 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3442 4e759de4 2019-06-26 stsp goto done;
3443 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3444 4e759de4 2019-06-26 stsp goto done;
3446 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3448 4e759de4 2019-06-26 stsp goto done;
3450 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3453 4e759de4 2019-06-26 stsp got_ref_close(ref);
3455 4e759de4 2019-06-26 stsp free(base_refname);
3456 4e759de4 2019-06-26 stsp free(refname);
3457 4e759de4 2019-06-26 stsp return err;
3460 4e759de4 2019-06-26 stsp static const struct got_error *
3461 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3463 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3464 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3465 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3466 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3467 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3468 4e759de4 2019-06-26 stsp const char *delref = NULL;
3470 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3471 4e759de4 2019-06-26 stsp switch (ch) {
3473 4e759de4 2019-06-26 stsp delref = optarg;
3476 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3477 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3478 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3480 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3483 4e759de4 2019-06-26 stsp do_list = 1;
3486 4e759de4 2019-06-26 stsp usage_branch();
3487 4e759de4 2019-06-26 stsp /* NOTREACHED */
3491 4e759de4 2019-06-26 stsp if (do_list && delref)
3492 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3494 4e759de4 2019-06-26 stsp argc -= optind;
3495 4e759de4 2019-06-26 stsp argv += optind;
3497 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3498 ad89fa31 2019-10-04 stsp do_show = 1;
3500 4e759de4 2019-06-26 stsp if (do_list || delref) {
3501 4e759de4 2019-06-26 stsp if (argc > 0)
3502 4e759de4 2019-06-26 stsp usage_branch();
3503 ad89fa31 2019-10-04 stsp } else if (!do_show && (argc < 1 || argc > 2))
3504 4e759de4 2019-06-26 stsp usage_branch();
3506 4e759de4 2019-06-26 stsp #ifndef PROFILE
3507 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3508 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3509 4e759de4 2019-06-26 stsp NULL) == -1)
3510 4e759de4 2019-06-26 stsp err(1, "pledge");
3512 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3513 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3514 4e759de4 2019-06-26 stsp err(1, "pledge");
3517 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3518 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3519 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3520 4e759de4 2019-06-26 stsp goto done;
3523 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3524 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3525 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3526 4e759de4 2019-06-26 stsp goto done;
3528 4e759de4 2019-06-26 stsp error = NULL;
3529 4e759de4 2019-06-26 stsp if (worktree) {
3530 4e759de4 2019-06-26 stsp repo_path =
3531 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3532 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3533 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3534 4e759de4 2019-06-26 stsp if (error)
3535 4e759de4 2019-06-26 stsp goto done;
3537 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3538 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3539 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3540 4e759de4 2019-06-26 stsp goto done;
3545 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3546 4e759de4 2019-06-26 stsp if (error != NULL)
3547 4e759de4 2019-06-26 stsp goto done;
3549 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3550 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3551 4e759de4 2019-06-26 stsp if (error)
3552 4e759de4 2019-06-26 stsp goto done;
3554 ad89fa31 2019-10-04 stsp if (do_show)
3555 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3556 ad89fa31 2019-10-04 stsp else if (do_list)
3557 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3558 4e759de4 2019-06-26 stsp else if (delref)
3559 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3561 4e759de4 2019-06-26 stsp const char *base_branch;
3562 4e759de4 2019-06-26 stsp if (argc == 1) {
3563 4e759de4 2019-06-26 stsp base_branch = worktree ?
3564 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3565 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3566 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3567 dc5351b4 2019-07-30 stsp base_branch += 11;
3569 4e759de4 2019-06-26 stsp base_branch = argv[1];
3570 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3574 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3575 d0eebce4 2019-03-11 stsp if (worktree)
3576 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3577 d0eebce4 2019-03-11 stsp free(cwd);
3578 d0eebce4 2019-03-11 stsp free(repo_path);
3579 d00136be 2019-03-26 stsp return error;
3583 d00136be 2019-03-26 stsp __dead static void
3584 8e7bd50a 2019-08-22 stsp usage_tag(void)
3586 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3587 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3588 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3593 b8bad2ba 2019-08-23 stsp static const struct got_error *
3594 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3596 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3597 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3598 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3599 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3600 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3602 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3603 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3604 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3605 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3607 b8bad2ba 2019-08-23 stsp return err;
3608 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3611 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3614 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3615 b8bad2ba 2019-08-23 stsp free(re_id);
3618 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3619 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3622 b8bad2ba 2019-08-23 stsp while (se) {
3623 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3626 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3627 b8bad2ba 2019-08-23 stsp free(se_id);
3630 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3631 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3633 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3634 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3636 b8bad2ba 2019-08-23 stsp return err;
3637 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3640 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3645 b8bad2ba 2019-08-23 stsp return err;
3649 8e7bd50a 2019-08-22 stsp static const struct got_error *
3650 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3651 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3653 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3654 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3655 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3656 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3657 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3661 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3663 b8bad2ba 2019-08-23 stsp return err;
3664 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3666 b8bad2ba 2019-08-23 stsp goto done;
3668 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3670 b8bad2ba 2019-08-23 stsp goto done;
3671 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3673 b8bad2ba 2019-08-23 stsp goto done;
3675 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3676 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3678 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3679 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3681 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3682 b8bad2ba 2019-08-23 stsp *cmp = -1;
3684 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3686 b8bad2ba 2019-08-23 stsp free(id1);
3687 b8bad2ba 2019-08-23 stsp free(id2);
3689 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3691 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3692 b8bad2ba 2019-08-23 stsp return err;
3695 b8bad2ba 2019-08-23 stsp static const struct got_error *
3696 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3698 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3699 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3700 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3702 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3704 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3706 8e7bd50a 2019-08-22 stsp return err;
3708 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3709 8e7bd50a 2019-08-22 stsp const char *refname;
3710 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3711 8e7bd50a 2019-08-22 stsp char datebuf[26];
3712 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3713 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3714 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3716 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3717 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3719 8e7bd50a 2019-08-22 stsp refname += 10;
3720 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3721 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3722 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3725 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3726 8e7bd50a 2019-08-22 stsp free(refstr);
3728 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3731 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3735 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3736 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3737 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3738 2417344c 2019-08-23 stsp if (datestr)
3739 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3740 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3741 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3744 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3745 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3746 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3748 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3749 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3751 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3752 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3754 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3755 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3760 8e7bd50a 2019-08-22 stsp free(id_str);
3761 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3762 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3763 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3764 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3768 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3770 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3772 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3773 8e7bd50a 2019-08-22 stsp } while (line);
3774 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3777 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3778 8e7bd50a 2019-08-22 stsp return NULL;
3781 8e7bd50a 2019-08-22 stsp static const struct got_error *
3782 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3783 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3785 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3786 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3787 f372d5cd 2019-10-21 stsp char *editor = NULL;
3788 8e7bd50a 2019-08-22 stsp int fd = -1;
3790 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3791 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3792 8e7bd50a 2019-08-22 stsp goto done;
3795 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3796 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3797 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3798 8e7bd50a 2019-08-22 stsp goto done;
3801 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3803 8e7bd50a 2019-08-22 stsp goto done;
3805 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3806 8e7bd50a 2019-08-22 stsp close(fd);
3808 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3810 8e7bd50a 2019-08-22 stsp goto done;
3811 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3813 8e7bd50a 2019-08-22 stsp free(initial_content);
3814 8e7bd50a 2019-08-22 stsp free(template);
3815 8e7bd50a 2019-08-22 stsp free(editor);
3817 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3818 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3819 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3820 8e7bd50a 2019-08-22 stsp if (err) {
3821 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3822 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3825 8e7bd50a 2019-08-22 stsp return err;
3828 8e7bd50a 2019-08-22 stsp static const struct got_error *
3829 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3830 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3832 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3833 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3834 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3835 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3836 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3837 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3838 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3841 8e7bd50a 2019-08-22 stsp * Don't let the user create a tag named '-'.
3842 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3843 8e7bd50a 2019-08-22 stsp * an unintended typo.
3845 8e7bd50a 2019-08-22 stsp if (tag_name[0] == '-' && tag_name[1] == '\0')
3846 24b5452a 2019-10-09 stsp return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3848 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3850 8e7bd50a 2019-08-22 stsp return err;
3852 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3853 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3855 8e7bd50a 2019-08-22 stsp goto done;
3857 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3859 8e7bd50a 2019-08-22 stsp goto done;
3861 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3862 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3863 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3864 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3865 8e7bd50a 2019-08-22 stsp goto done;
3867 8e7bd50a 2019-08-22 stsp tag_name += 10;
3868 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3869 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3870 8e7bd50a 2019-08-22 stsp goto done;
3873 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3874 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3875 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3876 8e7bd50a 2019-08-22 stsp goto done;
3877 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3878 8e7bd50a 2019-08-22 stsp goto done;
3880 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3881 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3882 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3883 f372d5cd 2019-10-21 stsp if (err) {
3884 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3885 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3886 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3887 8e7bd50a 2019-08-22 stsp goto done;
3891 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3892 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3893 f372d5cd 2019-10-21 stsp if (err) {
3894 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3895 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3896 8e7bd50a 2019-08-22 stsp goto done;
3899 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3900 f372d5cd 2019-10-21 stsp if (err) {
3901 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3902 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3903 8e7bd50a 2019-08-22 stsp goto done;
3906 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3907 f372d5cd 2019-10-21 stsp if (err) {
3908 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3909 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3910 f372d5cd 2019-10-21 stsp goto done;
3913 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3914 f372d5cd 2019-10-21 stsp if (err) {
3915 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3916 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3917 f372d5cd 2019-10-21 stsp goto done;
3919 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3921 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3922 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3923 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3924 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3925 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3926 f372d5cd 2019-10-21 stsp free(tag_id_str);
3928 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3929 8e7bd50a 2019-08-22 stsp free(commit_id);
3930 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3931 8e7bd50a 2019-08-22 stsp free(refname);
3932 8e7bd50a 2019-08-22 stsp free(tagmsg);
3933 f372d5cd 2019-10-21 stsp free(tagmsg_path);
3934 aba9c984 2019-09-08 stsp free(tagger);
3935 8e7bd50a 2019-08-22 stsp return err;
3938 8e7bd50a 2019-08-22 stsp static const struct got_error *
3939 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3941 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3942 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3943 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3944 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3945 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
3946 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3947 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3949 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3950 8e7bd50a 2019-08-22 stsp switch (ch) {
3952 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3955 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3956 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3957 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3959 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3962 8e7bd50a 2019-08-22 stsp do_list = 1;
3965 8e7bd50a 2019-08-22 stsp usage_tag();
3966 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3970 8e7bd50a 2019-08-22 stsp argc -= optind;
3971 8e7bd50a 2019-08-22 stsp argv += optind;
3973 8e7bd50a 2019-08-22 stsp if (do_list) {
3974 8e7bd50a 2019-08-22 stsp if (tagmsg)
3975 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3976 8e7bd50a 2019-08-22 stsp if (argc > 0)
3977 8e7bd50a 2019-08-22 stsp usage_tag();
3978 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3979 8e7bd50a 2019-08-22 stsp usage_tag();
3980 a2887370 2019-08-23 stsp else if (argc > 1)
3981 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3982 a2887370 2019-08-23 stsp tag_name = argv[0];
3984 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3985 8e7bd50a 2019-08-22 stsp if (do_list) {
3986 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3987 8e7bd50a 2019-08-22 stsp NULL) == -1)
3988 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3990 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3991 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3992 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3995 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
3996 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
3997 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
3998 8e7bd50a 2019-08-22 stsp goto done;
4001 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4002 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4003 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4004 8e7bd50a 2019-08-22 stsp goto done;
4006 8e7bd50a 2019-08-22 stsp error = NULL;
4007 8e7bd50a 2019-08-22 stsp if (worktree) {
4008 8e7bd50a 2019-08-22 stsp repo_path =
4009 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4010 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4011 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4012 8e7bd50a 2019-08-22 stsp if (error)
4013 8e7bd50a 2019-08-22 stsp goto done;
4015 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4016 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4017 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4018 8e7bd50a 2019-08-22 stsp goto done;
4023 8e7bd50a 2019-08-22 stsp if (do_list) {
4024 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4025 c9956ddf 2019-09-08 stsp if (error != NULL)
4026 c9956ddf 2019-09-08 stsp goto done;
4027 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4028 8e7bd50a 2019-08-22 stsp if (error)
4029 8e7bd50a 2019-08-22 stsp goto done;
4030 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4032 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4033 c9956ddf 2019-09-08 stsp if (error)
4034 c9956ddf 2019-09-08 stsp goto done;
4035 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4036 c9956ddf 2019-09-08 stsp if (error != NULL)
4037 c9956ddf 2019-09-08 stsp goto done;
4039 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4040 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4041 8e7bd50a 2019-08-22 stsp if (error)
4042 8e7bd50a 2019-08-22 stsp goto done;
4045 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4046 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4047 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4048 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4049 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4050 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4051 8e7bd50a 2019-08-22 stsp if (error)
4052 8e7bd50a 2019-08-22 stsp goto done;
4053 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4054 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4055 8e7bd50a 2019-08-22 stsp if (error)
4056 8e7bd50a 2019-08-22 stsp goto done;
4057 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4058 8e7bd50a 2019-08-22 stsp free(commit_id);
4059 8e7bd50a 2019-08-22 stsp if (error)
4060 8e7bd50a 2019-08-22 stsp goto done;
4063 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4064 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4068 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4069 8e7bd50a 2019-08-22 stsp if (worktree)
4070 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4071 8e7bd50a 2019-08-22 stsp free(cwd);
4072 8e7bd50a 2019-08-22 stsp free(repo_path);
4073 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4074 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4075 8e7bd50a 2019-08-22 stsp return error;
4078 8e7bd50a 2019-08-22 stsp __dead static void
4079 d00136be 2019-03-26 stsp usage_add(void)
4081 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4085 d00136be 2019-03-26 stsp static const struct got_error *
4086 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4088 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4089 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4090 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4091 1dd54920 2019-05-11 stsp char *cwd = NULL;
4092 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4093 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4096 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4098 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4099 d00136be 2019-03-26 stsp switch (ch) {
4101 d00136be 2019-03-26 stsp usage_add();
4102 d00136be 2019-03-26 stsp /* NOTREACHED */
4106 d00136be 2019-03-26 stsp argc -= optind;
4107 d00136be 2019-03-26 stsp argv += optind;
4109 43012d58 2019-07-14 stsp #ifndef PROFILE
4110 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4111 43012d58 2019-07-14 stsp NULL) == -1)
4112 43012d58 2019-07-14 stsp err(1, "pledge");
4114 723c305c 2019-05-11 jcs if (argc < 1)
4115 d00136be 2019-03-26 stsp usage_add();
4117 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4118 d00136be 2019-03-26 stsp if (cwd == NULL) {
4119 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4120 d00136be 2019-03-26 stsp goto done;
4123 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4124 d00136be 2019-03-26 stsp if (error)
4125 d00136be 2019-03-26 stsp goto done;
4127 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4129 031a5338 2019-03-26 stsp if (error != NULL)
4130 031a5338 2019-03-26 stsp goto done;
4132 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4133 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4134 d00136be 2019-03-26 stsp if (error)
4135 d00136be 2019-03-26 stsp goto done;
4137 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4138 6d022e97 2019-08-04 stsp if (error)
4139 6d022e97 2019-08-04 stsp goto done;
4141 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
4142 1dd54920 2019-05-11 stsp NULL, repo);
4145 031a5338 2019-03-26 stsp got_repo_close(repo);
4146 d00136be 2019-03-26 stsp if (worktree)
4147 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4148 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4149 1dd54920 2019-05-11 stsp free((char *)pe->path);
4150 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4151 2ec1f75b 2019-03-26 stsp free(cwd);
4152 2ec1f75b 2019-03-26 stsp return error;
4155 2ec1f75b 2019-03-26 stsp __dead static void
4156 648e4ef7 2019-07-09 stsp usage_remove(void)
4158 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4162 2a06fe5f 2019-08-24 stsp static const struct got_error *
4163 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4164 2a06fe5f 2019-08-24 stsp unsigned char staged_status, const char *path,
4165 2a06fe5f 2019-08-24 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4166 2a06fe5f 2019-08-24 stsp struct got_object_id *commit_id)
4168 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4169 2a06fe5f 2019-08-24 stsp return NULL;
4170 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4171 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4172 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4173 2a06fe5f 2019-08-24 stsp return NULL;
4176 2ec1f75b 2019-03-26 stsp static const struct got_error *
4177 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4179 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4180 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4181 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4182 17ed4618 2019-06-02 stsp char *cwd = NULL;
4183 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4184 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4185 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
4187 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4189 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
4190 2ec1f75b 2019-03-26 stsp switch (ch) {
4192 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4195 2ec1f75b 2019-03-26 stsp usage_add();
4196 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4200 2ec1f75b 2019-03-26 stsp argc -= optind;
4201 2ec1f75b 2019-03-26 stsp argv += optind;
4203 43012d58 2019-07-14 stsp #ifndef PROFILE
4204 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4205 43012d58 2019-07-14 stsp NULL) == -1)
4206 43012d58 2019-07-14 stsp err(1, "pledge");
4208 17ed4618 2019-06-02 stsp if (argc < 1)
4209 648e4ef7 2019-07-09 stsp usage_remove();
4211 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4212 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4213 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4214 2ec1f75b 2019-03-26 stsp goto done;
4216 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4217 2ec1f75b 2019-03-26 stsp if (error)
4218 2ec1f75b 2019-03-26 stsp goto done;
4220 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4223 2ec1f75b 2019-03-26 stsp goto done;
4225 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4226 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4227 2ec1f75b 2019-03-26 stsp if (error)
4228 2ec1f75b 2019-03-26 stsp goto done;
4230 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4231 6d022e97 2019-08-04 stsp if (error)
4232 6d022e97 2019-08-04 stsp goto done;
4234 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4235 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4236 a129376b 2019-03-28 stsp if (error)
4237 a129376b 2019-03-28 stsp goto done;
4240 a129376b 2019-03-28 stsp got_repo_close(repo);
4241 a129376b 2019-03-28 stsp if (worktree)
4242 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4243 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4244 17ed4618 2019-06-02 stsp free((char *)pe->path);
4245 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4246 a129376b 2019-03-28 stsp free(cwd);
4247 a129376b 2019-03-28 stsp return error;
4250 a129376b 2019-03-28 stsp __dead static void
4251 a129376b 2019-03-28 stsp usage_revert(void)
4253 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4254 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4258 1ee397ad 2019-07-12 stsp static const struct got_error *
4259 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4261 a129376b 2019-03-28 stsp while (path[0] == '/')
4263 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4264 33aa809d 2019-08-08 stsp return NULL;
4267 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4268 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4269 33aa809d 2019-08-08 stsp const char *action;
4272 33aa809d 2019-08-08 stsp static const struct got_error *
4273 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4274 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4276 33aa809d 2019-08-08 stsp char *line = NULL;
4277 33aa809d 2019-08-08 stsp size_t linesize = 0;
4278 33aa809d 2019-08-08 stsp ssize_t linelen;
4280 33aa809d 2019-08-08 stsp switch (status) {
4281 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4282 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4284 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4285 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4287 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4288 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4289 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4290 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4291 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4292 33aa809d 2019-08-08 stsp printf("%s", line);
4293 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4294 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4295 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4296 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4297 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4300 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4303 33aa809d 2019-08-08 stsp return NULL;
4306 33aa809d 2019-08-08 stsp static const struct got_error *
4307 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4308 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4310 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4311 33aa809d 2019-08-08 stsp char *line = NULL;
4312 33aa809d 2019-08-08 stsp size_t linesize = 0;
4313 33aa809d 2019-08-08 stsp ssize_t linelen;
4314 33aa809d 2019-08-08 stsp int resp = ' ';
4315 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4317 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4319 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4321 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4322 33aa809d 2019-08-08 stsp a->action);
4324 33aa809d 2019-08-08 stsp return err;
4325 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4326 33aa809d 2019-08-08 stsp if (linelen == -1) {
4327 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4328 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4329 33aa809d 2019-08-08 stsp return NULL;
4331 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4333 33aa809d 2019-08-08 stsp *nl = '\0';
4334 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4335 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4336 33aa809d 2019-08-08 stsp printf("y\n");
4337 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4338 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4339 33aa809d 2019-08-08 stsp printf("n\n");
4340 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4341 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4342 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4343 33aa809d 2019-08-08 stsp printf("q\n");
4345 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4346 33aa809d 2019-08-08 stsp free(line);
4347 33aa809d 2019-08-08 stsp return NULL;
4350 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4351 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4352 33aa809d 2019-08-08 stsp a->action);
4354 33aa809d 2019-08-08 stsp return err;
4355 33aa809d 2019-08-08 stsp resp = getchar();
4356 33aa809d 2019-08-08 stsp if (resp == '\n')
4357 33aa809d 2019-08-08 stsp resp = getchar();
4358 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4359 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4360 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4361 33aa809d 2019-08-08 stsp resp = ' ';
4363 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4364 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4365 33aa809d 2019-08-08 stsp resp = ' ';
4369 33aa809d 2019-08-08 stsp if (resp == 'y')
4370 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4371 33aa809d 2019-08-08 stsp else if (resp == 'n')
4372 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4373 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4374 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4376 1ee397ad 2019-07-12 stsp return NULL;
4380 a129376b 2019-03-28 stsp static const struct got_error *
4381 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4383 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4384 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4385 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4386 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4387 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4388 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4389 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4390 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4391 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4392 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4394 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4396 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4397 a129376b 2019-03-28 stsp switch (ch) {
4399 33aa809d 2019-08-08 stsp pflag = 1;
4402 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4405 0f6d7415 2019-08-08 stsp can_recurse = 1;
4408 a129376b 2019-03-28 stsp usage_revert();
4409 a129376b 2019-03-28 stsp /* NOTREACHED */
4413 a129376b 2019-03-28 stsp argc -= optind;
4414 a129376b 2019-03-28 stsp argv += optind;
4416 43012d58 2019-07-14 stsp #ifndef PROFILE
4417 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4418 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4419 43012d58 2019-07-14 stsp err(1, "pledge");
4421 e20a8b6f 2019-06-04 stsp if (argc < 1)
4422 a129376b 2019-03-28 stsp usage_revert();
4423 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4424 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4426 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4427 a129376b 2019-03-28 stsp if (cwd == NULL) {
4428 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4429 a129376b 2019-03-28 stsp goto done;
4431 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4432 2ec1f75b 2019-03-26 stsp if (error)
4433 2ec1f75b 2019-03-26 stsp goto done;
4435 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4437 a129376b 2019-03-28 stsp if (error != NULL)
4438 a129376b 2019-03-28 stsp goto done;
4440 33aa809d 2019-08-08 stsp if (patch_script_path) {
4441 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4442 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4443 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4444 33aa809d 2019-08-08 stsp patch_script_path);
4445 33aa809d 2019-08-08 stsp goto done;
4448 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4449 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4450 a129376b 2019-03-28 stsp if (error)
4451 a129376b 2019-03-28 stsp goto done;
4453 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4454 6d022e97 2019-08-04 stsp if (error)
4455 6d022e97 2019-08-04 stsp goto done;
4457 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4458 0f6d7415 2019-08-08 stsp char *ondisk_path;
4459 0f6d7415 2019-08-08 stsp struct stat sb;
4460 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4461 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4462 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4463 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4464 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4465 0f6d7415 2019-08-08 stsp goto done;
4467 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4468 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4469 0f6d7415 2019-08-08 stsp free(ondisk_path);
4472 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4473 0f6d7415 2019-08-08 stsp ondisk_path);
4474 0f6d7415 2019-08-08 stsp free(ondisk_path);
4475 0f6d7415 2019-08-08 stsp goto done;
4477 0f6d7415 2019-08-08 stsp free(ondisk_path);
4478 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4479 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4480 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4481 0f6d7415 2019-08-08 stsp goto done;
4486 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4487 33aa809d 2019-08-08 stsp cpa.action = "revert";
4488 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4489 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4490 a129376b 2019-03-28 stsp if (error)
4491 a129376b 2019-03-28 stsp goto done;
4493 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4494 33aa809d 2019-08-08 stsp error == NULL)
4495 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4497 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4498 2ec1f75b 2019-03-26 stsp if (worktree)
4499 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4500 2ec1f75b 2019-03-26 stsp free(path);
4501 d00136be 2019-03-26 stsp free(cwd);
4502 6bad629b 2019-02-04 stsp return error;
4505 c4296144 2019-05-09 stsp __dead static void
4506 c4296144 2019-05-09 stsp usage_commit(void)
4508 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4509 5c1e53bc 2019-07-28 stsp getprogname());
4513 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4514 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4515 e2ba3d07 2019-05-13 stsp const char *editor;
4516 e0870e44 2019-05-13 stsp const char *worktree_path;
4517 76d98825 2019-06-03 stsp const char *branch_name;
4518 314a6357 2019-05-13 stsp const char *repo_path;
4519 e0870e44 2019-05-13 stsp char *logmsg_path;
4523 33ad4cbe 2019-05-12 jcs static const struct got_error *
4524 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4527 76d98825 2019-06-03 stsp char *initial_content = NULL;
4528 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4529 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4530 e0870e44 2019-05-13 stsp char *template = NULL;
4531 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4533 33ad4cbe 2019-05-12 jcs size_t len;
4535 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4536 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4537 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4538 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4539 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4540 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4541 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4542 33ad4cbe 2019-05-12 jcs return NULL;
4545 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4546 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4548 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4549 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4550 76d98825 2019-06-03 stsp a->branch_name) == -1)
4551 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4553 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4555 e0870e44 2019-05-13 stsp goto done;
4557 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4559 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4560 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4561 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4562 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4563 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4567 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4569 76d98825 2019-06-03 stsp free(initial_content);
4570 e0870e44 2019-05-13 stsp free(template);
4572 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4573 3ce1b845 2019-07-15 stsp if (err == NULL) {
4574 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4575 3ce1b845 2019-07-15 stsp if (err) {
4576 3ce1b845 2019-07-15 stsp free(*logmsg);
4577 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4580 33ad4cbe 2019-05-12 jcs return err;
4583 c4296144 2019-05-09 stsp static const struct got_error *
4584 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4586 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4587 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4588 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4589 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4590 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4591 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4592 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4593 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4594 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4595 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4597 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4598 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4600 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4601 c4296144 2019-05-09 stsp switch (ch) {
4603 c4296144 2019-05-09 stsp logmsg = optarg;
4606 c4296144 2019-05-09 stsp usage_commit();
4607 c4296144 2019-05-09 stsp /* NOTREACHED */
4611 c4296144 2019-05-09 stsp argc -= optind;
4612 c4296144 2019-05-09 stsp argv += optind;
4614 43012d58 2019-07-14 stsp #ifndef PROFILE
4615 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4616 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4617 43012d58 2019-07-14 stsp err(1, "pledge");
4619 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4620 c4296144 2019-05-09 stsp if (cwd == NULL) {
4621 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4622 c4296144 2019-05-09 stsp goto done;
4624 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4625 7d5807f4 2019-07-11 stsp if (error)
4626 7d5807f4 2019-07-11 stsp goto done;
4628 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4629 c4296144 2019-05-09 stsp if (error)
4630 a698f62e 2019-07-25 stsp goto done;
4631 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4632 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4633 c4296144 2019-05-09 stsp goto done;
4636 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4637 916f288c 2019-07-30 stsp worktree);
4638 5c1e53bc 2019-07-28 stsp if (error)
4639 5c1e53bc 2019-07-28 stsp goto done;
4641 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4642 c9956ddf 2019-09-08 stsp if (error)
4643 c9956ddf 2019-09-08 stsp goto done;
4644 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4645 c9956ddf 2019-09-08 stsp gitconfig_path);
4646 c4296144 2019-05-09 stsp if (error != NULL)
4647 c4296144 2019-05-09 stsp goto done;
4649 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4650 aba9c984 2019-09-08 stsp if (error)
4651 aba9c984 2019-09-08 stsp return error;
4654 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4655 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4657 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4658 314a6357 2019-05-13 stsp error = get_editor(&editor);
4660 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4661 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4662 c4296144 2019-05-09 stsp if (error)
4663 c4296144 2019-05-09 stsp goto done;
4665 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4666 087fb88c 2019-08-04 stsp if (error)
4667 087fb88c 2019-08-04 stsp goto done;
4669 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4670 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4671 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4672 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4673 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4674 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4675 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4676 916f288c 2019-07-30 stsp goto done;
4678 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4680 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4681 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4682 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4683 e0870e44 2019-05-13 stsp if (error) {
4684 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4685 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4686 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4687 c4296144 2019-05-09 stsp goto done;
4690 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4691 c4296144 2019-05-09 stsp if (error)
4692 c4296144 2019-05-09 stsp goto done;
4693 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4695 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4696 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4697 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4698 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4699 7266f21f 2019-10-21 stsp error == NULL)
4700 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4701 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4703 c4296144 2019-05-09 stsp got_repo_close(repo);
4704 c4296144 2019-05-09 stsp if (worktree)
4705 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4706 c4296144 2019-05-09 stsp free(cwd);
4707 c4296144 2019-05-09 stsp free(id_str);
4708 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4709 e2ba3d07 2019-05-13 stsp free(editor);
4710 aba9c984 2019-09-08 stsp free(author);
4711 234035bc 2019-06-01 stsp return error;
4714 234035bc 2019-06-01 stsp __dead static void
4715 234035bc 2019-06-01 stsp usage_cherrypick(void)
4717 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4721 234035bc 2019-06-01 stsp static const struct got_error *
4722 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4724 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4725 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4726 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4727 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4728 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4729 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4730 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4731 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4732 234035bc 2019-06-01 stsp int ch, did_something = 0;
4734 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4735 234035bc 2019-06-01 stsp switch (ch) {
4737 234035bc 2019-06-01 stsp usage_cherrypick();
4738 234035bc 2019-06-01 stsp /* NOTREACHED */
4742 234035bc 2019-06-01 stsp argc -= optind;
4743 234035bc 2019-06-01 stsp argv += optind;
4745 43012d58 2019-07-14 stsp #ifndef PROFILE
4746 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4747 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4748 43012d58 2019-07-14 stsp err(1, "pledge");
4750 234035bc 2019-06-01 stsp if (argc != 1)
4751 234035bc 2019-06-01 stsp usage_cherrypick();
4753 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4754 234035bc 2019-06-01 stsp if (cwd == NULL) {
4755 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4756 234035bc 2019-06-01 stsp goto done;
4758 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4759 234035bc 2019-06-01 stsp if (error)
4760 234035bc 2019-06-01 stsp goto done;
4762 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4764 234035bc 2019-06-01 stsp if (error != NULL)
4765 234035bc 2019-06-01 stsp goto done;
4767 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4768 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4769 234035bc 2019-06-01 stsp if (error)
4770 234035bc 2019-06-01 stsp goto done;
4772 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4773 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4774 234035bc 2019-06-01 stsp if (error != NULL) {
4775 234035bc 2019-06-01 stsp struct got_reference *ref;
4776 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4777 234035bc 2019-06-01 stsp goto done;
4778 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4779 234035bc 2019-06-01 stsp if (error != NULL)
4780 234035bc 2019-06-01 stsp goto done;
4781 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4782 234035bc 2019-06-01 stsp got_ref_close(ref);
4783 234035bc 2019-06-01 stsp if (error != NULL)
4784 234035bc 2019-06-01 stsp goto done;
4786 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4787 234035bc 2019-06-01 stsp if (error)
4788 234035bc 2019-06-01 stsp goto done;
4790 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4791 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4792 234035bc 2019-06-01 stsp if (error != NULL)
4793 234035bc 2019-06-01 stsp goto done;
4795 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4796 234035bc 2019-06-01 stsp if (error) {
4797 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4798 234035bc 2019-06-01 stsp goto done;
4799 234035bc 2019-06-01 stsp error = NULL;
4801 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4802 234035bc 2019-06-01 stsp goto done;
4805 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4806 234035bc 2019-06-01 stsp if (error)
4807 234035bc 2019-06-01 stsp goto done;
4808 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4809 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4810 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4812 234035bc 2019-06-01 stsp if (error != NULL)
4813 234035bc 2019-06-01 stsp goto done;
4815 234035bc 2019-06-01 stsp if (did_something)
4816 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4818 234035bc 2019-06-01 stsp if (commit)
4819 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4820 234035bc 2019-06-01 stsp free(commit_id_str);
4821 234035bc 2019-06-01 stsp if (head_ref)
4822 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4823 234035bc 2019-06-01 stsp if (worktree)
4824 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4826 234035bc 2019-06-01 stsp got_repo_close(repo);
4827 c4296144 2019-05-09 stsp return error;
4830 5ef14e63 2019-06-02 stsp __dead static void
4831 5ef14e63 2019-06-02 stsp usage_backout(void)
4833 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4837 5ef14e63 2019-06-02 stsp static const struct got_error *
4838 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4840 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4841 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4842 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4843 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4844 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4845 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4846 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4847 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4848 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4850 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4851 5ef14e63 2019-06-02 stsp switch (ch) {
4853 5ef14e63 2019-06-02 stsp usage_backout();
4854 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4858 5ef14e63 2019-06-02 stsp argc -= optind;
4859 5ef14e63 2019-06-02 stsp argv += optind;
4861 43012d58 2019-07-14 stsp #ifndef PROFILE
4862 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4863 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4864 43012d58 2019-07-14 stsp err(1, "pledge");
4866 5ef14e63 2019-06-02 stsp if (argc != 1)
4867 5ef14e63 2019-06-02 stsp usage_backout();
4869 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4870 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4871 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4872 5ef14e63 2019-06-02 stsp goto done;
4874 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4875 5ef14e63 2019-06-02 stsp if (error)
4876 5ef14e63 2019-06-02 stsp goto done;
4878 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4880 5ef14e63 2019-06-02 stsp if (error != NULL)
4881 5ef14e63 2019-06-02 stsp goto done;
4883 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4884 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4885 5ef14e63 2019-06-02 stsp if (error)
4886 5ef14e63 2019-06-02 stsp goto done;
4888 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4889 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4890 5ef14e63 2019-06-02 stsp if (error != NULL) {
4891 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4892 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4893 5ef14e63 2019-06-02 stsp goto done;
4894 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4895 5ef14e63 2019-06-02 stsp if (error != NULL)
4896 5ef14e63 2019-06-02 stsp goto done;
4897 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4898 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4899 5ef14e63 2019-06-02 stsp if (error != NULL)
4900 5ef14e63 2019-06-02 stsp goto done;
4902 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4903 5ef14e63 2019-06-02 stsp if (error)
4904 5ef14e63 2019-06-02 stsp goto done;
4906 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4907 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4908 5ef14e63 2019-06-02 stsp if (error != NULL)
4909 5ef14e63 2019-06-02 stsp goto done;
4911 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4912 5ef14e63 2019-06-02 stsp if (error)
4913 5ef14e63 2019-06-02 stsp goto done;
4915 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4916 5ef14e63 2019-06-02 stsp if (error)
4917 5ef14e63 2019-06-02 stsp goto done;
4918 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4919 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4920 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4921 5ef14e63 2019-06-02 stsp goto done;
4924 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4925 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4926 5ef14e63 2019-06-02 stsp if (error != NULL)
4927 5ef14e63 2019-06-02 stsp goto done;
4929 5ef14e63 2019-06-02 stsp if (did_something)
4930 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4932 5ef14e63 2019-06-02 stsp if (commit)
4933 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4934 5ef14e63 2019-06-02 stsp free(commit_id_str);
4935 5ef14e63 2019-06-02 stsp if (head_ref)
4936 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4937 818c7501 2019-07-11 stsp if (worktree)
4938 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4940 818c7501 2019-07-11 stsp got_repo_close(repo);
4941 818c7501 2019-07-11 stsp return error;
4944 818c7501 2019-07-11 stsp __dead static void
4945 818c7501 2019-07-11 stsp usage_rebase(void)
4947 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4948 af54c8f8 2019-07-11 stsp getprogname());
4953 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4956 0ebf8283 2019-07-24 stsp size_t len;
4958 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4959 0ebf8283 2019-07-24 stsp if (len > limit)
4960 0ebf8283 2019-07-24 stsp len = limit;
4961 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4962 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4964 0ebf8283 2019-07-24 stsp *nl = '\0';
4967 0ebf8283 2019-07-24 stsp static const struct got_error *
4968 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4970 5943eee2 2019-08-13 stsp const struct got_error *err;
4971 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4972 5943eee2 2019-08-13 stsp const char *s;
4974 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4976 5943eee2 2019-08-13 stsp return err;
4978 5943eee2 2019-08-13 stsp s = logmsg0;
4979 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4982 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4983 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4984 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4985 5943eee2 2019-08-13 stsp goto done;
4988 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4990 5943eee2 2019-08-13 stsp free(logmsg0);
4991 5943eee2 2019-08-13 stsp return err;
4994 818c7501 2019-07-11 stsp static const struct got_error *
4995 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4996 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4998 818c7501 2019-07-11 stsp const struct got_error *err;
4999 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5001 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5003 818c7501 2019-07-11 stsp goto done;
5005 ff0d2220 2019-07-11 stsp if (new_id) {
5006 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5008 ff0d2220 2019-07-11 stsp goto done;
5011 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5012 ff0d2220 2019-07-11 stsp if (new_id_str)
5013 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5015 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5017 0ebf8283 2019-07-24 stsp goto done;
5019 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5020 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5022 818c7501 2019-07-11 stsp free(old_id_str);
5023 818c7501 2019-07-11 stsp free(new_id_str);
5024 818c7501 2019-07-11 stsp return err;
5027 1ee397ad 2019-07-12 stsp static const struct got_error *
5028 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5030 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5032 818c7501 2019-07-11 stsp while (path[0] == '/')
5034 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5036 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5037 1ee397ad 2019-07-12 stsp return NULL;
5038 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5039 818c7501 2019-07-11 stsp *rebase_status = status;
5040 1ee397ad 2019-07-12 stsp return NULL;
5043 818c7501 2019-07-11 stsp static const struct got_error *
5044 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5045 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5046 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5048 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5049 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5050 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5053 818c7501 2019-07-11 stsp static const struct got_error *
5054 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5055 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5056 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5057 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5059 ff0d2220 2019-07-11 stsp const struct got_error *error;
5060 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5061 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5063 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5064 ff0d2220 2019-07-11 stsp if (error)
5065 ff0d2220 2019-07-11 stsp return error;
5067 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5068 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5069 ff0d2220 2019-07-11 stsp if (error) {
5070 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5071 ff0d2220 2019-07-11 stsp goto done;
5072 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5074 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5075 ff0d2220 2019-07-11 stsp free(new_commit_id);
5078 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5079 ff0d2220 2019-07-11 stsp return error;
5082 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5083 64c6d990 2019-07-11 stsp const char *path_prefix;
5084 64c6d990 2019-07-11 stsp size_t len;
5085 8ca9bd68 2019-07-25 stsp int errcode;
5088 64c6d990 2019-07-11 stsp static const struct got_error *
5089 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5090 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5091 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5092 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5094 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5096 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5097 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5098 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5100 64c6d990 2019-07-11 stsp return NULL;
5103 64c6d990 2019-07-11 stsp static const struct got_error *
5104 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5105 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5106 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5108 64c6d990 2019-07-11 stsp const struct got_error *err;
5109 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5110 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5111 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5113 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5114 64c6d990 2019-07-11 stsp return NULL;
5116 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5118 64c6d990 2019-07-11 stsp goto done;
5120 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5122 64c6d990 2019-07-11 stsp goto done;
5124 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5125 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5127 64c6d990 2019-07-11 stsp goto done;
5129 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5130 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5132 64c6d990 2019-07-11 stsp goto done;
5134 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5135 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5136 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5137 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5138 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5139 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5140 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5142 64c6d990 2019-07-11 stsp if (tree1)
5143 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5144 64c6d990 2019-07-11 stsp if (tree2)
5145 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5146 64c6d990 2019-07-11 stsp if (commit)
5147 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5148 64c6d990 2019-07-11 stsp if (parent_commit)
5149 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5150 64c6d990 2019-07-11 stsp return err;
5153 ff0d2220 2019-07-11 stsp static const struct got_error *
5154 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5155 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5156 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5157 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5158 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5160 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5161 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5162 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5163 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5164 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5166 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5168 af61c510 2019-07-19 stsp return err;
5170 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5171 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5173 af61c510 2019-07-19 stsp goto done;
5174 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5175 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5176 af61c510 2019-07-19 stsp if (err) {
5177 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5178 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5179 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5180 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5181 af61c510 2019-07-19 stsp "been reached?!?");
5182 af61c510 2019-07-19 stsp goto done;
5183 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5184 af61c510 2019-07-19 stsp goto done;
5185 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5186 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5188 af61c510 2019-07-19 stsp goto done;
5190 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5191 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5193 af61c510 2019-07-19 stsp goto done;
5195 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5197 af61c510 2019-07-19 stsp goto done;
5198 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5199 af61c510 2019-07-19 stsp commit_id = parent_id;
5203 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5204 af61c510 2019-07-19 stsp return err;
5207 af61c510 2019-07-19 stsp static const struct got_error *
5208 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5210 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5211 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5212 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5213 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5214 818c7501 2019-07-11 stsp char *cwd = NULL;
5215 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5216 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5217 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5218 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5219 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5220 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5221 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5222 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5223 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5224 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5225 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5226 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5228 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5229 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5231 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5232 818c7501 2019-07-11 stsp switch (ch) {
5234 818c7501 2019-07-11 stsp abort_rebase = 1;
5237 818c7501 2019-07-11 stsp continue_rebase = 1;
5240 818c7501 2019-07-11 stsp usage_rebase();
5241 818c7501 2019-07-11 stsp /* NOTREACHED */
5245 818c7501 2019-07-11 stsp argc -= optind;
5246 818c7501 2019-07-11 stsp argv += optind;
5248 43012d58 2019-07-14 stsp #ifndef PROFILE
5249 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5250 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5251 43012d58 2019-07-14 stsp err(1, "pledge");
5253 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5254 818c7501 2019-07-11 stsp usage_rebase();
5255 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5256 818c7501 2019-07-11 stsp if (argc != 0)
5257 818c7501 2019-07-11 stsp usage_rebase();
5258 818c7501 2019-07-11 stsp } else if (argc != 1)
5259 818c7501 2019-07-11 stsp usage_rebase();
5261 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5262 818c7501 2019-07-11 stsp if (cwd == NULL) {
5263 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5264 818c7501 2019-07-11 stsp goto done;
5266 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5267 818c7501 2019-07-11 stsp if (error)
5268 818c7501 2019-07-11 stsp goto done;
5270 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5272 818c7501 2019-07-11 stsp if (error != NULL)
5273 818c7501 2019-07-11 stsp goto done;
5275 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5276 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5277 818c7501 2019-07-11 stsp if (error)
5278 818c7501 2019-07-11 stsp goto done;
5280 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5281 818c7501 2019-07-11 stsp if (error)
5282 818c7501 2019-07-11 stsp goto done;
5284 f6794adc 2019-07-23 stsp if (abort_rebase) {
5285 818c7501 2019-07-11 stsp int did_something;
5286 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5287 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5288 f6794adc 2019-07-23 stsp goto done;
5290 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5291 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5292 3e3a69f1 2019-07-25 stsp worktree, repo);
5293 818c7501 2019-07-11 stsp if (error)
5294 818c7501 2019-07-11 stsp goto done;
5295 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5296 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5297 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5298 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5299 818c7501 2019-07-11 stsp if (error)
5300 818c7501 2019-07-11 stsp goto done;
5301 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5302 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5305 818c7501 2019-07-11 stsp if (continue_rebase) {
5306 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5307 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5308 f6794adc 2019-07-23 stsp goto done;
5310 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5311 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5312 3e3a69f1 2019-07-25 stsp worktree, repo);
5313 818c7501 2019-07-11 stsp if (error)
5314 818c7501 2019-07-11 stsp goto done;
5316 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5317 01757395 2019-07-12 stsp resume_commit_id, repo);
5318 818c7501 2019-07-11 stsp if (error)
5319 818c7501 2019-07-11 stsp goto done;
5321 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5322 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5323 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5324 818c7501 2019-07-11 stsp goto done;
5327 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5328 818c7501 2019-07-11 stsp if (error != NULL)
5329 818c7501 2019-07-11 stsp goto done;
5332 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5333 ff0d2220 2019-07-11 stsp if (error)
5334 ff0d2220 2019-07-11 stsp goto done;
5336 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5337 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5339 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5340 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5341 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5342 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5343 818c7501 2019-07-11 stsp if (error)
5344 818c7501 2019-07-11 stsp goto done;
5345 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5346 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5347 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5348 818c7501 2019-07-11 stsp "with work tree's branch");
5349 818c7501 2019-07-11 stsp goto done;
5352 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5353 a51a74b3 2019-07-27 stsp if (error) {
5354 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5355 a51a74b3 2019-07-27 stsp goto done;
5356 a51a74b3 2019-07-27 stsp error = NULL;
5358 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5359 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5360 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5361 a51a74b3 2019-07-27 stsp goto done;
5363 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5364 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5365 818c7501 2019-07-11 stsp if (error)
5366 818c7501 2019-07-11 stsp goto done;
5369 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5370 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5371 818c7501 2019-07-11 stsp if (error)
5372 818c7501 2019-07-11 stsp goto done;
5374 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5375 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5376 fc66b545 2019-08-12 stsp if (pid == NULL) {
5377 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5378 fc66b545 2019-08-12 stsp int did_something;
5379 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5380 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5381 fc66b545 2019-08-12 stsp &did_something);
5382 fc66b545 2019-08-12 stsp if (error)
5383 fc66b545 2019-08-12 stsp goto done;
5384 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5385 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5387 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5388 fc66b545 2019-08-12 stsp goto done;
5390 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5391 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5392 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5393 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5394 818c7501 2019-07-11 stsp commit = NULL;
5395 818c7501 2019-07-11 stsp if (error)
5396 818c7501 2019-07-11 stsp goto done;
5398 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5399 ff0d2220 2019-07-11 stsp if (continue_rebase)
5400 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5401 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5403 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5404 818c7501 2019-07-11 stsp goto done;
5407 818c7501 2019-07-11 stsp pid = NULL;
5408 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5409 818c7501 2019-07-11 stsp commit_id = qid->id;
5410 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5411 818c7501 2019-07-11 stsp pid = qid;
5413 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5414 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5415 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5416 818c7501 2019-07-11 stsp if (error)
5417 818c7501 2019-07-11 stsp goto done;
5419 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5420 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5424 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5425 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5426 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5427 818c7501 2019-07-11 stsp if (error)
5428 818c7501 2019-07-11 stsp goto done;
5431 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5432 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5433 818c7501 2019-07-11 stsp if (error)
5434 818c7501 2019-07-11 stsp goto done;
5435 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5436 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5438 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5439 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5441 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5442 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5443 818c7501 2019-07-11 stsp free(resume_commit_id);
5444 818c7501 2019-07-11 stsp free(yca_id);
5445 818c7501 2019-07-11 stsp if (commit)
5446 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5447 818c7501 2019-07-11 stsp if (branch)
5448 818c7501 2019-07-11 stsp got_ref_close(branch);
5449 818c7501 2019-07-11 stsp if (new_base_branch)
5450 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5451 818c7501 2019-07-11 stsp if (tmp_branch)
5452 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5453 5ef14e63 2019-06-02 stsp if (worktree)
5454 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5456 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5457 5ef14e63 2019-06-02 stsp return error;
5460 0ebf8283 2019-07-24 stsp __dead static void
5461 0ebf8283 2019-07-24 stsp usage_histedit(void)
5463 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5464 0ebf8283 2019-07-24 stsp getprogname());
5468 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5469 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5470 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5471 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5472 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5474 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5475 0ebf8283 2019-07-24 stsp unsigned char code;
5476 0ebf8283 2019-07-24 stsp const char *name;
5477 0ebf8283 2019-07-24 stsp const char *desc;
5478 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5479 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5480 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5481 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5482 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5483 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5484 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5487 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5488 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5489 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5490 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5491 0ebf8283 2019-07-24 stsp char *logmsg;
5493 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5495 0ebf8283 2019-07-24 stsp static const struct got_error *
5496 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5497 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5499 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5500 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5501 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5504 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5506 0ebf8283 2019-07-24 stsp goto done;
5508 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5510 0ebf8283 2019-07-24 stsp goto done;
5512 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5514 0ebf8283 2019-07-24 stsp goto done;
5516 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5517 0ebf8283 2019-07-24 stsp if (n < 0)
5518 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5520 0ebf8283 2019-07-24 stsp if (commit)
5521 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5522 0ebf8283 2019-07-24 stsp free(id_str);
5523 0ebf8283 2019-07-24 stsp free(logmsg);
5524 0ebf8283 2019-07-24 stsp return err;
5527 0ebf8283 2019-07-24 stsp static const struct got_error *
5528 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5529 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5531 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5532 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5534 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5535 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5537 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5538 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5544 0ebf8283 2019-07-24 stsp return err;
5547 0ebf8283 2019-07-24 stsp static const struct got_error *
5548 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5550 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5553 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5554 0ebf8283 2019-07-24 stsp if (n < 0)
5555 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5557 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5558 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5559 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5560 0ebf8283 2019-07-24 stsp cmd->desc);
5561 0ebf8283 2019-07-24 stsp if (n < 0) {
5562 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5566 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5567 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5568 0ebf8283 2019-07-24 stsp if (n < 0)
5569 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5570 0ebf8283 2019-07-24 stsp return err;
5573 0ebf8283 2019-07-24 stsp static const struct got_error *
5574 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5576 0ebf8283 2019-07-24 stsp static char msg[42];
5579 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5581 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5582 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5584 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5587 0ebf8283 2019-07-24 stsp static const struct got_error *
5588 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5589 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5591 0ebf8283 2019-07-24 stsp const struct got_error *err;
5592 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5593 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5595 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5597 0ebf8283 2019-07-24 stsp return err;
5599 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5601 0ebf8283 2019-07-24 stsp goto done;
5603 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5605 5943eee2 2019-08-13 stsp goto done;
5606 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5607 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5608 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5609 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5610 0ebf8283 2019-07-24 stsp goto done;
5613 0ebf8283 2019-07-24 stsp if (folded_commit)
5614 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5615 0ebf8283 2019-07-24 stsp free(id_str);
5616 5943eee2 2019-08-13 stsp free(folded_logmsg);
5617 0ebf8283 2019-07-24 stsp return err;
5620 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5621 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5623 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5625 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5626 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5627 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5628 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5629 3f9de99f 2019-07-24 stsp folded = prev;
5630 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5633 0ebf8283 2019-07-24 stsp return folded;
5636 0ebf8283 2019-07-24 stsp static const struct got_error *
5637 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5638 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5640 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5641 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5642 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5643 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5645 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5647 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5649 0ebf8283 2019-07-24 stsp return err;
5651 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5652 0ebf8283 2019-07-24 stsp if (folded) {
5653 0ebf8283 2019-07-24 stsp while (folded != hle) {
5654 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5655 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5658 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5659 0ebf8283 2019-07-24 stsp logmsg, repo);
5661 0ebf8283 2019-07-24 stsp goto done;
5662 0ebf8283 2019-07-24 stsp free(logmsg);
5663 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5664 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5668 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5670 0ebf8283 2019-07-24 stsp goto done;
5671 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5673 5943eee2 2019-08-13 stsp goto done;
5674 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5675 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5676 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5677 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5678 0ebf8283 2019-07-24 stsp goto done;
5680 0ebf8283 2019-07-24 stsp free(logmsg);
5681 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5683 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5685 0ebf8283 2019-07-24 stsp goto done;
5687 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5689 0ebf8283 2019-07-24 stsp goto done;
5691 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5692 0ebf8283 2019-07-24 stsp close(fd);
5694 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5696 0ebf8283 2019-07-24 stsp goto done;
5698 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5699 0ebf8283 2019-07-24 stsp if (err) {
5700 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5701 0ebf8283 2019-07-24 stsp goto done;
5702 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5705 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5706 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5707 0ebf8283 2019-07-24 stsp free(logmsg_path);
5708 0ebf8283 2019-07-24 stsp free(logmsg);
5709 5943eee2 2019-08-13 stsp free(orig_logmsg);
5710 0ebf8283 2019-07-24 stsp free(editor);
5711 0ebf8283 2019-07-24 stsp if (commit)
5712 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5713 0ebf8283 2019-07-24 stsp return err;
5716 0ebf8283 2019-07-24 stsp static const struct got_error *
5717 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5718 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5720 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5721 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5722 0ebf8283 2019-07-24 stsp size_t size;
5723 0ebf8283 2019-07-24 stsp ssize_t len;
5724 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5725 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5726 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5727 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5729 0ebf8283 2019-07-24 stsp for (;;) {
5730 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5731 0ebf8283 2019-07-24 stsp if (len == -1) {
5732 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5733 0ebf8283 2019-07-24 stsp if (feof(f))
5735 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5736 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5741 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5743 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5744 0ebf8283 2019-07-24 stsp free(line);
5745 0ebf8283 2019-07-24 stsp line = NULL;
5748 0ebf8283 2019-07-24 stsp cmd = NULL;
5749 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5750 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5751 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5752 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5753 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5756 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5761 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5762 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5765 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5767 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5768 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5769 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5772 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5773 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5777 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5778 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5779 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5783 0ebf8283 2019-07-24 stsp free(line);
5784 0ebf8283 2019-07-24 stsp line = NULL;
5788 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5790 0ebf8283 2019-07-24 stsp *end = '\0';
5792 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5793 0ebf8283 2019-07-24 stsp if (err) {
5794 0ebf8283 2019-07-24 stsp /* override error code */
5795 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5799 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5800 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5801 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5804 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5805 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5806 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5807 0ebf8283 2019-07-24 stsp commit_id = NULL;
5808 0ebf8283 2019-07-24 stsp free(line);
5809 0ebf8283 2019-07-24 stsp line = NULL;
5810 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5813 0ebf8283 2019-07-24 stsp free(line);
5814 0ebf8283 2019-07-24 stsp free(commit_id);
5815 0ebf8283 2019-07-24 stsp return err;
5818 0ebf8283 2019-07-24 stsp static const struct got_error *
5819 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5820 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5822 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5823 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5824 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5825 bfce7f83 2019-07-27 stsp static char msg[80];
5826 bfce7f83 2019-07-27 stsp char *id_str;
5828 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5829 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5830 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5831 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5832 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5834 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5835 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5836 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5839 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5840 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5842 bfce7f83 2019-07-27 stsp return err;
5843 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5844 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5845 bfce7f83 2019-07-27 stsp free(id_str);
5846 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5850 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5851 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5852 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5853 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5855 bfce7f83 2019-07-27 stsp return NULL;
5858 bfce7f83 2019-07-27 stsp static const struct got_error *
5859 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5860 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5861 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5863 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5864 0ebf8283 2019-07-24 stsp char *editor;
5865 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5867 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5869 0ebf8283 2019-07-24 stsp return err;
5871 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5872 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5873 0ebf8283 2019-07-24 stsp goto done;
5876 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5877 0ebf8283 2019-07-24 stsp if (f == NULL) {
5878 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5879 0ebf8283 2019-07-24 stsp goto done;
5881 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5883 bfce7f83 2019-07-27 stsp goto done;
5885 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5887 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5888 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5889 0ebf8283 2019-07-24 stsp free(editor);
5890 0ebf8283 2019-07-24 stsp return err;
5893 0ebf8283 2019-07-24 stsp static const struct got_error *
5894 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5895 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5897 0ebf8283 2019-07-24 stsp static const struct got_error *
5898 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5899 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5901 0ebf8283 2019-07-24 stsp const struct got_error *err;
5902 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5903 0ebf8283 2019-07-24 stsp char *path = NULL;
5905 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5907 0ebf8283 2019-07-24 stsp return err;
5909 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5911 0ebf8283 2019-07-24 stsp goto done;
5913 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5915 0ebf8283 2019-07-24 stsp goto done;
5917 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5918 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5919 0ebf8283 2019-07-24 stsp goto done;
5923 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5924 0ebf8283 2019-07-24 stsp if (err) {
5925 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5926 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5927 0ebf8283 2019-07-24 stsp goto done;
5928 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5929 0ebf8283 2019-07-24 stsp commits, path, repo);
5932 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5933 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5934 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5935 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5936 0ebf8283 2019-07-24 stsp free(path);
5937 0ebf8283 2019-07-24 stsp return err;
5940 0ebf8283 2019-07-24 stsp static const struct got_error *
5941 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5942 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5944 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5945 0ebf8283 2019-07-24 stsp char *path = NULL;
5946 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5947 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5948 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5950 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5952 0ebf8283 2019-07-24 stsp return err;
5954 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5955 0ebf8283 2019-07-24 stsp if (f == NULL) {
5956 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5957 0ebf8283 2019-07-24 stsp goto done;
5959 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5960 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5965 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5966 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5967 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5968 0ebf8283 2019-07-24 stsp if (n < 0) {
5969 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5975 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5976 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5977 0ebf8283 2019-07-24 stsp free(path);
5978 0ebf8283 2019-07-24 stsp if (commit)
5979 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5980 0ebf8283 2019-07-24 stsp return err;
5984 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5986 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5988 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5989 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5990 bfce7f83 2019-07-27 stsp free(hle);
5994 0ebf8283 2019-07-24 stsp static const struct got_error *
5995 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5996 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5998 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5999 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6001 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6002 0ebf8283 2019-07-24 stsp if (f == NULL) {
6003 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6004 0ebf8283 2019-07-24 stsp goto done;
6007 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6009 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6010 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6011 0ebf8283 2019-07-24 stsp return err;
6014 0ebf8283 2019-07-24 stsp static const struct got_error *
6015 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6016 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6017 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6019 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6020 0ebf8283 2019-07-24 stsp int resp = ' ';
6022 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6023 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6024 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6025 0ebf8283 2019-07-24 stsp resp = getchar();
6026 a61a4414 2019-08-07 stsp if (resp == '\n')
6027 a61a4414 2019-08-07 stsp resp = getchar();
6028 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6029 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6030 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6032 426ebf2e 2019-07-25 stsp if (err) {
6033 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6034 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6036 bfce7f83 2019-07-27 stsp prev_err = err;
6037 426ebf2e 2019-07-25 stsp resp = ' ';
6041 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6042 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6043 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6044 0ebf8283 2019-07-24 stsp commits, repo);
6045 426ebf2e 2019-07-25 stsp if (err) {
6046 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6047 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6049 bfce7f83 2019-07-27 stsp prev_err = err;
6050 426ebf2e 2019-07-25 stsp resp = ' ';
6054 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6055 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6058 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6061 0ebf8283 2019-07-24 stsp return err;
6064 0ebf8283 2019-07-24 stsp static const struct got_error *
6065 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6066 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6067 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6069 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6070 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6071 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6072 3e3a69f1 2019-07-25 stsp branch, repo);
6075 0ebf8283 2019-07-24 stsp static const struct got_error *
6076 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6077 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6079 0ebf8283 2019-07-24 stsp const struct got_error *err;
6080 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6082 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6084 0ebf8283 2019-07-24 stsp goto done;
6086 0ebf8283 2019-07-24 stsp if (new_id) {
6087 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6089 0ebf8283 2019-07-24 stsp goto done;
6092 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6093 0ebf8283 2019-07-24 stsp if (new_id_str)
6094 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6096 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6097 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6098 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6099 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6100 0ebf8283 2019-07-24 stsp goto done;
6102 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6104 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6106 0ebf8283 2019-07-24 stsp goto done;
6109 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6110 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6111 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6112 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6113 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6115 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6116 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6117 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6125 0ebf8283 2019-07-24 stsp free(old_id_str);
6126 0ebf8283 2019-07-24 stsp free(new_id_str);
6127 0ebf8283 2019-07-24 stsp return err;
6130 0ebf8283 2019-07-24 stsp static const struct got_error *
6131 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6132 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6133 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6134 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6136 0ebf8283 2019-07-24 stsp const struct got_error *err;
6137 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6138 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6140 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6141 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6142 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6144 0ebf8283 2019-07-24 stsp return err;
6147 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6149 0ebf8283 2019-07-24 stsp return err;
6151 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6152 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6153 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6154 0ebf8283 2019-07-24 stsp if (err) {
6155 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6156 0ebf8283 2019-07-24 stsp goto done;
6157 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6159 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6160 0ebf8283 2019-07-24 stsp free(new_commit_id);
6163 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6164 0ebf8283 2019-07-24 stsp return err;
6167 0ebf8283 2019-07-24 stsp static const struct got_error *
6168 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6169 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6171 0ebf8283 2019-07-24 stsp const struct got_error *error;
6172 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6174 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6176 0ebf8283 2019-07-24 stsp if (error)
6177 0ebf8283 2019-07-24 stsp return error;
6179 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6180 0ebf8283 2019-07-24 stsp if (error)
6181 0ebf8283 2019-07-24 stsp return error;
6183 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6184 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6185 0ebf8283 2019-07-24 stsp return error;
6188 0ebf8283 2019-07-24 stsp static const struct got_error *
6189 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6191 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6192 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6193 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6194 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6195 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6196 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6197 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6198 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6199 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6200 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6201 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6202 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6203 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6204 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6205 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6206 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6207 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6208 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6209 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6210 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6211 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6213 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6214 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6215 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6217 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6218 0ebf8283 2019-07-24 stsp switch (ch) {
6220 0ebf8283 2019-07-24 stsp abort_edit = 1;
6223 0ebf8283 2019-07-24 stsp continue_edit = 1;
6226 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6229 0ebf8283 2019-07-24 stsp usage_histedit();
6230 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6234 0ebf8283 2019-07-24 stsp argc -= optind;
6235 0ebf8283 2019-07-24 stsp argv += optind;
6237 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6238 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6239 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6240 0ebf8283 2019-07-24 stsp err(1, "pledge");
6242 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6243 0ebf8283 2019-07-24 stsp usage_histedit();
6244 0ebf8283 2019-07-24 stsp if (argc != 0)
6245 0ebf8283 2019-07-24 stsp usage_histedit();
6248 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6249 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6250 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6251 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6252 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6253 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6256 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6257 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6258 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6259 0ebf8283 2019-07-24 stsp goto done;
6261 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6262 0ebf8283 2019-07-24 stsp if (error)
6263 0ebf8283 2019-07-24 stsp goto done;
6265 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6267 0ebf8283 2019-07-24 stsp if (error != NULL)
6268 0ebf8283 2019-07-24 stsp goto done;
6270 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6271 0ebf8283 2019-07-24 stsp if (error)
6272 0ebf8283 2019-07-24 stsp goto done;
6273 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6274 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6275 0ebf8283 2019-07-24 stsp goto done;
6278 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6279 0ebf8283 2019-07-24 stsp if (error)
6280 0ebf8283 2019-07-24 stsp goto done;
6282 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6283 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6284 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6285 3e3a69f1 2019-07-25 stsp worktree, repo);
6286 0ebf8283 2019-07-24 stsp if (error)
6287 0ebf8283 2019-07-24 stsp goto done;
6288 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6289 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6290 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6291 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6292 0ebf8283 2019-07-24 stsp if (error)
6293 0ebf8283 2019-07-24 stsp goto done;
6294 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6295 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6296 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6297 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6298 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6299 0ebf8283 2019-07-24 stsp goto done;
6302 0ebf8283 2019-07-24 stsp if (continue_edit) {
6303 0ebf8283 2019-07-24 stsp char *path;
6305 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6306 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6307 0ebf8283 2019-07-24 stsp goto done;
6310 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6311 0ebf8283 2019-07-24 stsp if (error)
6312 0ebf8283 2019-07-24 stsp goto done;
6314 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6315 0ebf8283 2019-07-24 stsp free(path);
6316 0ebf8283 2019-07-24 stsp if (error)
6317 0ebf8283 2019-07-24 stsp goto done;
6319 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6320 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6321 3e3a69f1 2019-07-25 stsp worktree, repo);
6322 0ebf8283 2019-07-24 stsp if (error)
6323 0ebf8283 2019-07-24 stsp goto done;
6325 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6326 0ebf8283 2019-07-24 stsp if (error)
6327 0ebf8283 2019-07-24 stsp goto done;
6329 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6330 0ebf8283 2019-07-24 stsp head_commit_id);
6331 0ebf8283 2019-07-24 stsp if (error)
6332 0ebf8283 2019-07-24 stsp goto done;
6333 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6334 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6335 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6336 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6337 3aac7cf7 2019-07-25 stsp goto done;
6339 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6340 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6341 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6342 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6343 0ebf8283 2019-07-24 stsp commit = NULL;
6344 0ebf8283 2019-07-24 stsp if (error)
6345 0ebf8283 2019-07-24 stsp goto done;
6347 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6348 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6349 0ebf8283 2019-07-24 stsp goto done;
6352 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6353 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6354 0ebf8283 2019-07-24 stsp if (error != NULL)
6355 0ebf8283 2019-07-24 stsp goto done;
6357 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6358 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6359 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6360 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6361 c7d20a3f 2019-07-30 stsp goto done;
6364 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6365 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6366 bfce7f83 2019-07-27 stsp branch = NULL;
6367 0ebf8283 2019-07-24 stsp if (error)
6368 0ebf8283 2019-07-24 stsp goto done;
6370 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6371 0ebf8283 2019-07-24 stsp head_commit_id);
6372 0ebf8283 2019-07-24 stsp if (error)
6373 0ebf8283 2019-07-24 stsp goto done;
6374 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6375 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6376 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6377 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6378 3aac7cf7 2019-07-25 stsp goto done;
6380 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6381 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6382 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6383 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6384 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6385 0ebf8283 2019-07-24 stsp commit = NULL;
6386 0ebf8283 2019-07-24 stsp if (error)
6387 0ebf8283 2019-07-24 stsp goto done;
6389 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6390 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6391 bfce7f83 2019-07-27 stsp if (error)
6392 bfce7f83 2019-07-27 stsp goto done;
6394 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6395 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6396 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6397 6ba2bea2 2019-07-27 stsp if (error) {
6398 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6399 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6400 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6401 0ebf8283 2019-07-24 stsp goto done;
6404 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6406 6ba2bea2 2019-07-27 stsp if (error) {
6407 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6408 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6409 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6410 0ebf8283 2019-07-24 stsp goto done;
6415 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6417 6ba2bea2 2019-07-27 stsp if (error) {
6418 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6419 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6420 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6421 0ebf8283 2019-07-24 stsp goto done;
6426 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6427 4ec14e60 2019-08-28 hiltjo if (error)
6428 0ebf8283 2019-07-24 stsp goto done;
6430 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6431 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6432 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6433 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6436 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6437 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6438 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6439 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6442 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6443 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6445 0ebf8283 2019-07-24 stsp if (error)
6446 0ebf8283 2019-07-24 stsp goto done;
6450 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6451 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6452 0ebf8283 2019-07-24 stsp if (error)
6453 0ebf8283 2019-07-24 stsp goto done;
6457 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6458 0ebf8283 2019-07-24 stsp hle->commit_id);
6459 0ebf8283 2019-07-24 stsp if (error)
6460 0ebf8283 2019-07-24 stsp goto done;
6461 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6462 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6464 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6465 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6466 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6467 0ebf8283 2019-07-24 stsp if (error)
6468 0ebf8283 2019-07-24 stsp goto done;
6469 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6470 0ebf8283 2019-07-24 stsp commit = NULL;
6472 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6473 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6477 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6478 0ebf8283 2019-07-24 stsp char *id_str;
6479 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6480 0ebf8283 2019-07-24 stsp if (error)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6484 0ebf8283 2019-07-24 stsp free(id_str);
6485 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6486 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6487 3e3a69f1 2019-07-25 stsp fileindex);
6488 0ebf8283 2019-07-24 stsp goto done;
6491 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6492 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6493 0ebf8283 2019-07-24 stsp if (error)
6494 0ebf8283 2019-07-24 stsp goto done;
6498 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6499 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6500 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6501 0ebf8283 2019-07-24 stsp if (error)
6502 0ebf8283 2019-07-24 stsp goto done;
6505 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6506 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6507 0ebf8283 2019-07-24 stsp if (error)
6508 0ebf8283 2019-07-24 stsp goto done;
6509 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6510 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6512 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6513 3e3a69f1 2019-07-25 stsp branch, repo);
6515 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6516 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6517 0ebf8283 2019-07-24 stsp free(head_commit_id);
6518 0ebf8283 2019-07-24 stsp free(base_commit_id);
6519 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6520 0ebf8283 2019-07-24 stsp if (commit)
6521 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6522 0ebf8283 2019-07-24 stsp if (branch)
6523 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6524 0ebf8283 2019-07-24 stsp if (tmp_branch)
6525 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6526 0ebf8283 2019-07-24 stsp if (worktree)
6527 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6529 2822a352 2019-10-15 stsp got_repo_close(repo);
6530 2822a352 2019-10-15 stsp return error;
6533 2822a352 2019-10-15 stsp __dead static void
6534 2822a352 2019-10-15 stsp usage_integrate(void)
6536 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6540 2822a352 2019-10-15 stsp static const struct got_error *
6541 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6543 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6544 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6545 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6546 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6547 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6548 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6549 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6550 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6551 2822a352 2019-10-15 stsp int ch, did_something = 0;
6553 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6554 2822a352 2019-10-15 stsp switch (ch) {
6556 2822a352 2019-10-15 stsp usage_integrate();
6557 2822a352 2019-10-15 stsp /* NOTREACHED */
6561 2822a352 2019-10-15 stsp argc -= optind;
6562 2822a352 2019-10-15 stsp argv += optind;
6564 2822a352 2019-10-15 stsp if (argc != 1)
6565 2822a352 2019-10-15 stsp usage_integrate();
6566 2822a352 2019-10-15 stsp branch_arg = argv[0];
6568 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6569 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6570 2822a352 2019-10-15 stsp err(1, "pledge");
6572 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6573 2822a352 2019-10-15 stsp if (cwd == NULL) {
6574 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6575 2822a352 2019-10-15 stsp goto done;
6578 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6579 2822a352 2019-10-15 stsp if (error)
6580 2822a352 2019-10-15 stsp goto done;
6582 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6583 2822a352 2019-10-15 stsp if (error)
6584 2822a352 2019-10-15 stsp goto done;
6586 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6588 2822a352 2019-10-15 stsp if (error != NULL)
6589 2822a352 2019-10-15 stsp goto done;
6591 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6592 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6593 2822a352 2019-10-15 stsp if (error)
6594 2822a352 2019-10-15 stsp goto done;
6596 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6597 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6598 2822a352 2019-10-15 stsp goto done;
6601 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6602 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6603 2822a352 2019-10-15 stsp if (error)
6604 2822a352 2019-10-15 stsp goto done;
6606 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6607 2822a352 2019-10-15 stsp if (refname == NULL) {
6608 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6609 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6610 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6611 2822a352 2019-10-15 stsp goto done;
6613 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6614 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6615 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6616 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6617 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6618 2822a352 2019-10-15 stsp goto done;
6621 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6622 2822a352 2019-10-15 stsp if (error)
6623 2822a352 2019-10-15 stsp goto done;
6625 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6626 2822a352 2019-10-15 stsp if (error)
6627 2822a352 2019-10-15 stsp goto done;
6629 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6630 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6631 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6632 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6633 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6634 2822a352 2019-10-15 stsp goto done;
6637 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6638 2822a352 2019-10-15 stsp if (error) {
6639 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6640 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6641 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6642 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6643 2822a352 2019-10-15 stsp goto done;
6646 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6647 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6648 2822a352 2019-10-15 stsp check_cancelled, NULL);
6649 2822a352 2019-10-15 stsp if (error)
6650 2822a352 2019-10-15 stsp goto done;
6652 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6655 715dc77e 2019-08-03 stsp got_repo_close(repo);
6656 2822a352 2019-10-15 stsp if (worktree)
6657 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6658 2822a352 2019-10-15 stsp free(cwd);
6659 2822a352 2019-10-15 stsp free(base_commit_id);
6660 2822a352 2019-10-15 stsp free(commit_id);
6661 2822a352 2019-10-15 stsp free(refname);
6662 2822a352 2019-10-15 stsp free(base_refname);
6663 715dc77e 2019-08-03 stsp return error;
6666 715dc77e 2019-08-03 stsp __dead static void
6667 715dc77e 2019-08-03 stsp usage_stage(void)
6669 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6670 f3055044 2019-08-07 stsp "[file-path ...]\n",
6671 715dc77e 2019-08-03 stsp getprogname());
6675 715dc77e 2019-08-03 stsp static const struct got_error *
6676 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6677 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6678 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6680 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6681 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6683 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6684 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6685 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6686 408b4ebc 2019-08-03 stsp return NULL;
6688 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6689 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6690 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6692 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6694 408b4ebc 2019-08-03 stsp return err;
6696 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6697 408b4ebc 2019-08-03 stsp free(id_str);
6698 dc424a06 2019-08-07 stsp return NULL;
6701 dc424a06 2019-08-07 stsp static const struct got_error *
6702 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6704 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6705 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6706 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6707 715dc77e 2019-08-03 stsp char *cwd = NULL;
6708 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6709 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6710 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6711 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6712 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6713 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6715 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6717 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6718 715dc77e 2019-08-03 stsp switch (ch) {
6720 408b4ebc 2019-08-03 stsp list_stage = 1;
6723 dc424a06 2019-08-07 stsp pflag = 1;
6726 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6729 715dc77e 2019-08-03 stsp usage_stage();
6730 715dc77e 2019-08-03 stsp /* NOTREACHED */
6734 715dc77e 2019-08-03 stsp argc -= optind;
6735 715dc77e 2019-08-03 stsp argv += optind;
6737 715dc77e 2019-08-03 stsp #ifndef PROFILE
6738 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6739 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6740 715dc77e 2019-08-03 stsp err(1, "pledge");
6742 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6743 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6744 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6745 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6747 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6748 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6749 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6750 715dc77e 2019-08-03 stsp goto done;
6753 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6754 715dc77e 2019-08-03 stsp if (error)
6755 715dc77e 2019-08-03 stsp goto done;
6757 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6759 715dc77e 2019-08-03 stsp if (error != NULL)
6760 715dc77e 2019-08-03 stsp goto done;
6762 dc424a06 2019-08-07 stsp if (patch_script_path) {
6763 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6764 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6765 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6766 dc424a06 2019-08-07 stsp patch_script_path);
6767 dc424a06 2019-08-07 stsp goto done;
6770 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6771 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6772 715dc77e 2019-08-03 stsp if (error)
6773 715dc77e 2019-08-03 stsp goto done;
6775 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6776 6d022e97 2019-08-04 stsp if (error)
6777 6d022e97 2019-08-04 stsp goto done;
6779 6d022e97 2019-08-04 stsp if (list_stage)
6780 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6781 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6783 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6784 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6785 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6786 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6787 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6790 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6791 2e1f37b0 2019-08-08 stsp error == NULL)
6792 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6794 ad493afc 2019-08-03 stsp got_repo_close(repo);
6795 ad493afc 2019-08-03 stsp if (worktree)
6796 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6797 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6798 ad493afc 2019-08-03 stsp free((char *)pe->path);
6799 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6800 ad493afc 2019-08-03 stsp free(cwd);
6801 ad493afc 2019-08-03 stsp return error;
6804 ad493afc 2019-08-03 stsp __dead static void
6805 ad493afc 2019-08-03 stsp usage_unstage(void)
6807 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6808 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6809 ad493afc 2019-08-03 stsp getprogname());
6814 ad493afc 2019-08-03 stsp static const struct got_error *
6815 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6817 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6818 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6819 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6820 ad493afc 2019-08-03 stsp char *cwd = NULL;
6821 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6822 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6823 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6824 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6825 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6826 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6828 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6830 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6831 ad493afc 2019-08-03 stsp switch (ch) {
6833 2e1f37b0 2019-08-08 stsp pflag = 1;
6836 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6839 ad493afc 2019-08-03 stsp usage_unstage();
6840 ad493afc 2019-08-03 stsp /* NOTREACHED */
6844 ad493afc 2019-08-03 stsp argc -= optind;
6845 ad493afc 2019-08-03 stsp argv += optind;
6847 ad493afc 2019-08-03 stsp #ifndef PROFILE
6848 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6849 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6850 ad493afc 2019-08-03 stsp err(1, "pledge");
6852 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6853 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6855 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6856 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6857 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6858 ad493afc 2019-08-03 stsp goto done;
6861 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6862 ad493afc 2019-08-03 stsp if (error)
6863 ad493afc 2019-08-03 stsp goto done;
6865 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6867 ad493afc 2019-08-03 stsp if (error != NULL)
6868 ad493afc 2019-08-03 stsp goto done;
6870 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6871 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6872 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6873 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6874 2e1f37b0 2019-08-08 stsp patch_script_path);
6875 2e1f37b0 2019-08-08 stsp goto done;
6879 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6880 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6881 ad493afc 2019-08-03 stsp if (error)
6882 ad493afc 2019-08-03 stsp goto done;
6884 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6885 ad493afc 2019-08-03 stsp if (error)
6886 ad493afc 2019-08-03 stsp goto done;
6888 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6889 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6890 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6891 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6893 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6894 2e1f37b0 2019-08-08 stsp error == NULL)
6895 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6897 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6898 715dc77e 2019-08-03 stsp if (worktree)
6899 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6900 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6901 715dc77e 2019-08-03 stsp free((char *)pe->path);
6902 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6903 715dc77e 2019-08-03 stsp free(cwd);
6904 01073a5d 2019-08-22 stsp return error;
6907 01073a5d 2019-08-22 stsp __dead static void
6908 01073a5d 2019-08-22 stsp usage_cat(void)
6910 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6911 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
6915 01073a5d 2019-08-22 stsp static const struct got_error *
6916 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6918 01073a5d 2019-08-22 stsp const struct got_error *err;
6919 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6921 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6923 01073a5d 2019-08-22 stsp return err;
6925 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6926 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6927 01073a5d 2019-08-22 stsp return err;
6930 01073a5d 2019-08-22 stsp static const struct got_error *
6931 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6933 01073a5d 2019-08-22 stsp const struct got_error *err;
6934 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6935 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
6936 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
6938 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6940 01073a5d 2019-08-22 stsp return err;
6942 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6943 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6944 01073a5d 2019-08-22 stsp while (te) {
6945 01073a5d 2019-08-22 stsp char *id_str;
6946 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6948 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6951 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6952 01073a5d 2019-08-22 stsp free(id_str);
6953 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6956 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6957 01073a5d 2019-08-22 stsp return err;
6960 01073a5d 2019-08-22 stsp static const struct got_error *
6961 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6963 01073a5d 2019-08-22 stsp const struct got_error *err;
6964 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6965 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6966 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6967 01073a5d 2019-08-22 stsp char *id_str = NULL;
6968 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6970 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6972 01073a5d 2019-08-22 stsp return err;
6974 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6976 01073a5d 2019-08-22 stsp goto done;
6978 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6979 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6980 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
6981 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6982 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6983 01073a5d 2019-08-22 stsp char *pid_str;
6984 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6986 01073a5d 2019-08-22 stsp goto done;
6987 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6988 01073a5d 2019-08-22 stsp free(pid_str);
6990 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6991 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6992 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6994 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6995 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6996 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
6998 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
6999 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7000 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7002 01073a5d 2019-08-22 stsp free(id_str);
7003 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7004 01073a5d 2019-08-22 stsp return err;
7007 01073a5d 2019-08-22 stsp static const struct got_error *
7008 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7010 01073a5d 2019-08-22 stsp const struct got_error *err;
7011 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7012 01073a5d 2019-08-22 stsp char *id_str = NULL;
7013 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7015 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7017 01073a5d 2019-08-22 stsp return err;
7019 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7021 01073a5d 2019-08-22 stsp goto done;
7023 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7025 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7026 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7027 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7028 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7030 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7031 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7032 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7034 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7035 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7036 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7038 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7039 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7040 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7046 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7047 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7049 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7050 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7051 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7053 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7054 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7055 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7057 01073a5d 2019-08-22 stsp free(id_str);
7058 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7059 01073a5d 2019-08-22 stsp return err;
7062 01073a5d 2019-08-22 stsp static const struct got_error *
7063 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7065 01073a5d 2019-08-22 stsp const struct got_error *error;
7066 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7067 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7068 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7069 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7070 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7071 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7073 01073a5d 2019-08-22 stsp #ifndef PROFILE
7074 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7075 01073a5d 2019-08-22 stsp NULL) == -1)
7076 01073a5d 2019-08-22 stsp err(1, "pledge");
7079 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7080 01073a5d 2019-08-22 stsp switch (ch) {
7082 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7085 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7086 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7087 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7089 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7092 896e9b6f 2019-08-26 stsp force_path = 1;
7095 01073a5d 2019-08-22 stsp usage_cat();
7096 01073a5d 2019-08-22 stsp /* NOTREACHED */
7100 01073a5d 2019-08-22 stsp argc -= optind;
7101 01073a5d 2019-08-22 stsp argv += optind;
7103 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7104 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7105 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7106 01073a5d 2019-08-22 stsp goto done;
7108 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7109 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7110 01073a5d 2019-08-22 stsp goto done;
7111 01073a5d 2019-08-22 stsp if (worktree) {
7112 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7113 01073a5d 2019-08-22 stsp repo_path = strdup(
7114 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7115 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7116 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7117 01073a5d 2019-08-22 stsp goto done;
7122 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7123 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7124 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7125 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7128 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7129 01073a5d 2019-08-22 stsp free(repo_path);
7130 01073a5d 2019-08-22 stsp if (error != NULL)
7131 01073a5d 2019-08-22 stsp goto done;
7133 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7134 896e9b6f 2019-08-26 stsp if (error)
7135 896e9b6f 2019-08-26 stsp goto done;
7137 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7138 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7139 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7140 01073a5d 2019-08-22 stsp if (error)
7141 01073a5d 2019-08-22 stsp goto done;
7143 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7144 896e9b6f 2019-08-26 stsp if (force_path) {
7145 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7147 896e9b6f 2019-08-26 stsp if (error)
7150 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7151 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7152 896e9b6f 2019-08-26 stsp if (error) {
7153 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7154 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7156 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7157 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7158 896e9b6f 2019-08-26 stsp if (error)
7163 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7164 01073a5d 2019-08-22 stsp if (error)
7167 01073a5d 2019-08-22 stsp switch (obj_type) {
7168 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7169 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7171 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7172 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7174 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7175 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7177 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7178 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7181 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7184 01073a5d 2019-08-22 stsp if (error)
7186 01073a5d 2019-08-22 stsp free(label);
7187 01073a5d 2019-08-22 stsp label = NULL;
7189 01073a5d 2019-08-22 stsp id = NULL;
7193 01073a5d 2019-08-22 stsp free(label);
7195 896e9b6f 2019-08-26 stsp free(commit_id);
7196 01073a5d 2019-08-22 stsp if (worktree)
7197 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7198 01073a5d 2019-08-22 stsp if (repo) {
7199 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7200 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7201 01073a5d 2019-08-22 stsp if (error == NULL)
7202 01073a5d 2019-08-22 stsp error = repo_error;
7204 0ebf8283 2019-07-24 stsp return error;