2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 53ccebc2 2019-07-30 stsp #include "got_version.h"
42 f42b1b34 2018-03-12 stsp #include "got_error.h"
43 f42b1b34 2018-03-12 stsp #include "got_object.h"
44 5261c201 2018-04-01 stsp #include "got_reference.h"
45 f42b1b34 2018-03-12 stsp #include "got_repository.h"
46 1dd54920 2019-05-11 stsp #include "got_path.h"
47 e6209546 2019-08-22 stsp #include "got_cancel.h"
48 c09a553d 2018-03-12 stsp #include "got_worktree.h"
49 79109fed 2018-03-27 stsp #include "got_diff.h"
50 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
51 404c43c4 2018-06-21 stsp #include "got_blame.h"
52 63219cd2 2019-01-04 stsp #include "got_privsep.h"
53 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
55 5c860e29 2018-03-12 stsp #ifndef nitems
56 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
60 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
63 99437157 2018-11-11 stsp catch_sigint(int signo)
65 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp catch_sigpipe(int signo)
71 99437157 2018-11-11 stsp sigpipe_received = 1;
75 8cfb4057 2019-07-09 stsp struct got_cmd {
76 5c860e29 2018-03-12 stsp const char *cmd_name;
77 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
78 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
79 97b3a7be 2019-07-09 stsp const char *cmd_alias;
82 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
83 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
84 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
86 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
90 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
91 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
92 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
93 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
94 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
95 d00136be 2019-03-26 stsp __dead static void usage_add(void);
96 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
97 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
98 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
99 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
100 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
101 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
102 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
103 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
104 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
105 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
106 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
108 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
109 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
111 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
114 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
115 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
117 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
118 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
119 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
120 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
121 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
122 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
123 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
124 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
125 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
126 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
127 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
128 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
129 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
130 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
131 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
133 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
134 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
135 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
136 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
137 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
138 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
139 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
140 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
141 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
142 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
143 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
144 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
145 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
146 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
147 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
148 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
149 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
150 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
152 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
153 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
154 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
155 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
156 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
157 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
160 ce5b7c56 2019-07-09 stsp static void
161 ce5b7c56 2019-07-09 stsp list_commands(void)
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
166 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
167 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
168 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
170 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
174 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
176 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
177 5c860e29 2018-03-12 stsp unsigned int i;
179 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
180 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
181 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
182 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
185 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
187 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 5c860e29 2018-03-12 stsp switch (ch) {
196 ce5b7c56 2019-07-09 stsp usage(hflag);
197 5c860e29 2018-03-12 stsp /* NOTREACHED */
201 5c860e29 2018-03-12 stsp argc -= optind;
202 5c860e29 2018-03-12 stsp argv += optind;
203 1e70621d 2018-03-27 stsp optind = 0;
205 53ccebc2 2019-07-30 stsp if (Vflag) {
206 53ccebc2 2019-07-30 stsp got_version_print_str();
210 5c860e29 2018-03-12 stsp if (argc <= 0)
211 ce5b7c56 2019-07-09 stsp usage(hflag);
213 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
214 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
216 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
217 d7d4f210 2018-03-12 stsp const struct got_error *error;
219 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
221 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
226 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
228 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
229 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
230 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
231 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
232 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 70015d7a 2019-11-08 stsp !(sigint_received &&
234 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 ce5b7c56 2019-07-09 stsp list_commands();
247 4ed7e80c 2018-05-20 stsp __dead static void
248 ce5b7c56 2019-07-09 stsp usage(int hflag)
250 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 53ccebc2 2019-07-30 stsp getprogname());
253 ce5b7c56 2019-07-09 stsp list_commands();
257 0266afb7 2019-01-04 stsp static const struct got_error *
258 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
260 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
261 e2ba3d07 2019-05-13 stsp const char *editor;
263 8920fa04 2019-08-18 stsp *abspath = NULL;
265 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
266 e2ba3d07 2019-05-13 stsp if (editor == NULL)
267 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
269 0ee7065d 2019-05-13 stsp if (editor) {
270 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
272 0ee7065d 2019-05-13 stsp return err;
275 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
276 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
277 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
278 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
281 e2ba3d07 2019-05-13 stsp return NULL;
284 e2ba3d07 2019-05-13 stsp static const struct got_error *
285 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
286 c530dc23 2019-07-23 stsp const char *worktree_path)
288 163ce85a 2019-05-13 stsp const struct got_error *err;
290 37c06ea4 2019-07-15 stsp #ifdef PROFILE
291 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
292 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
294 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
297 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
300 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
303 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
304 163ce85a 2019-05-13 stsp if (err != NULL)
305 163ce85a 2019-05-13 stsp return err;
307 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
310 0266afb7 2019-01-04 stsp return NULL;
313 2c7829a4 2019-06-17 stsp __dead static void
314 2c7829a4 2019-06-17 stsp usage_init(void)
316 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
320 2c7829a4 2019-06-17 stsp static const struct got_error *
321 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
323 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
324 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
327 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
328 2c7829a4 2019-06-17 stsp switch (ch) {
330 2c7829a4 2019-06-17 stsp usage_init();
331 2c7829a4 2019-06-17 stsp /* NOTREACHED */
335 2c7829a4 2019-06-17 stsp argc -= optind;
336 2c7829a4 2019-06-17 stsp argv += optind;
338 2c7829a4 2019-06-17 stsp #ifndef PROFILE
339 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 2c7829a4 2019-06-17 stsp err(1, "pledge");
342 2c7829a4 2019-06-17 stsp if (argc != 1)
343 bc20e173 2019-06-17 stsp usage_init();
345 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
346 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
347 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
349 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
351 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
352 2c7829a4 2019-06-17 stsp if (error &&
353 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
356 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
360 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
362 3ce1b845 2019-07-15 stsp free(repo_path);
363 3ce1b845 2019-07-15 stsp return error;
366 3ce1b845 2019-07-15 stsp __dead static void
367 3ce1b845 2019-07-15 stsp usage_import(void)
369 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
375 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
378 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
379 3ce1b845 2019-07-15 stsp int st = -1;
381 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
382 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
383 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
385 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
387 3ce1b845 2019-07-15 stsp goto doneediting;
389 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
390 3ce1b845 2019-07-15 stsp _exit(127);
393 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
394 3ce1b845 2019-07-15 stsp if (errno != EINTR)
397 3ce1b845 2019-07-15 stsp doneediting:
398 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
399 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
400 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
402 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
403 3ce1b845 2019-07-15 stsp errno = EINTR;
407 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
410 3ce1b845 2019-07-15 stsp static const struct got_error *
411 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 3ce1b845 2019-07-15 stsp const char *initial_content)
414 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
415 3ce1b845 2019-07-15 stsp char buf[1024];
416 3ce1b845 2019-07-15 stsp struct stat st, st2;
418 3ce1b845 2019-07-15 stsp int content_changed = 0;
419 3ce1b845 2019-07-15 stsp size_t len;
421 3ce1b845 2019-07-15 stsp *logmsg = NULL;
423 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
424 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
426 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
427 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
432 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
436 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
437 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
438 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
439 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
442 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
443 3ce1b845 2019-07-15 stsp if (fp == NULL) {
444 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
447 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
448 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
449 3ce1b845 2019-07-15 stsp content_changed = 1;
450 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
452 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
454 3ce1b845 2019-07-15 stsp fclose(fp);
456 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
461 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
462 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
466 3ce1b845 2019-07-15 stsp free(*logmsg);
467 3ce1b845 2019-07-15 stsp *logmsg = NULL;
469 3ce1b845 2019-07-15 stsp return err;
472 3ce1b845 2019-07-15 stsp static const struct got_error *
473 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
476 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
477 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
480 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
481 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
482 3ce1b845 2019-07-15 stsp branch_name) == -1)
483 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
485 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
492 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 3ce1b845 2019-07-15 stsp free(initial_content);
495 3ce1b845 2019-07-15 stsp return err;
498 3ce1b845 2019-07-15 stsp static const struct got_error *
499 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
501 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
502 3ce1b845 2019-07-15 stsp return NULL;
505 3ce1b845 2019-07-15 stsp static const struct got_error *
506 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
508 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
509 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
511 84792843 2019-08-09 stsp *author = NULL;
513 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
514 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
515 c9956ddf 2019-09-08 stsp if (name && email) {
516 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
517 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
518 aba9c984 2019-09-08 stsp return NULL;
521 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
522 84792843 2019-08-09 stsp if (got_author == NULL) {
523 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
528 c9956ddf 2019-09-08 stsp return NULL;
530 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
531 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 aba9c984 2019-09-08 stsp *author = strdup(got_author);
535 aba9c984 2019-09-08 stsp if (*author == NULL)
536 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
539 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
540 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
542 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
543 84792843 2019-08-09 stsp got_author++;
544 aba9c984 2019-09-08 stsp if (*got_author != '<') {
545 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '@') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
555 84792843 2019-08-09 stsp got_author++;
556 84792843 2019-08-09 stsp if (*got_author != '>')
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
560 aba9c984 2019-09-08 stsp free(*author);
561 aba9c984 2019-09-08 stsp *author = NULL;
563 aba9c984 2019-09-08 stsp return err;
566 c9956ddf 2019-09-08 stsp static const struct got_error *
567 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
569 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
571 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
572 c9956ddf 2019-09-08 stsp if (homedir) {
573 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
577 c9956ddf 2019-09-08 stsp return NULL;
580 84792843 2019-08-09 stsp static const struct got_error *
581 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
583 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
584 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
587 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
589 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
592 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
593 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
594 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
596 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
598 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 3ce1b845 2019-07-15 stsp switch (ch) {
601 3ce1b845 2019-07-15 stsp branch_name = optarg;
604 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
605 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
606 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
611 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
612 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
613 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
619 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
621 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
627 b2b65d18 2019-08-22 stsp usage_import();
628 3ce1b845 2019-07-15 stsp /* NOTREACHED */
632 3ce1b845 2019-07-15 stsp argc -= optind;
633 3ce1b845 2019-07-15 stsp argv += optind;
635 3ce1b845 2019-07-15 stsp #ifndef PROFILE
636 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
638 3ce1b845 2019-07-15 stsp NULL) == -1)
639 3ce1b845 2019-07-15 stsp err(1, "pledge");
641 3ce1b845 2019-07-15 stsp if (argc != 1)
642 3ce1b845 2019-07-15 stsp usage_import();
644 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
645 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
646 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
647 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
649 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
650 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
653 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
657 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
659 aba9c984 2019-09-08 stsp return error;
662 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
663 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
664 e560b7e0 2019-11-28 stsp * an unintended typo.
666 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
667 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
674 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
675 3ce1b845 2019-07-15 stsp if (error) {
676 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
679 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 3ce1b845 2019-07-15 stsp "import target branch already exists");
684 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
685 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
686 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
689 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
692 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
693 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
695 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
696 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
699 8e158b01 2019-09-22 stsp free(logmsg);
700 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 ef293bdd 2019-10-21 stsp path_dir, refname);
702 ef293bdd 2019-10-21 stsp if (error) {
703 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
704 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
705 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
710 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
711 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
712 ef293bdd 2019-10-21 stsp if (logmsg_path)
713 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
717 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
718 ef293bdd 2019-10-21 stsp if (error) {
719 ef293bdd 2019-10-21 stsp if (logmsg_path)
720 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
726 ef293bdd 2019-10-21 stsp if (error) {
727 ef293bdd 2019-10-21 stsp if (logmsg_path)
728 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
732 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
733 ef293bdd 2019-10-21 stsp if (error) {
734 ef293bdd 2019-10-21 stsp if (logmsg_path)
735 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
740 ef293bdd 2019-10-21 stsp if (error) {
741 ef293bdd 2019-10-21 stsp if (logmsg_path)
742 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
747 ef293bdd 2019-10-21 stsp if (error) {
748 ef293bdd 2019-10-21 stsp if (logmsg_path)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
754 3ce1b845 2019-07-15 stsp if (error) {
755 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
756 ef293bdd 2019-10-21 stsp if (logmsg_path)
757 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
761 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
762 3ce1b845 2019-07-15 stsp branch_ref);
763 ef293bdd 2019-10-21 stsp if (error) {
764 ef293bdd 2019-10-21 stsp if (logmsg_path)
765 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
769 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
777 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
778 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
780 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
781 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
782 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
783 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
785 8e158b01 2019-09-22 stsp free(logmsg);
786 ef293bdd 2019-10-21 stsp free(logmsg_path);
787 2c7829a4 2019-06-17 stsp free(repo_path);
788 3ce1b845 2019-07-15 stsp free(editor);
789 3ce1b845 2019-07-15 stsp free(refname);
790 3ce1b845 2019-07-15 stsp free(new_commit_id);
791 3ce1b845 2019-07-15 stsp free(id_str);
792 aba9c984 2019-09-08 stsp free(author);
793 c9956ddf 2019-09-08 stsp free(gitconfig_path);
794 3ce1b845 2019-07-15 stsp if (branch_ref)
795 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
796 3ce1b845 2019-07-15 stsp if (head_ref)
797 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
798 2c7829a4 2019-06-17 stsp return error;
801 4ed7e80c 2018-05-20 stsp __dead static void
802 c09a553d 2018-03-12 stsp usage_checkout(void)
804 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
809 7f47418f 2019-12-20 stsp static void
810 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
812 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
813 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
814 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
815 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
816 7f47418f 2019-12-20 stsp getprogname());
819 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
820 7f47418f 2019-12-20 stsp const char *worktree_path;
821 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
824 1ee397ad 2019-07-12 stsp static const struct got_error *
825 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
827 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
829 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
830 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
831 7f47418f 2019-12-20 stsp return NULL;
833 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
834 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
835 1ee397ad 2019-07-12 stsp return NULL;
838 92a684f4 2018-03-12 stsp while (path[0] == '/')
841 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
842 1ee397ad 2019-07-12 stsp return NULL;
845 99437157 2018-11-11 stsp static const struct got_error *
846 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
848 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
849 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
850 99437157 2018-11-11 stsp return NULL;
853 8069f636 2019-01-12 stsp static const struct got_error *
854 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
855 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 3aef623b 2019-10-15 stsp struct got_repository *repo)
858 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
859 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
861 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
864 36a38700 2019-05-10 stsp return err;
866 d5bea539 2019-05-13 stsp if (yca_id == NULL)
867 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
870 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
871 d5bea539 2019-05-13 stsp * and the work tree's base commit.
873 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
875 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
883 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
884 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
885 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
887 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
888 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
890 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
892 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
894 d5bea539 2019-05-13 stsp free(yca_id);
895 d5bea539 2019-05-13 stsp return NULL;
898 a367ff0f 2019-05-14 stsp static const struct got_error *
899 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
900 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
901 a51a74b3 2019-07-27 stsp struct got_repository *repo)
903 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
904 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
905 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
906 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
908 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
913 a51a74b3 2019-07-27 stsp is_same_branch = 1;
916 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
917 a51a74b3 2019-07-27 stsp is_same_branch = 1;
921 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
925 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
931 a367ff0f 2019-05-14 stsp struct got_object_id *id;
932 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
933 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
935 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
936 a367ff0f 2019-05-14 stsp err = NULL;
941 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
944 a367ff0f 2019-05-14 stsp is_same_branch = 1;
951 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
952 a367ff0f 2019-05-14 stsp free(head_commit_id);
953 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
954 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
955 a367ff0f 2019-05-14 stsp return err;
958 4ed7e80c 2018-05-20 stsp static const struct got_error *
959 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
961 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
962 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
963 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
964 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
965 c09a553d 2018-03-12 stsp char *repo_path = NULL;
966 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
967 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
968 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
969 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
970 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
971 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
972 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
974 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
976 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -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 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
990 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
993 2deda0b9 2019-03-07 stsp usage_checkout();
994 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
998 0bb8a95e 2018-03-12 stsp argc -= optind;
999 0bb8a95e 2018-03-12 stsp argv += optind;
1001 6715a751 2018-03-16 stsp #ifndef PROFILE
1002 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1003 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1004 c09a553d 2018-03-12 stsp err(1, "pledge");
1006 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1007 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1008 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1009 76089277 2018-04-01 stsp if (repo_path == NULL)
1010 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1011 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1012 76089277 2018-04-01 stsp if (cwd == NULL) {
1013 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1014 76089277 2018-04-01 stsp goto done;
1016 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1017 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1018 230a42bd 2019-05-11 jcs if (base == NULL) {
1019 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1020 230a42bd 2019-05-11 jcs path_prefix);
1024 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1025 230a42bd 2019-05-11 jcs if (base == NULL) {
1026 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1027 230a42bd 2019-05-11 jcs repo_path);
1031 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1032 c09a553d 2018-03-12 stsp if (dotgit)
1033 c09a553d 2018-03-12 stsp *dotgit = '\0';
1034 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1035 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1036 c09a553d 2018-03-12 stsp free(cwd);
1037 76089277 2018-04-01 stsp goto done;
1039 c09a553d 2018-03-12 stsp free(cwd);
1040 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1041 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1042 76089277 2018-04-01 stsp if (repo_path == NULL) {
1043 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1044 76089277 2018-04-01 stsp goto done;
1046 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1047 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1048 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1049 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1051 b4b3a7dd 2019-07-22 stsp goto done;
1053 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1054 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1055 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1056 b4b3a7dd 2019-07-22 stsp goto done;
1060 c09a553d 2018-03-12 stsp usage_checkout();
1062 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1063 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1065 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1066 c09a553d 2018-03-12 stsp if (error != NULL)
1067 c02c541e 2019-03-29 stsp goto done;
1069 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1070 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1071 c530dc23 2019-07-23 stsp if (error) {
1072 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1073 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1074 c530dc23 2019-07-23 stsp goto done;
1075 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1076 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1077 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1078 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1079 c530dc23 2019-07-23 stsp goto done;
1083 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1084 c02c541e 2019-03-29 stsp if (error)
1085 c09a553d 2018-03-12 stsp goto done;
1087 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1088 c09a553d 2018-03-12 stsp if (error != NULL)
1089 c09a553d 2018-03-12 stsp goto done;
1091 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1092 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1093 c09a553d 2018-03-12 stsp goto done;
1095 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1096 c09a553d 2018-03-12 stsp if (error != NULL)
1097 c09a553d 2018-03-12 stsp goto done;
1099 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1100 e5dc7198 2018-12-29 stsp path_prefix);
1101 e5dc7198 2018-12-29 stsp if (error != NULL)
1102 e5dc7198 2018-12-29 stsp goto done;
1103 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1104 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1105 49520a32 2018-12-29 stsp goto done;
1108 8069f636 2019-01-12 stsp if (commit_id_str) {
1109 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1110 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1111 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1112 30837e32 2019-07-25 stsp if (error)
1113 8069f636 2019-01-12 stsp goto done;
1114 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1115 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1116 8069f636 2019-01-12 stsp if (error != NULL) {
1117 8069f636 2019-01-12 stsp free(commit_id);
1118 8069f636 2019-01-12 stsp goto done;
1120 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1121 45d344f6 2019-05-14 stsp if (error)
1122 45d344f6 2019-05-14 stsp goto done;
1123 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1124 8069f636 2019-01-12 stsp commit_id);
1125 8069f636 2019-01-12 stsp free(commit_id);
1126 8069f636 2019-01-12 stsp if (error)
1127 8069f636 2019-01-12 stsp goto done;
1130 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1131 f2ea84fa 2019-07-27 stsp if (error)
1132 f2ea84fa 2019-07-27 stsp goto done;
1133 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1134 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1135 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1136 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1137 c09a553d 2018-03-12 stsp if (error != NULL)
1138 c09a553d 2018-03-12 stsp goto done;
1140 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1141 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1142 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1144 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1145 8069f636 2019-01-12 stsp free(commit_id_str);
1146 76089277 2018-04-01 stsp free(repo_path);
1147 507dc3bb 2018-12-29 stsp free(worktree_path);
1148 507dc3bb 2018-12-29 stsp return error;
1151 507dc3bb 2018-12-29 stsp __dead static void
1152 507dc3bb 2018-12-29 stsp usage_update(void)
1154 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1155 507dc3bb 2018-12-29 stsp getprogname());
1159 1ee397ad 2019-07-12 stsp static const struct got_error *
1160 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1162 784955db 2019-01-12 stsp int *did_something = arg;
1164 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1165 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1166 1ee397ad 2019-07-12 stsp return NULL;
1168 1545c615 2019-02-10 stsp *did_something = 1;
1170 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1171 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1172 1ee397ad 2019-07-12 stsp return NULL;
1174 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1176 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1177 1ee397ad 2019-07-12 stsp return NULL;
1180 be7061eb 2018-12-30 stsp static const struct got_error *
1181 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1182 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1183 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1185 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1186 a1fb16d8 2019-05-24 stsp char *base_id_str;
1187 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1189 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1190 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1191 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1192 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1193 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1194 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1195 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1198 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1199 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1200 a1fb16d8 2019-05-24 stsp if (err) {
1201 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1202 a1fb16d8 2019-05-24 stsp return err;
1203 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1205 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1206 a1fb16d8 2019-05-24 stsp return NULL;
1208 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1209 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1210 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1212 a1fb16d8 2019-05-24 stsp return err;
1213 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1214 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1215 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1216 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1217 0ebf8283 2019-07-24 stsp return NULL;
1220 0ebf8283 2019-07-24 stsp static const struct got_error *
1221 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1223 0ebf8283 2019-07-24 stsp const struct got_error *err;
1224 0ebf8283 2019-07-24 stsp int in_progress;
1226 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1228 0ebf8283 2019-07-24 stsp return err;
1229 0ebf8283 2019-07-24 stsp if (in_progress)
1230 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1232 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1234 0ebf8283 2019-07-24 stsp return err;
1235 0ebf8283 2019-07-24 stsp if (in_progress)
1236 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1238 a1fb16d8 2019-05-24 stsp return NULL;
1241 a5edda0a 2019-07-27 stsp static const struct got_error *
1242 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1243 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1245 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1246 a5edda0a 2019-07-27 stsp char *path;
1249 a5edda0a 2019-07-27 stsp if (argc == 0) {
1250 a5edda0a 2019-07-27 stsp path = strdup("");
1251 a5edda0a 2019-07-27 stsp if (path == NULL)
1252 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1253 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1256 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1257 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1260 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1261 a5edda0a 2019-07-27 stsp if (err) {
1262 a5edda0a 2019-07-27 stsp free(path);
1267 a5edda0a 2019-07-27 stsp return err;
1270 a1fb16d8 2019-05-24 stsp static const struct got_error *
1271 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1273 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1274 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1275 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1276 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1277 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1278 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1279 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1280 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1281 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1282 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1283 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1285 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1287 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1288 507dc3bb 2018-12-29 stsp switch (ch) {
1290 024e9686 2019-05-14 stsp branch_name = optarg;
1293 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1294 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1295 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1298 2deda0b9 2019-03-07 stsp usage_update();
1299 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1303 507dc3bb 2018-12-29 stsp argc -= optind;
1304 507dc3bb 2018-12-29 stsp argv += optind;
1306 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1307 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1308 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1309 507dc3bb 2018-12-29 stsp err(1, "pledge");
1311 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1312 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1313 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1314 c4cdcb68 2019-04-03 stsp goto done;
1316 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1317 7d5807f4 2019-07-11 stsp if (error)
1318 7d5807f4 2019-07-11 stsp goto done;
1320 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1321 f2ea84fa 2019-07-27 stsp if (error)
1322 f2ea84fa 2019-07-27 stsp goto done;
1324 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1326 507dc3bb 2018-12-29 stsp if (error != NULL)
1327 507dc3bb 2018-12-29 stsp goto done;
1329 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1330 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1331 087fb88c 2019-08-04 stsp if (error)
1332 087fb88c 2019-08-04 stsp goto done;
1334 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1335 0266afb7 2019-01-04 stsp if (error)
1336 0266afb7 2019-01-04 stsp goto done;
1338 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1339 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1340 024e9686 2019-05-14 stsp if (error != NULL)
1341 024e9686 2019-05-14 stsp goto done;
1342 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1343 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1344 9c4b8182 2019-01-02 stsp if (error != NULL)
1345 9c4b8182 2019-01-02 stsp goto done;
1346 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1347 507dc3bb 2018-12-29 stsp if (error != NULL)
1348 507dc3bb 2018-12-29 stsp goto done;
1350 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1351 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1352 04f57cb3 2019-07-25 stsp free(commit_id_str);
1353 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1354 30837e32 2019-07-25 stsp if (error)
1355 a297e751 2019-07-11 stsp goto done;
1356 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1357 a297e751 2019-07-11 stsp if (error)
1358 507dc3bb 2018-12-29 stsp goto done;
1361 a1fb16d8 2019-05-24 stsp if (branch_name) {
1362 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1363 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1364 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1366 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1367 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1368 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1369 024e9686 2019-05-14 stsp goto done;
1371 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1372 024e9686 2019-05-14 stsp if (error)
1373 024e9686 2019-05-14 stsp goto done;
1374 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1376 a367ff0f 2019-05-14 stsp free(head_commit_id);
1377 024e9686 2019-05-14 stsp if (error != NULL)
1378 024e9686 2019-05-14 stsp goto done;
1379 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1380 a367ff0f 2019-05-14 stsp if (error)
1381 a367ff0f 2019-05-14 stsp goto done;
1382 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1383 024e9686 2019-05-14 stsp if (error)
1384 024e9686 2019-05-14 stsp goto done;
1386 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1387 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1388 a367ff0f 2019-05-14 stsp if (error != NULL) {
1389 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1390 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1391 024e9686 2019-05-14 stsp goto done;
1393 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1394 a367ff0f 2019-05-14 stsp if (error)
1395 a367ff0f 2019-05-14 stsp goto done;
1398 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1399 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1400 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1401 507dc3bb 2018-12-29 stsp commit_id);
1402 507dc3bb 2018-12-29 stsp if (error)
1403 507dc3bb 2018-12-29 stsp goto done;
1406 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1407 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1408 507dc3bb 2018-12-29 stsp if (error != NULL)
1409 507dc3bb 2018-12-29 stsp goto done;
1411 784955db 2019-01-12 stsp if (did_something)
1412 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1414 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1416 c09a553d 2018-03-12 stsp free(worktree_path);
1417 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1418 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1419 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1420 507dc3bb 2018-12-29 stsp free(commit_id);
1421 9c4b8182 2019-01-02 stsp free(commit_id_str);
1422 c09a553d 2018-03-12 stsp return error;
1425 f42b1b34 2018-03-12 stsp static const struct got_error *
1426 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1427 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1428 63035f9f 2019-10-06 stsp struct got_repository *repo)
1430 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1431 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1433 44392932 2019-08-25 stsp if (blob_id1) {
1434 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1436 44392932 2019-08-25 stsp goto done;
1439 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1441 44392932 2019-08-25 stsp goto done;
1443 44392932 2019-08-25 stsp while (path[0] == '/')
1445 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1446 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1448 44392932 2019-08-25 stsp if (blob1)
1449 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1450 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1451 44392932 2019-08-25 stsp return err;
1454 44392932 2019-08-25 stsp static const struct got_error *
1455 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1456 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1457 63035f9f 2019-10-06 stsp struct got_repository *repo)
1459 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1460 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1461 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1463 44392932 2019-08-25 stsp if (tree_id1) {
1464 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1466 44392932 2019-08-25 stsp goto done;
1469 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1471 0f2b3dca 2018-12-22 stsp goto done;
1473 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1474 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1475 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1476 44392932 2019-08-25 stsp while (path[0] == '/')
1478 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1479 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1481 79109fed 2018-03-27 stsp if (tree1)
1482 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1483 366e0a5f 2019-10-10 stsp if (tree2)
1484 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1485 44392932 2019-08-25 stsp return err;
1488 44392932 2019-08-25 stsp static const struct got_error *
1489 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1490 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1492 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1493 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1494 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1495 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1496 44392932 2019-08-25 stsp struct got_object_qid *qid;
1498 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1499 44392932 2019-08-25 stsp if (qid != NULL) {
1500 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1503 44392932 2019-08-25 stsp return err;
1506 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1507 44392932 2019-08-25 stsp int obj_type;
1508 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1510 44392932 2019-08-25 stsp goto done;
1511 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1512 44392932 2019-08-25 stsp if (err) {
1513 44392932 2019-08-25 stsp free(obj_id2);
1514 44392932 2019-08-25 stsp goto done;
1516 44392932 2019-08-25 stsp if (pcommit) {
1517 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1518 44392932 2019-08-25 stsp qid->id, path);
1519 44392932 2019-08-25 stsp if (err) {
1520 44392932 2019-08-25 stsp free(obj_id2);
1521 44392932 2019-08-25 stsp goto done;
1523 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1524 44392932 2019-08-25 stsp if (err) {
1525 44392932 2019-08-25 stsp free(obj_id2);
1526 44392932 2019-08-25 stsp goto done;
1529 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1530 44392932 2019-08-25 stsp if (err) {
1531 44392932 2019-08-25 stsp free(obj_id2);
1532 44392932 2019-08-25 stsp goto done;
1534 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1535 44392932 2019-08-25 stsp switch (obj_type) {
1536 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1537 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1540 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1541 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1545 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1548 44392932 2019-08-25 stsp free(obj_id1);
1549 44392932 2019-08-25 stsp free(obj_id2);
1551 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1552 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1554 44392932 2019-08-25 stsp goto done;
1555 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1556 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1558 44392932 2019-08-25 stsp goto done;
1559 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1560 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1563 0f2b3dca 2018-12-22 stsp free(id_str1);
1564 0f2b3dca 2018-12-22 stsp free(id_str2);
1565 44392932 2019-08-25 stsp if (pcommit)
1566 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1567 79109fed 2018-03-27 stsp return err;
1570 4bb494d5 2018-06-16 stsp static char *
1571 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1573 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1574 09867e48 2019-08-13 stsp char *p, *s;
1576 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1577 09867e48 2019-08-13 stsp if (tm == NULL)
1578 09867e48 2019-08-13 stsp return NULL;
1579 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1580 09867e48 2019-08-13 stsp if (s == NULL)
1581 09867e48 2019-08-13 stsp return NULL;
1582 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1584 6c281f94 2018-06-11 stsp *p = '\0';
1588 6841bf13 2019-11-29 kn static const struct got_error *
1589 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1590 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1592 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1593 6841bf13 2019-11-29 kn regmatch_t regmatch;
1594 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1596 6841bf13 2019-11-29 kn *have_match = 0;
1598 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1602 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1606 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1607 6841bf13 2019-11-29 kn *have_match = 1;
1609 6841bf13 2019-11-29 kn free(id_str);
1610 6841bf13 2019-11-29 kn free(logmsg);
1614 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1616 79109fed 2018-03-27 stsp static const struct got_error *
1617 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1618 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1619 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1621 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1622 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1623 6c281f94 2018-06-11 stsp char datebuf[26];
1624 45d799e2 2018-12-23 stsp time_t committer_time;
1625 45d799e2 2018-12-23 stsp const char *author, *committer;
1626 199a4027 2019-02-02 stsp char *refs_str = NULL;
1627 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1629 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1631 199a4027 2019-02-02 stsp const char *name;
1632 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1635 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1636 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1638 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1639 199a4027 2019-02-02 stsp name += 5;
1640 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1642 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1643 e34f9ed6 2019-02-02 stsp name += 6;
1644 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1645 141c2bff 2019-02-04 stsp name += 8;
1646 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1647 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1648 5d844a1e 2019-08-13 stsp if (err) {
1649 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1650 5d844a1e 2019-08-13 stsp return err;
1651 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1652 5d844a1e 2019-08-13 stsp err = NULL;
1653 5d844a1e 2019-08-13 stsp tag = NULL;
1656 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1657 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1659 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1660 a436ad14 2019-08-13 stsp if (cmp != 0)
1662 199a4027 2019-02-02 stsp s = refs_str;
1663 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1664 199a4027 2019-02-02 stsp name) == -1) {
1665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1667 34ca4898 2019-08-13 stsp return err;
1671 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1673 8bf5b3c9 2018-03-17 stsp return err;
1675 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1676 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1677 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1678 832c249c 2018-06-10 stsp free(id_str);
1679 d3d493d7 2019-02-21 stsp id_str = NULL;
1680 d3d493d7 2019-02-21 stsp free(refs_str);
1681 d3d493d7 2019-02-21 stsp refs_str = NULL;
1682 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1683 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1684 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1685 09867e48 2019-08-13 stsp if (datestr)
1686 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1687 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1688 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1689 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1690 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1691 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1692 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1693 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1694 3fe1abad 2018-06-10 stsp int n = 1;
1695 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1696 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1697 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1699 a0603db2 2018-06-10 stsp return err;
1700 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1701 a0603db2 2018-06-10 stsp free(id_str);
1705 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1707 5943eee2 2019-08-13 stsp return err;
1709 621015ac 2018-07-23 stsp logmsg = logmsg0;
1711 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1713 832c249c 2018-06-10 stsp printf(" %s\n", line);
1714 832c249c 2018-06-10 stsp } while (line);
1715 621015ac 2018-07-23 stsp free(logmsg0);
1717 971751ac 2018-03-27 stsp if (show_patch) {
1718 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1719 971751ac 2018-03-27 stsp if (err == 0)
1720 971751ac 2018-03-27 stsp printf("\n");
1723 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1724 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1725 07862c20 2018-09-15 stsp return err;
1728 f42b1b34 2018-03-12 stsp static const struct got_error *
1729 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1730 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1731 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1732 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1734 f42b1b34 2018-03-12 stsp const struct got_error *err;
1735 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1736 6841bf13 2019-11-29 kn regex_t regex;
1737 6841bf13 2019-11-29 kn int have_match;
1739 6841bf13 2019-11-29 kn if (search_pattern &&
1740 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1741 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1743 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1745 f42b1b34 2018-03-12 stsp return err;
1746 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1747 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1749 fcc85cad 2018-07-22 stsp goto done;
1751 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1752 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1754 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1757 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1758 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1759 372ccdbb 2018-06-10 stsp if (err) {
1760 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1761 9ba79e04 2018-06-11 stsp err = NULL;
1764 b43fbaa0 2018-06-11 stsp if (id == NULL)
1767 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1771 6841bf13 2019-11-29 kn if (search_pattern) {
1772 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
1774 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1777 6841bf13 2019-11-29 kn if (have_match == 0) {
1778 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1783 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1784 44392932 2019-08-25 stsp diff_context, refs);
1785 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1786 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1790 6841bf13 2019-11-29 kn if (search_pattern)
1791 6841bf13 2019-11-29 kn regfree(®ex);
1792 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1793 f42b1b34 2018-03-12 stsp return err;
1796 4ed7e80c 2018-05-20 stsp __dead static void
1797 6f3d1eb0 2018-03-12 stsp usage_log(void)
1799 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1800 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1804 b1ebc001 2019-08-13 stsp static int
1805 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1807 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1808 b1ebc001 2019-08-13 stsp long long n;
1809 b1ebc001 2019-08-13 stsp const char *errstr;
1811 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1812 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1814 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1815 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1820 4ed7e80c 2018-05-20 stsp static const struct got_error *
1821 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1823 f42b1b34 2018-03-12 stsp const struct got_error *error;
1824 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1825 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1826 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1827 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1828 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1829 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1830 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1831 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
1832 64a96a6d 2018-04-01 stsp const char *errstr;
1833 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1835 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1837 6715a751 2018-03-16 stsp #ifndef PROFILE
1838 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1841 f42b1b34 2018-03-12 stsp err(1, "pledge");
1844 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1846 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1847 79109fed 2018-03-27 stsp switch (ch) {
1849 79109fed 2018-03-27 stsp show_patch = 1;
1852 d142fc45 2018-04-01 stsp start_commit = optarg;
1855 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1857 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1858 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1861 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1862 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1863 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1866 48c8c60d 2020-01-27 stsp log_branches = 1;
1869 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1870 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1871 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1873 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1876 6841bf13 2019-11-29 kn search_pattern = optarg;
1879 2deda0b9 2019-03-07 stsp usage_log();
1880 79109fed 2018-03-27 stsp /* NOTREACHED */
1884 79109fed 2018-03-27 stsp argc -= optind;
1885 79109fed 2018-03-27 stsp argv += optind;
1887 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1888 dc1edbfa 2019-11-29 kn diff_context = 3;
1889 dc1edbfa 2019-11-29 kn else if (!show_patch)
1890 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1892 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1893 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1894 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1895 04ca23f4 2018-07-16 stsp goto done;
1898 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1899 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1900 cffc0aa4 2019-02-05 stsp goto done;
1901 cffc0aa4 2019-02-05 stsp error = NULL;
1903 e7301579 2019-03-18 stsp if (argc == 0) {
1904 cbd1af7a 2019-03-18 stsp path = strdup("");
1905 e7301579 2019-03-18 stsp if (path == NULL) {
1906 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1907 cbd1af7a 2019-03-18 stsp goto done;
1909 e7301579 2019-03-18 stsp } else if (argc == 1) {
1910 e7301579 2019-03-18 stsp if (worktree) {
1911 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1913 e7301579 2019-03-18 stsp if (error)
1914 e7301579 2019-03-18 stsp goto done;
1916 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1917 e7301579 2019-03-18 stsp if (path == NULL) {
1918 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1919 e7301579 2019-03-18 stsp goto done;
1923 cbd1af7a 2019-03-18 stsp usage_log();
1925 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1926 5486daa2 2019-05-11 stsp repo_path = worktree ?
1927 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1929 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1930 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1931 cffc0aa4 2019-02-05 stsp goto done;
1934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1935 d7d4f210 2018-03-12 stsp if (error != NULL)
1936 04ca23f4 2018-07-16 stsp goto done;
1938 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1939 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1940 c02c541e 2019-03-29 stsp if (error)
1941 c02c541e 2019-03-29 stsp goto done;
1943 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1944 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1945 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1946 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1947 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1948 3235492e 2018-04-01 stsp if (error != NULL)
1949 3235492e 2018-04-01 stsp return error;
1950 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1951 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1952 3235492e 2018-04-01 stsp if (error != NULL)
1953 3235492e 2018-04-01 stsp return error;
1954 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1956 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1957 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1958 3235492e 2018-04-01 stsp if (error == NULL) {
1959 1caad61b 2019-02-01 stsp int obj_type;
1960 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1961 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1962 d1f2edc9 2018-06-13 stsp if (error != NULL)
1963 1caad61b 2019-02-01 stsp goto done;
1964 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1965 1caad61b 2019-02-01 stsp if (error != NULL)
1966 1caad61b 2019-02-01 stsp goto done;
1967 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1968 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1969 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1970 1caad61b 2019-02-01 stsp if (error != NULL)
1971 1caad61b 2019-02-01 stsp goto done;
1972 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1973 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1974 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1975 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1976 1caad61b 2019-02-01 stsp goto done;
1979 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1980 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1981 1caad61b 2019-02-01 stsp if (id == NULL)
1982 638f9024 2019-05-13 stsp error = got_error_from_errno(
1983 230a42bd 2019-05-11 jcs "got_object_id_dup");
1984 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1985 1caad61b 2019-02-01 stsp if (error)
1986 1caad61b 2019-02-01 stsp goto done;
1987 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1989 1caad61b 2019-02-01 stsp goto done;
1991 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1992 d1f2edc9 2018-06-13 stsp if (error != NULL)
1993 1caad61b 2019-02-01 stsp goto done;
1995 15a94983 2018-12-23 stsp if (commit == NULL) {
1996 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1997 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1998 d1f2edc9 2018-06-13 stsp if (error != NULL)
1999 d1f2edc9 2018-06-13 stsp return error;
2002 d7d4f210 2018-03-12 stsp if (error != NULL)
2003 04ca23f4 2018-07-16 stsp goto done;
2005 deeabeae 2019-08-27 stsp if (worktree) {
2006 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2008 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2009 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2010 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2011 deeabeae 2019-08-27 stsp goto done;
2013 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2016 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2017 04ca23f4 2018-07-16 stsp if (error != NULL)
2018 04ca23f4 2018-07-16 stsp goto done;
2019 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2020 04ca23f4 2018-07-16 stsp free(path);
2021 04ca23f4 2018-07-16 stsp path = in_repo_path;
2024 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2025 199a4027 2019-02-02 stsp if (error)
2026 199a4027 2019-02-02 stsp goto done;
2028 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2029 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2031 04ca23f4 2018-07-16 stsp free(path);
2032 04ca23f4 2018-07-16 stsp free(repo_path);
2033 04ca23f4 2018-07-16 stsp free(cwd);
2035 cffc0aa4 2019-02-05 stsp if (worktree)
2036 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2037 ad242220 2018-09-08 stsp if (repo) {
2038 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2039 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2040 ad242220 2018-09-08 stsp if (error == NULL)
2041 ad242220 2018-09-08 stsp error = repo_error;
2043 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2044 8bf5b3c9 2018-03-17 stsp return error;
2047 4ed7e80c 2018-05-20 stsp __dead static void
2048 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2050 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2051 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2055 b72f483a 2019-02-05 stsp struct print_diff_arg {
2056 b72f483a 2019-02-05 stsp struct got_repository *repo;
2057 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2058 b72f483a 2019-02-05 stsp int diff_context;
2059 3fc0c068 2019-02-10 stsp const char *id_str;
2060 3fc0c068 2019-02-10 stsp int header_shown;
2061 98eaaa12 2019-08-03 stsp int diff_staged;
2062 63035f9f 2019-10-06 stsp int ignore_whitespace;
2065 4ed7e80c 2018-05-20 stsp static const struct got_error *
2066 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2067 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2068 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2069 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2071 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2072 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2073 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2074 12463d8b 2019-12-13 stsp int fd = -1;
2075 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2076 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2077 b72f483a 2019-02-05 stsp struct stat sb;
2079 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2080 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2081 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2082 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2083 98eaaa12 2019-08-03 stsp return NULL;
2085 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2086 98eaaa12 2019-08-03 stsp return NULL;
2087 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2088 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2089 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2090 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2091 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2092 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2093 98eaaa12 2019-08-03 stsp return NULL;
2096 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2097 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2098 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2099 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2100 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2103 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2104 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2105 98eaaa12 2019-08-03 stsp switch (staged_status) {
2106 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2107 98eaaa12 2019-08-03 stsp label1 = path;
2108 98eaaa12 2019-08-03 stsp label2 = path;
2110 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2111 98eaaa12 2019-08-03 stsp label2 = path;
2113 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2114 98eaaa12 2019-08-03 stsp label1 = path;
2117 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2119 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2120 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2121 63035f9f 2019-10-06 stsp a->repo, stdout);
2124 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2125 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2126 4ce46740 2019-08-08 stsp char *id_str;
2127 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2130 4ce46740 2019-08-08 stsp goto done;
2131 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2133 4ce46740 2019-08-08 stsp goto done;
2134 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2135 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2136 4ce46740 2019-08-08 stsp free(id_str);
2137 4ce46740 2019-08-08 stsp goto done;
2139 4ce46740 2019-08-08 stsp free(id_str);
2140 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2141 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2143 4ce46740 2019-08-08 stsp goto done;
2146 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2147 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2148 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2149 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2150 2ec1f75b 2019-03-26 stsp goto done;
2153 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2154 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2155 12463d8b 2019-12-13 stsp if (fd == -1) {
2156 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2157 12463d8b 2019-12-13 stsp goto done;
2160 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2161 12463d8b 2019-12-13 stsp if (fd == -1) {
2162 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2163 12463d8b 2019-12-13 stsp goto done;
2166 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2167 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2168 2ec1f75b 2019-03-26 stsp goto done;
2170 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2171 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2172 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2173 2ec1f75b 2019-03-26 stsp goto done;
2177 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2179 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2180 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2182 b72f483a 2019-02-05 stsp if (blob1)
2183 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2184 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2185 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2186 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2187 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2188 b72f483a 2019-02-05 stsp free(abspath);
2189 927df6b7 2019-02-10 stsp return err;
2192 927df6b7 2019-02-10 stsp static const struct got_error *
2193 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2195 b00d56cd 2018-04-01 stsp const struct got_error *error;
2196 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2197 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2198 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2199 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2200 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2201 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2202 15a94983 2018-12-23 stsp int type1, type2;
2203 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2204 c0cc5c62 2018-10-18 stsp const char *errstr;
2205 927df6b7 2019-02-10 stsp char *path = NULL;
2207 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2208 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2209 25eccc22 2019-01-04 stsp NULL) == -1)
2210 b00d56cd 2018-04-01 stsp err(1, "pledge");
2213 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2214 b00d56cd 2018-04-01 stsp switch (ch) {
2216 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2218 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2219 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2222 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2223 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2224 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2226 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2229 98eaaa12 2019-08-03 stsp diff_staged = 1;
2232 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2235 2deda0b9 2019-03-07 stsp usage_diff();
2236 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2240 b00d56cd 2018-04-01 stsp argc -= optind;
2241 b00d56cd 2018-04-01 stsp argv += optind;
2243 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2244 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2245 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2246 b72f483a 2019-02-05 stsp goto done;
2248 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2249 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2250 927df6b7 2019-02-10 stsp goto done;
2251 927df6b7 2019-02-10 stsp if (argc <= 1) {
2252 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2253 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2254 927df6b7 2019-02-10 stsp goto done;
2256 b72f483a 2019-02-05 stsp if (repo_path)
2258 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2259 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2260 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2261 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2262 927df6b7 2019-02-10 stsp goto done;
2264 927df6b7 2019-02-10 stsp if (argc == 1) {
2265 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2267 927df6b7 2019-02-10 stsp if (error)
2268 927df6b7 2019-02-10 stsp goto done;
2270 927df6b7 2019-02-10 stsp path = strdup("");
2271 927df6b7 2019-02-10 stsp if (path == NULL) {
2272 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2273 927df6b7 2019-02-10 stsp goto done;
2276 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2277 98eaaa12 2019-08-03 stsp if (diff_staged)
2278 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2279 98eaaa12 2019-08-03 stsp "objects in repository");
2280 15a94983 2018-12-23 stsp id_str1 = argv[0];
2281 15a94983 2018-12-23 stsp id_str2 = argv[1];
2282 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2283 30db809c 2019-06-05 stsp repo_path =
2284 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2285 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2286 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2287 30db809c 2019-06-05 stsp goto done;
2291 b00d56cd 2018-04-01 stsp usage_diff();
2293 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2294 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2295 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2296 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2299 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2300 76089277 2018-04-01 stsp free(repo_path);
2301 b00d56cd 2018-04-01 stsp if (error != NULL)
2302 b00d56cd 2018-04-01 stsp goto done;
2304 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2305 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2306 c02c541e 2019-03-29 stsp if (error)
2307 c02c541e 2019-03-29 stsp goto done;
2309 30db809c 2019-06-05 stsp if (argc <= 1) {
2310 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2311 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2312 b72f483a 2019-02-05 stsp char *id_str;
2314 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2316 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2317 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2318 b72f483a 2019-02-05 stsp if (error)
2319 b72f483a 2019-02-05 stsp goto done;
2320 b72f483a 2019-02-05 stsp arg.repo = repo;
2321 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2322 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2323 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2324 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2325 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2326 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2328 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2329 72ea6654 2019-07-27 stsp if (error)
2330 72ea6654 2019-07-27 stsp goto done;
2332 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2333 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2334 b72f483a 2019-02-05 stsp free(id_str);
2335 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2336 b72f483a 2019-02-05 stsp goto done;
2339 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2340 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2341 0adb7196 2019-08-11 stsp if (error)
2342 0adb7196 2019-08-11 stsp goto done;
2344 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2345 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2346 0adb7196 2019-08-11 stsp if (error)
2347 0adb7196 2019-08-11 stsp goto done;
2349 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2350 15a94983 2018-12-23 stsp if (error)
2351 15a94983 2018-12-23 stsp goto done;
2353 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2354 15a94983 2018-12-23 stsp if (error)
2355 15a94983 2018-12-23 stsp goto done;
2357 15a94983 2018-12-23 stsp if (type1 != type2) {
2358 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2359 b00d56cd 2018-04-01 stsp goto done;
2362 15a94983 2018-12-23 stsp switch (type1) {
2363 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2364 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2365 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2367 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2368 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2369 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2371 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2372 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2373 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2374 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2377 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2380 5e70831e 2019-05-28 stsp free(label1);
2381 5e70831e 2019-05-28 stsp free(label2);
2382 15a94983 2018-12-23 stsp free(id1);
2383 15a94983 2018-12-23 stsp free(id2);
2384 927df6b7 2019-02-10 stsp free(path);
2385 b72f483a 2019-02-05 stsp if (worktree)
2386 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2387 ad242220 2018-09-08 stsp if (repo) {
2388 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2389 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2390 ad242220 2018-09-08 stsp if (error == NULL)
2391 ad242220 2018-09-08 stsp error = repo_error;
2393 b00d56cd 2018-04-01 stsp return error;
2396 404c43c4 2018-06-21 stsp __dead static void
2397 404c43c4 2018-06-21 stsp usage_blame(void)
2399 9270e621 2019-02-05 stsp fprintf(stderr,
2400 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2401 404c43c4 2018-06-21 stsp getprogname());
2405 28315671 2019-08-14 stsp struct blame_line {
2406 28315671 2019-08-14 stsp int annotated;
2407 28315671 2019-08-14 stsp char *id_str;
2408 82f6abb8 2019-08-14 stsp char *committer;
2409 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2412 28315671 2019-08-14 stsp struct blame_cb_args {
2413 28315671 2019-08-14 stsp struct blame_line *lines;
2414 28315671 2019-08-14 stsp int nlines;
2415 7ef28ff8 2019-08-14 stsp int nlines_prec;
2416 28315671 2019-08-14 stsp int lineno_cur;
2417 28315671 2019-08-14 stsp off_t *line_offsets;
2419 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2422 404c43c4 2018-06-21 stsp static const struct got_error *
2423 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2425 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2426 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2427 28315671 2019-08-14 stsp struct blame_line *bline;
2428 82f6abb8 2019-08-14 stsp char *line = NULL;
2429 28315671 2019-08-14 stsp size_t linesize = 0;
2430 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2431 28315671 2019-08-14 stsp off_t offset;
2432 bcb49d15 2019-08-14 stsp struct tm tm;
2433 bcb49d15 2019-08-14 stsp time_t committer_time;
2435 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2436 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2437 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2439 28315671 2019-08-14 stsp if (sigint_received)
2440 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2442 2839f8b9 2019-08-18 stsp if (lineno == -1)
2443 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2445 28315671 2019-08-14 stsp /* Annotate this line. */
2446 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2447 28315671 2019-08-14 stsp if (bline->annotated)
2448 28315671 2019-08-14 stsp return NULL;
2449 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2451 28315671 2019-08-14 stsp return err;
2453 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2455 82f6abb8 2019-08-14 stsp goto done;
2457 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2458 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2459 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2460 bcb49d15 2019-08-14 stsp goto done;
2463 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2464 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2465 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2466 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2467 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2468 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2469 82f6abb8 2019-08-14 stsp goto done;
2471 28315671 2019-08-14 stsp bline->annotated = 1;
2473 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2474 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2475 28315671 2019-08-14 stsp if (!bline->annotated)
2476 82f6abb8 2019-08-14 stsp goto done;
2478 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2479 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2480 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2481 82f6abb8 2019-08-14 stsp goto done;
2484 28315671 2019-08-14 stsp while (bline->annotated) {
2485 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2486 82f6abb8 2019-08-14 stsp size_t len;
2488 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2489 28315671 2019-08-14 stsp if (ferror(a->f))
2490 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2494 82f6abb8 2019-08-14 stsp committer = bline->committer;
2495 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2496 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2497 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2498 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2500 82f6abb8 2019-08-14 stsp *at = '\0';
2501 82f6abb8 2019-08-14 stsp len = strlen(committer);
2502 82f6abb8 2019-08-14 stsp if (len >= 9)
2503 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2505 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2507 28315671 2019-08-14 stsp *nl = '\0';
2508 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2509 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2511 28315671 2019-08-14 stsp a->lineno_cur++;
2512 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2515 82f6abb8 2019-08-14 stsp if (commit)
2516 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2517 28315671 2019-08-14 stsp free(line);
2518 28315671 2019-08-14 stsp return err;
2521 28315671 2019-08-14 stsp static const struct got_error *
2522 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2524 404c43c4 2018-06-21 stsp const struct got_error *error;
2525 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2526 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2527 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2528 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2529 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2530 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2531 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2532 28315671 2019-08-14 stsp struct blame_cb_args bca;
2533 28315671 2019-08-14 stsp int ch, obj_type, i;
2534 b02560ec 2019-08-19 stsp size_t filesize;
2536 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2538 404c43c4 2018-06-21 stsp #ifndef PROFILE
2539 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2540 36e2fb66 2019-01-04 stsp NULL) == -1)
2541 404c43c4 2018-06-21 stsp err(1, "pledge");
2544 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2545 404c43c4 2018-06-21 stsp switch (ch) {
2547 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2550 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2551 66bea077 2018-08-02 stsp if (repo_path == NULL)
2552 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2554 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2557 2deda0b9 2019-03-07 stsp usage_blame();
2558 404c43c4 2018-06-21 stsp /* NOTREACHED */
2562 404c43c4 2018-06-21 stsp argc -= optind;
2563 404c43c4 2018-06-21 stsp argv += optind;
2565 a39318fd 2018-08-02 stsp if (argc == 1)
2566 404c43c4 2018-06-21 stsp path = argv[0];
2568 404c43c4 2018-06-21 stsp usage_blame();
2570 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2571 66bea077 2018-08-02 stsp if (cwd == NULL) {
2572 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2573 66bea077 2018-08-02 stsp goto done;
2575 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2576 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2577 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2578 66bea077 2018-08-02 stsp goto done;
2580 0c06baac 2019-02-05 stsp error = NULL;
2581 0c06baac 2019-02-05 stsp if (worktree) {
2582 0c06baac 2019-02-05 stsp repo_path =
2583 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2584 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2585 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2586 0c06baac 2019-02-05 stsp if (error)
2587 0c06baac 2019-02-05 stsp goto done;
2589 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2590 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2591 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2592 0c06baac 2019-02-05 stsp goto done;
2597 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2598 c02c541e 2019-03-29 stsp if (error != NULL)
2599 36e2fb66 2019-01-04 stsp goto done;
2601 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2602 c02c541e 2019-03-29 stsp if (error)
2603 404c43c4 2018-06-21 stsp goto done;
2605 0c06baac 2019-02-05 stsp if (worktree) {
2606 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2607 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2608 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2609 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2610 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2611 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2612 6efaaa2d 2019-02-05 stsp path) == -1) {
2613 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2614 0c06baac 2019-02-05 stsp goto done;
2616 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2619 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2621 0c06baac 2019-02-05 stsp if (error)
2622 66bea077 2018-08-02 stsp goto done;
2624 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2625 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2626 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2627 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2628 404c43c4 2018-06-21 stsp if (error != NULL)
2629 66bea077 2018-08-02 stsp goto done;
2630 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2631 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2632 404c43c4 2018-06-21 stsp if (error != NULL)
2633 66bea077 2018-08-02 stsp goto done;
2635 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2636 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2637 30837e32 2019-07-25 stsp if (error)
2638 66bea077 2018-08-02 stsp goto done;
2641 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2642 28315671 2019-08-14 stsp if (error)
2643 28315671 2019-08-14 stsp goto done;
2645 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2646 28315671 2019-08-14 stsp if (error)
2647 28315671 2019-08-14 stsp goto done;
2649 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2650 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2651 28315671 2019-08-14 stsp goto done;
2654 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2655 28315671 2019-08-14 stsp if (error)
2656 28315671 2019-08-14 stsp goto done;
2657 28315671 2019-08-14 stsp bca.f = got_opentemp();
2658 28315671 2019-08-14 stsp if (bca.f == NULL) {
2659 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2660 28315671 2019-08-14 stsp goto done;
2662 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2663 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2664 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2665 28315671 2019-08-14 stsp goto done;
2667 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2668 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2669 b02560ec 2019-08-19 stsp bca.nlines--;
2671 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2672 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2673 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2674 28315671 2019-08-14 stsp goto done;
2676 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2677 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2678 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2679 7ef28ff8 2019-08-14 stsp while (i > 0) {
2681 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2683 82f6abb8 2019-08-14 stsp bca.repo = repo;
2685 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2686 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2688 66bea077 2018-08-02 stsp free(in_repo_path);
2689 66bea077 2018-08-02 stsp free(repo_path);
2690 66bea077 2018-08-02 stsp free(cwd);
2691 404c43c4 2018-06-21 stsp free(commit_id);
2692 28315671 2019-08-14 stsp free(obj_id);
2694 28315671 2019-08-14 stsp got_object_blob_close(blob);
2695 0c06baac 2019-02-05 stsp if (worktree)
2696 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2697 ad242220 2018-09-08 stsp if (repo) {
2698 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2699 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2700 ad242220 2018-09-08 stsp if (error == NULL)
2701 ad242220 2018-09-08 stsp error = repo_error;
2703 3affba96 2019-09-22 stsp if (bca.lines) {
2704 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2705 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2706 3affba96 2019-09-22 stsp free(bline->id_str);
2707 3affba96 2019-09-22 stsp free(bline->committer);
2709 3affba96 2019-09-22 stsp free(bca.lines);
2711 28315671 2019-08-14 stsp free(bca.line_offsets);
2712 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2713 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2714 404c43c4 2018-06-21 stsp return error;
2717 5de5890b 2018-10-18 stsp __dead static void
2718 5de5890b 2018-10-18 stsp usage_tree(void)
2720 c1669e2e 2019-01-09 stsp fprintf(stderr,
2721 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2722 5de5890b 2018-10-18 stsp getprogname());
2726 c1669e2e 2019-01-09 stsp static void
2727 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2728 c1669e2e 2019-01-09 stsp const char *root_path)
2730 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2731 848d6979 2019-08-12 stsp const char *modestr = "";
2732 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2734 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2735 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2738 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2739 63c5ca5d 2019-08-24 stsp modestr = "$";
2740 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2741 848d6979 2019-08-12 stsp modestr = "@";
2742 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2743 848d6979 2019-08-12 stsp modestr = "/";
2744 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2745 848d6979 2019-08-12 stsp modestr = "*";
2747 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2748 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2751 5de5890b 2018-10-18 stsp static const struct got_error *
2752 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2753 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2754 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2756 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2757 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2758 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2759 56e0773d 2019-11-28 stsp int nentries, i;
2761 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2763 5de5890b 2018-10-18 stsp goto done;
2765 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2767 5de5890b 2018-10-18 stsp goto done;
2768 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2769 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2770 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2771 5de5890b 2018-10-18 stsp char *id = NULL;
2773 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2776 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2777 5de5890b 2018-10-18 stsp if (show_ids) {
2778 5de5890b 2018-10-18 stsp char *id_str;
2779 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2780 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2782 5de5890b 2018-10-18 stsp goto done;
2783 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2784 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2785 5de5890b 2018-10-18 stsp free(id_str);
2786 5de5890b 2018-10-18 stsp goto done;
2788 5de5890b 2018-10-18 stsp free(id_str);
2790 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2793 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2794 c1669e2e 2019-01-09 stsp char *child_path;
2795 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2796 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2797 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2798 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2799 c1669e2e 2019-01-09 stsp goto done;
2801 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2802 c1669e2e 2019-01-09 stsp root_path, repo);
2803 c1669e2e 2019-01-09 stsp free(child_path);
2805 c1669e2e 2019-01-09 stsp goto done;
2810 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2811 5de5890b 2018-10-18 stsp free(tree_id);
2812 5de5890b 2018-10-18 stsp return err;
2815 5de5890b 2018-10-18 stsp static const struct got_error *
2816 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2818 5de5890b 2018-10-18 stsp const struct got_error *error;
2819 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2820 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2821 7a2c19d6 2019-02-05 stsp const char *path;
2822 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2823 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2824 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2825 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2828 5de5890b 2018-10-18 stsp #ifndef PROFILE
2829 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2830 0f8d269b 2019-01-04 stsp NULL) == -1)
2831 5de5890b 2018-10-18 stsp err(1, "pledge");
2834 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2835 5de5890b 2018-10-18 stsp switch (ch) {
2837 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2840 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2841 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2842 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2844 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2847 5de5890b 2018-10-18 stsp show_ids = 1;
2850 c1669e2e 2019-01-09 stsp recurse = 1;
2853 2deda0b9 2019-03-07 stsp usage_tree();
2854 5de5890b 2018-10-18 stsp /* NOTREACHED */
2858 5de5890b 2018-10-18 stsp argc -= optind;
2859 5de5890b 2018-10-18 stsp argv += optind;
2861 5de5890b 2018-10-18 stsp if (argc == 1)
2862 5de5890b 2018-10-18 stsp path = argv[0];
2863 5de5890b 2018-10-18 stsp else if (argc > 1)
2864 5de5890b 2018-10-18 stsp usage_tree();
2866 7a2c19d6 2019-02-05 stsp path = NULL;
2868 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2869 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2870 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2871 9bf7a39b 2019-02-05 stsp goto done;
2873 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2874 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2875 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2876 7a2c19d6 2019-02-05 stsp goto done;
2878 7a2c19d6 2019-02-05 stsp error = NULL;
2879 7a2c19d6 2019-02-05 stsp if (worktree) {
2880 7a2c19d6 2019-02-05 stsp repo_path =
2881 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2882 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2883 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2884 7a2c19d6 2019-02-05 stsp if (error)
2885 7a2c19d6 2019-02-05 stsp goto done;
2887 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2888 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2889 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2890 7a2c19d6 2019-02-05 stsp goto done;
2895 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2896 5de5890b 2018-10-18 stsp if (error != NULL)
2897 c02c541e 2019-03-29 stsp goto done;
2899 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2900 c02c541e 2019-03-29 stsp if (error)
2901 5de5890b 2018-10-18 stsp goto done;
2903 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2904 9bf7a39b 2019-02-05 stsp if (worktree) {
2905 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2906 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2907 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2908 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2909 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2910 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2911 9bf7a39b 2019-02-05 stsp goto done;
2913 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2915 9bf7a39b 2019-02-05 stsp if (error)
2916 9bf7a39b 2019-02-05 stsp goto done;
2918 9bf7a39b 2019-02-05 stsp path = "/";
2920 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2921 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2922 9bf7a39b 2019-02-05 stsp if (error != NULL)
2923 9bf7a39b 2019-02-05 stsp goto done;
2926 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2927 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2928 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2929 5de5890b 2018-10-18 stsp if (error != NULL)
2930 5de5890b 2018-10-18 stsp goto done;
2931 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2932 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2933 5de5890b 2018-10-18 stsp if (error != NULL)
2934 5de5890b 2018-10-18 stsp goto done;
2936 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2937 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2938 30837e32 2019-07-25 stsp if (error)
2939 5de5890b 2018-10-18 stsp goto done;
2942 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2943 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2945 5de5890b 2018-10-18 stsp free(in_repo_path);
2946 5de5890b 2018-10-18 stsp free(repo_path);
2947 5de5890b 2018-10-18 stsp free(cwd);
2948 5de5890b 2018-10-18 stsp free(commit_id);
2949 7a2c19d6 2019-02-05 stsp if (worktree)
2950 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2951 5de5890b 2018-10-18 stsp if (repo) {
2952 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2953 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2954 5de5890b 2018-10-18 stsp if (error == NULL)
2955 5de5890b 2018-10-18 stsp error = repo_error;
2957 5de5890b 2018-10-18 stsp return error;
2960 6bad629b 2019-02-04 stsp __dead static void
2961 6bad629b 2019-02-04 stsp usage_status(void)
2963 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2967 b72f483a 2019-02-05 stsp static const struct got_error *
2968 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2969 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2970 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2971 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2973 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2974 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2975 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2976 b72f483a 2019-02-05 stsp return NULL;
2979 6bad629b 2019-02-04 stsp static const struct got_error *
2980 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2982 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2983 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2984 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2985 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2986 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2987 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2990 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2992 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2993 6bad629b 2019-02-04 stsp switch (ch) {
2995 2deda0b9 2019-03-07 stsp usage_status();
2996 6bad629b 2019-02-04 stsp /* NOTREACHED */
3000 6bad629b 2019-02-04 stsp argc -= optind;
3001 6bad629b 2019-02-04 stsp argv += optind;
3003 6bad629b 2019-02-04 stsp #ifndef PROFILE
3004 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3005 6bad629b 2019-02-04 stsp NULL) == -1)
3006 6bad629b 2019-02-04 stsp err(1, "pledge");
3008 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3009 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3010 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3011 927df6b7 2019-02-10 stsp goto done;
3014 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3015 927df6b7 2019-02-10 stsp if (error != NULL)
3016 a5edda0a 2019-07-27 stsp goto done;
3018 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3020 6bad629b 2019-02-04 stsp if (error != NULL)
3021 6bad629b 2019-02-04 stsp goto done;
3023 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3024 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3025 087fb88c 2019-08-04 stsp if (error)
3026 087fb88c 2019-08-04 stsp goto done;
3028 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3029 6bad629b 2019-02-04 stsp if (error)
3030 6bad629b 2019-02-04 stsp goto done;
3032 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3033 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3035 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3036 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3037 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3038 927df6b7 2019-02-10 stsp free(cwd);
3039 d0eebce4 2019-03-11 stsp return error;
3042 d0eebce4 2019-03-11 stsp __dead static void
3043 d0eebce4 2019-03-11 stsp usage_ref(void)
3045 d0eebce4 2019-03-11 stsp fprintf(stderr,
3046 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3047 d0eebce4 2019-03-11 stsp getprogname());
3051 d0eebce4 2019-03-11 stsp static const struct got_error *
3052 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3054 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3055 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3056 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3058 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3059 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3061 d0eebce4 2019-03-11 stsp return err;
3063 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3064 d0eebce4 2019-03-11 stsp char *refstr;
3065 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3066 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3067 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3068 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3069 d0eebce4 2019-03-11 stsp free(refstr);
3072 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3073 d0eebce4 2019-03-11 stsp return NULL;
3076 d0eebce4 2019-03-11 stsp static const struct got_error *
3077 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3079 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3080 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3082 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3084 d0eebce4 2019-03-11 stsp return err;
3086 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3087 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3088 d0eebce4 2019-03-11 stsp return err;
3091 d0eebce4 2019-03-11 stsp static const struct got_error *
3092 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3095 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3096 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3099 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name