Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
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.
8 5c860e29 2018-03-12 stsp *
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.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 f42b1b34 2018-03-12 stsp
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
33 5c860e29 2018-03-12 stsp
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 63219cd2 2019-01-04 stsp #include "got_privsep.h"
43 5c860e29 2018-03-12 stsp
44 5c860e29 2018-03-12 stsp #ifndef nitems
45 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 5c860e29 2018-03-12 stsp #endif
47 99437157 2018-11-11 stsp
48 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
50 99437157 2018-11-11 stsp
51 99437157 2018-11-11 stsp static void
52 99437157 2018-11-11 stsp catch_sigint(int signo)
53 99437157 2018-11-11 stsp {
54 99437157 2018-11-11 stsp sigint_received = 1;
55 99437157 2018-11-11 stsp }
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigpipe(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigpipe_received = 1;
61 99437157 2018-11-11 stsp }
62 5c860e29 2018-03-12 stsp
63 99437157 2018-11-11 stsp
64 5c860e29 2018-03-12 stsp struct cmd {
65 5c860e29 2018-03-12 stsp const char *cmd_name;
66 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
67 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
68 46a0db7d 2018-03-12 stsp const char *cmd_descr;
69 5c860e29 2018-03-12 stsp };
70 5c860e29 2018-03-12 stsp
71 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
73 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
76 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
77 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
78 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
79 5c860e29 2018-03-12 stsp
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
81 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
84 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
85 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
87 5c860e29 2018-03-12 stsp
88 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
89 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
90 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
91 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
92 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
93 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
94 1b6b95a8 2018-03-12 stsp "show repository history" },
95 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
96 b00d56cd 2018-04-01 stsp "compare files and directories" },
97 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
98 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
99 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
100 5de5890b 2018-10-18 stsp " list files and directories in repository" },
101 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
102 1b6b95a8 2018-03-12 stsp "show modification status of files" },
103 5c860e29 2018-03-12 stsp };
104 5c860e29 2018-03-12 stsp
105 5c860e29 2018-03-12 stsp int
106 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
107 5c860e29 2018-03-12 stsp {
108 5c860e29 2018-03-12 stsp struct cmd *cmd;
109 5c860e29 2018-03-12 stsp unsigned int i;
110 5c860e29 2018-03-12 stsp int ch;
111 1b6b95a8 2018-03-12 stsp int hflag = 0;
112 5c860e29 2018-03-12 stsp
113 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
114 5c860e29 2018-03-12 stsp
115 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
116 5c860e29 2018-03-12 stsp switch (ch) {
117 1b6b95a8 2018-03-12 stsp case 'h':
118 1b6b95a8 2018-03-12 stsp hflag = 1;
119 1b6b95a8 2018-03-12 stsp break;
120 5c860e29 2018-03-12 stsp default:
121 5c860e29 2018-03-12 stsp usage();
122 5c860e29 2018-03-12 stsp /* NOTREACHED */
123 5c860e29 2018-03-12 stsp }
124 5c860e29 2018-03-12 stsp }
125 5c860e29 2018-03-12 stsp
126 5c860e29 2018-03-12 stsp argc -= optind;
127 5c860e29 2018-03-12 stsp argv += optind;
128 1e70621d 2018-03-27 stsp optind = 0;
129 5c860e29 2018-03-12 stsp
130 5c860e29 2018-03-12 stsp if (argc <= 0)
131 5c860e29 2018-03-12 stsp usage();
132 5c860e29 2018-03-12 stsp
133 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
134 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
135 99437157 2018-11-11 stsp
136 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
137 d7d4f210 2018-03-12 stsp const struct got_error *error;
138 d7d4f210 2018-03-12 stsp
139 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
140 5c860e29 2018-03-12 stsp
141 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
142 5c860e29 2018-03-12 stsp continue;
143 5c860e29 2018-03-12 stsp
144 1b6b95a8 2018-03-12 stsp if (hflag)
145 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
146 1b6b95a8 2018-03-12 stsp
147 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
148 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
149 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
150 d7d4f210 2018-03-12 stsp return 1;
151 d7d4f210 2018-03-12 stsp }
152 d7d4f210 2018-03-12 stsp
153 d7d4f210 2018-03-12 stsp return 0;
154 5c860e29 2018-03-12 stsp }
155 5c860e29 2018-03-12 stsp
156 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
157 5c860e29 2018-03-12 stsp return 1;
158 5c860e29 2018-03-12 stsp }
159 5c860e29 2018-03-12 stsp
160 4ed7e80c 2018-05-20 stsp __dead static void
161 5c860e29 2018-03-12 stsp usage(void)
162 5c860e29 2018-03-12 stsp {
163 46a0db7d 2018-03-12 stsp int i;
164 46a0db7d 2018-03-12 stsp
165 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
166 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
167 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
168 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
169 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
170 46a0db7d 2018-03-12 stsp }
171 5c860e29 2018-03-12 stsp exit(1);
172 5c860e29 2018-03-12 stsp }
173 5c860e29 2018-03-12 stsp
174 0266afb7 2019-01-04 stsp static const struct got_error *
175 0266afb7 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
176 0266afb7 2019-01-04 stsp {
177 0266afb7 2019-01-04 stsp const struct got_error *error;
178 0266afb7 2019-01-04 stsp
179 0266afb7 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
180 0266afb7 2019-01-04 stsp return got_error_from_errno();
181 0266afb7 2019-01-04 stsp
182 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
183 0266afb7 2019-01-04 stsp return got_error_from_errno();
184 0266afb7 2019-01-04 stsp
185 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
186 0266afb7 2019-01-04 stsp return got_error_from_errno();
187 0266afb7 2019-01-04 stsp
188 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
189 0266afb7 2019-01-04 stsp if (error != NULL)
190 0266afb7 2019-01-04 stsp return error;
191 0266afb7 2019-01-04 stsp
192 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
193 0266afb7 2019-01-04 stsp return got_error_from_errno();
194 0266afb7 2019-01-04 stsp
195 0266afb7 2019-01-04 stsp return NULL;
196 0266afb7 2019-01-04 stsp }
197 0266afb7 2019-01-04 stsp
198 4ed7e80c 2018-05-20 stsp __dead static void
199 c09a553d 2018-03-12 stsp usage_checkout(void)
200 c09a553d 2018-03-12 stsp {
201 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
202 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
203 c09a553d 2018-03-12 stsp exit(1);
204 92a684f4 2018-03-12 stsp }
205 92a684f4 2018-03-12 stsp
206 92a684f4 2018-03-12 stsp static void
207 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
208 92a684f4 2018-03-12 stsp {
209 92a684f4 2018-03-12 stsp char *worktree_path = arg;
210 92a684f4 2018-03-12 stsp
211 92a684f4 2018-03-12 stsp while (path[0] == '/')
212 92a684f4 2018-03-12 stsp path++;
213 92a684f4 2018-03-12 stsp
214 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
215 99437157 2018-11-11 stsp }
216 99437157 2018-11-11 stsp
217 99437157 2018-11-11 stsp static const struct got_error *
218 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
219 99437157 2018-11-11 stsp {
220 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
221 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
222 99437157 2018-11-11 stsp return NULL;
223 8069f636 2019-01-12 stsp }
224 8069f636 2019-01-12 stsp
225 8069f636 2019-01-12 stsp static const struct got_error *
226 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
227 8069f636 2019-01-12 stsp struct got_repository *repo)
228 8069f636 2019-01-12 stsp {
229 8069f636 2019-01-12 stsp const struct got_error *err;
230 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
231 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
232 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
233 8069f636 2019-01-12 stsp
234 8069f636 2019-01-12 stsp head_ref = got_worktree_get_head_ref(worktree);
235 8069f636 2019-01-12 stsp if (head_ref == NULL)
236 8069f636 2019-01-12 stsp return got_error_from_errno();
237 8069f636 2019-01-12 stsp
238 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
239 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
240 8069f636 2019-01-12 stsp if (err)
241 8069f636 2019-01-12 stsp goto done;
242 8069f636 2019-01-12 stsp
243 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
244 8069f636 2019-01-12 stsp if (err)
245 8069f636 2019-01-12 stsp goto done;
246 8069f636 2019-01-12 stsp
247 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
248 8069f636 2019-01-12 stsp if (err)
249 8069f636 2019-01-12 stsp goto done;
250 8069f636 2019-01-12 stsp while (1) {
251 8069f636 2019-01-12 stsp struct got_object_id *id;
252 8069f636 2019-01-12 stsp
253 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
254 8069f636 2019-01-12 stsp break;
255 8069f636 2019-01-12 stsp
256 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
257 8069f636 2019-01-12 stsp if (err) {
258 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
259 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
260 8069f636 2019-01-12 stsp break;
261 8069f636 2019-01-12 stsp }
262 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
263 8069f636 2019-01-12 stsp break;
264 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
265 8069f636 2019-01-12 stsp if (err)
266 8069f636 2019-01-12 stsp break;
267 8069f636 2019-01-12 stsp else
268 8069f636 2019-01-12 stsp continue;
269 8069f636 2019-01-12 stsp }
270 8069f636 2019-01-12 stsp if (id == NULL)
271 8069f636 2019-01-12 stsp break;
272 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
273 8069f636 2019-01-12 stsp break;
274 8069f636 2019-01-12 stsp }
275 8069f636 2019-01-12 stsp done:
276 8069f636 2019-01-12 stsp if (head_ref)
277 8069f636 2019-01-12 stsp got_ref_close(head_ref);
278 8069f636 2019-01-12 stsp if (graph)
279 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
280 8069f636 2019-01-12 stsp return err;
281 c09a553d 2018-03-12 stsp }
282 c09a553d 2018-03-12 stsp
283 8069f636 2019-01-12 stsp
284 4ed7e80c 2018-05-20 stsp static const struct got_error *
285 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
286 c09a553d 2018-03-12 stsp {
287 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
288 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
289 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
290 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
291 c09a553d 2018-03-12 stsp char *repo_path = NULL;
292 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
293 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
294 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
295 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
296 c09a553d 2018-03-12 stsp
297 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
298 0bb8a95e 2018-03-12 stsp switch (ch) {
299 8069f636 2019-01-12 stsp case 'c':
300 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
301 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
302 8069f636 2019-01-12 stsp return got_error_from_errno();
303 8069f636 2019-01-12 stsp break;
304 0bb8a95e 2018-03-12 stsp case 'p':
305 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
306 0bb8a95e 2018-03-12 stsp break;
307 0bb8a95e 2018-03-12 stsp default:
308 0bb8a95e 2018-03-12 stsp usage();
309 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
310 0bb8a95e 2018-03-12 stsp }
311 0bb8a95e 2018-03-12 stsp }
312 0bb8a95e 2018-03-12 stsp
313 0bb8a95e 2018-03-12 stsp argc -= optind;
314 0bb8a95e 2018-03-12 stsp argv += optind;
315 0bb8a95e 2018-03-12 stsp
316 6715a751 2018-03-16 stsp #ifndef PROFILE
317 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
318 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
319 c09a553d 2018-03-12 stsp err(1, "pledge");
320 6715a751 2018-03-16 stsp #endif
321 0bb8a95e 2018-03-12 stsp if (argc == 1) {
322 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
323 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
324 76089277 2018-04-01 stsp if (repo_path == NULL)
325 76089277 2018-04-01 stsp return got_error_from_errno();
326 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
327 76089277 2018-04-01 stsp if (cwd == NULL) {
328 76089277 2018-04-01 stsp error = got_error_from_errno();
329 76089277 2018-04-01 stsp goto done;
330 76089277 2018-04-01 stsp }
331 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
332 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
333 5d7c1dab 2018-04-01 stsp else
334 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
335 76089277 2018-04-01 stsp if (base == NULL) {
336 76089277 2018-04-01 stsp error = got_error_from_errno();
337 76089277 2018-04-01 stsp goto done;
338 76089277 2018-04-01 stsp }
339 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
340 c09a553d 2018-03-12 stsp if (dotgit)
341 c09a553d 2018-03-12 stsp *dotgit = '\0';
342 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
343 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
344 c09a553d 2018-03-12 stsp free(cwd);
345 76089277 2018-04-01 stsp goto done;
346 c09a553d 2018-03-12 stsp }
347 c09a553d 2018-03-12 stsp free(cwd);
348 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
349 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
350 76089277 2018-04-01 stsp if (repo_path == NULL) {
351 76089277 2018-04-01 stsp error = got_error_from_errno();
352 76089277 2018-04-01 stsp goto done;
353 76089277 2018-04-01 stsp }
354 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
355 76089277 2018-04-01 stsp if (worktree_path == NULL) {
356 76089277 2018-04-01 stsp error = got_error_from_errno();
357 76089277 2018-04-01 stsp goto done;
358 76089277 2018-04-01 stsp }
359 c09a553d 2018-03-12 stsp } else
360 c09a553d 2018-03-12 stsp usage_checkout();
361 63219cd2 2019-01-04 stsp
362 0266afb7 2019-01-04 stsp error = apply_unveil(repo_path, worktree_path);
363 0266afb7 2019-01-04 stsp if (error)
364 63219cd2 2019-01-04 stsp goto done;
365 c09a553d 2018-03-12 stsp
366 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
367 c09a553d 2018-03-12 stsp if (error != NULL)
368 c09a553d 2018-03-12 stsp goto done;
369 8069f636 2019-01-12 stsp
370 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
371 c09a553d 2018-03-12 stsp if (error != NULL)
372 c09a553d 2018-03-12 stsp goto done;
373 c09a553d 2018-03-12 stsp
374 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
375 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
376 c09a553d 2018-03-12 stsp goto done;
377 c09a553d 2018-03-12 stsp
378 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
379 c09a553d 2018-03-12 stsp if (error != NULL)
380 c09a553d 2018-03-12 stsp goto done;
381 c09a553d 2018-03-12 stsp
382 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
383 e5dc7198 2018-12-29 stsp path_prefix);
384 e5dc7198 2018-12-29 stsp if (error != NULL)
385 e5dc7198 2018-12-29 stsp goto done;
386 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
387 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
388 49520a32 2018-12-29 stsp goto done;
389 49520a32 2018-12-29 stsp }
390 49520a32 2018-12-29 stsp
391 8069f636 2019-01-12 stsp if (commit_id_str) {
392 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
393 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
394 8069f636 2019-01-12 stsp commit_id_str);
395 8069f636 2019-01-12 stsp if (error != NULL)
396 8069f636 2019-01-12 stsp goto done;
397 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
398 8069f636 2019-01-12 stsp if (error != NULL) {
399 8069f636 2019-01-12 stsp free(commit_id);
400 8069f636 2019-01-12 stsp goto done;
401 8069f636 2019-01-12 stsp }
402 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
403 8069f636 2019-01-12 stsp commit_id);
404 8069f636 2019-01-12 stsp free(commit_id);
405 8069f636 2019-01-12 stsp if (error)
406 8069f636 2019-01-12 stsp goto done;
407 8069f636 2019-01-12 stsp }
408 8069f636 2019-01-12 stsp
409 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
410 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
411 c09a553d 2018-03-12 stsp if (error != NULL)
412 c09a553d 2018-03-12 stsp goto done;
413 c09a553d 2018-03-12 stsp
414 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
415 c09a553d 2018-03-12 stsp
416 c09a553d 2018-03-12 stsp done:
417 8069f636 2019-01-12 stsp free(commit_id_str);
418 76089277 2018-04-01 stsp free(repo_path);
419 507dc3bb 2018-12-29 stsp free(worktree_path);
420 507dc3bb 2018-12-29 stsp return error;
421 507dc3bb 2018-12-29 stsp }
422 507dc3bb 2018-12-29 stsp
423 507dc3bb 2018-12-29 stsp __dead static void
424 507dc3bb 2018-12-29 stsp usage_update(void)
425 507dc3bb 2018-12-29 stsp {
426 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
427 507dc3bb 2018-12-29 stsp getprogname());
428 507dc3bb 2018-12-29 stsp exit(1);
429 507dc3bb 2018-12-29 stsp }
430 507dc3bb 2018-12-29 stsp
431 507dc3bb 2018-12-29 stsp static void
432 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
433 507dc3bb 2018-12-29 stsp {
434 784955db 2019-01-12 stsp int *did_something = arg;
435 784955db 2019-01-12 stsp
436 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
437 507dc3bb 2018-12-29 stsp return;
438 507dc3bb 2018-12-29 stsp
439 1545c615 2019-02-10 stsp *did_something = 1;
440 507dc3bb 2018-12-29 stsp while (path[0] == '/')
441 507dc3bb 2018-12-29 stsp path++;
442 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
443 be7061eb 2018-12-30 stsp }
444 be7061eb 2018-12-30 stsp
445 be7061eb 2018-12-30 stsp static const struct got_error *
446 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
447 507dc3bb 2018-12-29 stsp {
448 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
449 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
450 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
451 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
452 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
453 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
454 784955db 2019-01-12 stsp int ch, did_something = 0;
455 507dc3bb 2018-12-29 stsp
456 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
457 507dc3bb 2018-12-29 stsp switch (ch) {
458 507dc3bb 2018-12-29 stsp case 'c':
459 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
460 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
461 9c4b8182 2019-01-02 stsp return got_error_from_errno();
462 507dc3bb 2018-12-29 stsp break;
463 507dc3bb 2018-12-29 stsp default:
464 507dc3bb 2018-12-29 stsp usage();
465 507dc3bb 2018-12-29 stsp /* NOTREACHED */
466 507dc3bb 2018-12-29 stsp }
467 507dc3bb 2018-12-29 stsp }
468 507dc3bb 2018-12-29 stsp
469 507dc3bb 2018-12-29 stsp argc -= optind;
470 507dc3bb 2018-12-29 stsp argv += optind;
471 507dc3bb 2018-12-29 stsp
472 507dc3bb 2018-12-29 stsp #ifndef PROFILE
473 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
474 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
475 507dc3bb 2018-12-29 stsp err(1, "pledge");
476 507dc3bb 2018-12-29 stsp #endif
477 507dc3bb 2018-12-29 stsp if (argc == 0) {
478 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
479 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
480 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
481 507dc3bb 2018-12-29 stsp goto done;
482 507dc3bb 2018-12-29 stsp }
483 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
484 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
485 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
486 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
487 507dc3bb 2018-12-29 stsp goto done;
488 507dc3bb 2018-12-29 stsp }
489 507dc3bb 2018-12-29 stsp } else
490 507dc3bb 2018-12-29 stsp usage_update();
491 507dc3bb 2018-12-29 stsp
492 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
493 507dc3bb 2018-12-29 stsp if (error != NULL)
494 507dc3bb 2018-12-29 stsp goto done;
495 507dc3bb 2018-12-29 stsp
496 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
497 507dc3bb 2018-12-29 stsp if (error != NULL)
498 507dc3bb 2018-12-29 stsp goto done;
499 507dc3bb 2018-12-29 stsp
500 8c02d095 2019-02-05 stsp error = apply_unveil(got_repo_get_path(repo),
501 8c02d095 2019-02-05 stsp got_worktree_get_root_path(worktree));
502 0266afb7 2019-01-04 stsp if (error)
503 0266afb7 2019-01-04 stsp goto done;
504 0266afb7 2019-01-04 stsp
505 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
506 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
507 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
508 507dc3bb 2018-12-29 stsp if (error != NULL)
509 507dc3bb 2018-12-29 stsp goto done;
510 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
511 9c4b8182 2019-01-02 stsp if (error != NULL)
512 9c4b8182 2019-01-02 stsp goto done;
513 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
514 507dc3bb 2018-12-29 stsp if (error != NULL)
515 507dc3bb 2018-12-29 stsp goto done;
516 507dc3bb 2018-12-29 stsp } else {
517 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
518 507dc3bb 2018-12-29 stsp commit_id_str);
519 507dc3bb 2018-12-29 stsp if (error != NULL)
520 507dc3bb 2018-12-29 stsp goto done;
521 507dc3bb 2018-12-29 stsp }
522 35c965b2 2018-12-29 stsp
523 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
524 be7061eb 2018-12-30 stsp if (error != NULL)
525 be7061eb 2018-12-30 stsp goto done;
526 507dc3bb 2018-12-29 stsp
527 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
528 507dc3bb 2018-12-29 stsp commit_id) != 0) {
529 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
530 507dc3bb 2018-12-29 stsp commit_id);
531 507dc3bb 2018-12-29 stsp if (error)
532 507dc3bb 2018-12-29 stsp goto done;
533 507dc3bb 2018-12-29 stsp }
534 507dc3bb 2018-12-29 stsp
535 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
536 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
537 507dc3bb 2018-12-29 stsp if (error != NULL)
538 507dc3bb 2018-12-29 stsp goto done;
539 9c4b8182 2019-01-02 stsp
540 784955db 2019-01-12 stsp if (did_something)
541 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
542 784955db 2019-01-12 stsp else
543 784955db 2019-01-12 stsp printf("Already up-to-date\n");
544 507dc3bb 2018-12-29 stsp done:
545 c09a553d 2018-03-12 stsp free(worktree_path);
546 507dc3bb 2018-12-29 stsp free(commit_id);
547 9c4b8182 2019-01-02 stsp free(commit_id_str);
548 c09a553d 2018-03-12 stsp return error;
549 c09a553d 2018-03-12 stsp }
550 c09a553d 2018-03-12 stsp
551 f42b1b34 2018-03-12 stsp static const struct got_error *
552 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
553 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
554 5c860e29 2018-03-12 stsp {
555 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
556 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
557 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
558 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
559 79109fed 2018-03-27 stsp
560 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
561 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
562 79109fed 2018-03-27 stsp if (err)
563 79109fed 2018-03-27 stsp return err;
564 79109fed 2018-03-27 stsp
565 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
566 79f35eb3 2018-06-11 stsp if (qid != NULL) {
567 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
568 79109fed 2018-03-27 stsp
569 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
570 79109fed 2018-03-27 stsp if (err)
571 79109fed 2018-03-27 stsp return err;
572 79109fed 2018-03-27 stsp
573 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
574 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
575 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
576 0f2b3dca 2018-12-22 stsp if (err)
577 0f2b3dca 2018-12-22 stsp return err;
578 0f2b3dca 2018-12-22 stsp
579 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
580 79109fed 2018-03-27 stsp if (err)
581 79109fed 2018-03-27 stsp return err;
582 79109fed 2018-03-27 stsp }
583 79109fed 2018-03-27 stsp
584 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
585 0f2b3dca 2018-12-22 stsp if (err)
586 0f2b3dca 2018-12-22 stsp goto done;
587 0f2b3dca 2018-12-22 stsp
588 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
589 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
590 0f2b3dca 2018-12-22 stsp done:
591 79109fed 2018-03-27 stsp if (tree1)
592 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
593 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
594 0f2b3dca 2018-12-22 stsp free(id_str1);
595 0f2b3dca 2018-12-22 stsp free(id_str2);
596 79109fed 2018-03-27 stsp return err;
597 79109fed 2018-03-27 stsp }
598 79109fed 2018-03-27 stsp
599 4bb494d5 2018-06-16 stsp static char *
600 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
601 6c281f94 2018-06-11 stsp {
602 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
603 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
604 6c281f94 2018-06-11 stsp if (p)
605 6c281f94 2018-06-11 stsp *p = '\0';
606 6c281f94 2018-06-11 stsp return s;
607 6c281f94 2018-06-11 stsp }
608 6c281f94 2018-06-11 stsp
609 79109fed 2018-03-27 stsp static const struct got_error *
610 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
611 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
612 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
613 79109fed 2018-03-27 stsp {
614 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
615 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
616 6c281f94 2018-06-11 stsp char datebuf[26];
617 45d799e2 2018-12-23 stsp time_t committer_time;
618 45d799e2 2018-12-23 stsp const char *author, *committer;
619 199a4027 2019-02-02 stsp char *refs_str = NULL;
620 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
621 5c860e29 2018-03-12 stsp
622 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
623 199a4027 2019-02-02 stsp char *s;
624 199a4027 2019-02-02 stsp const char *name;
625 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
626 199a4027 2019-02-02 stsp continue;
627 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
628 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
629 d9498b20 2019-02-05 stsp continue;
630 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
631 199a4027 2019-02-02 stsp name += 5;
632 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
633 e34f9ed6 2019-02-02 stsp name += 6;
634 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
635 141c2bff 2019-02-04 stsp name += 8;
636 199a4027 2019-02-02 stsp s = refs_str;
637 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
638 199a4027 2019-02-02 stsp name) == -1) {
639 199a4027 2019-02-02 stsp err = got_error_from_errno();
640 199a4027 2019-02-02 stsp free(s);
641 199a4027 2019-02-02 stsp break;
642 199a4027 2019-02-02 stsp }
643 199a4027 2019-02-02 stsp free(s);
644 199a4027 2019-02-02 stsp }
645 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
646 8bf5b3c9 2018-03-17 stsp if (err)
647 8bf5b3c9 2018-03-17 stsp return err;
648 788c352e 2018-06-16 stsp
649 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
650 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
651 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
652 832c249c 2018-06-10 stsp free(id_str);
653 d3d493d7 2019-02-21 stsp id_str = NULL;
654 d3d493d7 2019-02-21 stsp free(refs_str);
655 d3d493d7 2019-02-21 stsp refs_str = NULL;
656 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
657 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
658 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
659 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
660 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
661 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
662 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
663 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
664 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
665 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
666 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
667 3fe1abad 2018-06-10 stsp int n = 1;
668 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
669 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
670 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
671 a0603db2 2018-06-10 stsp if (err)
672 a0603db2 2018-06-10 stsp return err;
673 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
674 a0603db2 2018-06-10 stsp free(id_str);
675 a0603db2 2018-06-10 stsp }
676 a0603db2 2018-06-10 stsp }
677 832c249c 2018-06-10 stsp
678 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
679 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
680 832c249c 2018-06-10 stsp return got_error_from_errno();
681 8bf5b3c9 2018-03-17 stsp
682 621015ac 2018-07-23 stsp logmsg = logmsg0;
683 832c249c 2018-06-10 stsp do {
684 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
685 832c249c 2018-06-10 stsp if (line)
686 832c249c 2018-06-10 stsp printf(" %s\n", line);
687 832c249c 2018-06-10 stsp } while (line);
688 621015ac 2018-07-23 stsp free(logmsg0);
689 832c249c 2018-06-10 stsp
690 971751ac 2018-03-27 stsp if (show_patch) {
691 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
692 971751ac 2018-03-27 stsp if (err == 0)
693 971751ac 2018-03-27 stsp printf("\n");
694 971751ac 2018-03-27 stsp }
695 07862c20 2018-09-15 stsp
696 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
697 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
698 07862c20 2018-09-15 stsp return err;
699 f42b1b34 2018-03-12 stsp }
700 5c860e29 2018-03-12 stsp
701 f42b1b34 2018-03-12 stsp static const struct got_error *
702 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
703 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
704 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
705 f42b1b34 2018-03-12 stsp {
706 f42b1b34 2018-03-12 stsp const struct got_error *err;
707 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
708 372ccdbb 2018-06-10 stsp
709 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
710 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
711 f42b1b34 2018-03-12 stsp if (err)
712 f42b1b34 2018-03-12 stsp return err;
713 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
714 372ccdbb 2018-06-10 stsp if (err)
715 fcc85cad 2018-07-22 stsp goto done;
716 31cedeaf 2018-09-15 stsp while (1) {
717 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
718 372ccdbb 2018-06-10 stsp struct got_object_id *id;
719 8bf5b3c9 2018-03-17 stsp
720 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
721 84453469 2018-11-11 stsp break;
722 84453469 2018-11-11 stsp
723 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
724 372ccdbb 2018-06-10 stsp if (err) {
725 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
726 9ba79e04 2018-06-11 stsp err = NULL;
727 9ba79e04 2018-06-11 stsp break;
728 9ba79e04 2018-06-11 stsp }
729 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
730 372ccdbb 2018-06-10 stsp break;
731 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
732 372ccdbb 2018-06-10 stsp if (err)
733 372ccdbb 2018-06-10 stsp break;
734 372ccdbb 2018-06-10 stsp else
735 372ccdbb 2018-06-10 stsp continue;
736 7e665116 2018-04-02 stsp }
737 b43fbaa0 2018-06-11 stsp if (id == NULL)
738 7e665116 2018-04-02 stsp break;
739 b43fbaa0 2018-06-11 stsp
740 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
741 b43fbaa0 2018-06-11 stsp if (err)
742 fcc85cad 2018-07-22 stsp break;
743 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
744 199a4027 2019-02-02 stsp refs);
745 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
746 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
747 7e665116 2018-04-02 stsp break;
748 31cedeaf 2018-09-15 stsp }
749 fcc85cad 2018-07-22 stsp done:
750 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
751 f42b1b34 2018-03-12 stsp return err;
752 f42b1b34 2018-03-12 stsp }
753 5c860e29 2018-03-12 stsp
754 4ed7e80c 2018-05-20 stsp __dead static void
755 6f3d1eb0 2018-03-12 stsp usage_log(void)
756 6f3d1eb0 2018-03-12 stsp {
757 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
758 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
759 6f3d1eb0 2018-03-12 stsp exit(1);
760 6f3d1eb0 2018-03-12 stsp }
761 6f3d1eb0 2018-03-12 stsp
762 4ed7e80c 2018-05-20 stsp static const struct got_error *
763 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
764 f42b1b34 2018-03-12 stsp {
765 f42b1b34 2018-03-12 stsp const struct got_error *error;
766 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
767 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
768 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
769 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
770 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
771 d142fc45 2018-04-01 stsp char *start_commit = NULL;
772 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
773 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
774 64a96a6d 2018-04-01 stsp const char *errstr;
775 199a4027 2019-02-02 stsp struct got_reflist_head refs;
776 5c860e29 2018-03-12 stsp
777 6715a751 2018-03-16 stsp #ifndef PROFILE
778 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
779 6098196c 2019-01-04 stsp NULL)
780 ad242220 2018-09-08 stsp == -1)
781 f42b1b34 2018-03-12 stsp err(1, "pledge");
782 6715a751 2018-03-16 stsp #endif
783 79109fed 2018-03-27 stsp
784 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
785 79109fed 2018-03-27 stsp switch (ch) {
786 79109fed 2018-03-27 stsp case 'p':
787 79109fed 2018-03-27 stsp show_patch = 1;
788 d142fc45 2018-04-01 stsp break;
789 d142fc45 2018-04-01 stsp case 'c':
790 d142fc45 2018-04-01 stsp start_commit = optarg;
791 64a96a6d 2018-04-01 stsp break;
792 c0cc5c62 2018-10-18 stsp case 'C':
793 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
794 4a8520aa 2018-10-18 stsp &errstr);
795 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
796 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
797 c0cc5c62 2018-10-18 stsp break;
798 64a96a6d 2018-04-01 stsp case 'l':
799 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
800 64a96a6d 2018-04-01 stsp if (errstr != NULL)
801 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
802 79109fed 2018-03-27 stsp break;
803 0ed6ed4c 2018-06-13 stsp case 'f':
804 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
805 a0603db2 2018-06-10 stsp break;
806 04ca23f4 2018-07-16 stsp case 'r':
807 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
808 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
809 04ca23f4 2018-07-16 stsp err(1, "-r option");
810 04ca23f4 2018-07-16 stsp break;
811 79109fed 2018-03-27 stsp default:
812 79109fed 2018-03-27 stsp usage();
813 79109fed 2018-03-27 stsp /* NOTREACHED */
814 79109fed 2018-03-27 stsp }
815 79109fed 2018-03-27 stsp }
816 79109fed 2018-03-27 stsp
817 79109fed 2018-03-27 stsp argc -= optind;
818 79109fed 2018-03-27 stsp argv += optind;
819 79109fed 2018-03-27 stsp
820 04ca23f4 2018-07-16 stsp if (argc == 0)
821 04ca23f4 2018-07-16 stsp path = strdup("");
822 04ca23f4 2018-07-16 stsp else if (argc == 1)
823 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
824 04ca23f4 2018-07-16 stsp else
825 6f3d1eb0 2018-03-12 stsp usage_log();
826 04ca23f4 2018-07-16 stsp if (path == NULL)
827 04ca23f4 2018-07-16 stsp return got_error_from_errno();
828 f42b1b34 2018-03-12 stsp
829 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
830 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
831 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
832 04ca23f4 2018-07-16 stsp goto done;
833 04ca23f4 2018-07-16 stsp }
834 cffc0aa4 2019-02-05 stsp
835 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
836 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
837 cffc0aa4 2019-02-05 stsp goto done;
838 cffc0aa4 2019-02-05 stsp error = NULL;
839 cffc0aa4 2019-02-05 stsp
840 cffc0aa4 2019-02-05 stsp repo_path = worktree ?
841 cffc0aa4 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
842 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
843 cffc0aa4 2019-02-05 stsp error = got_error_from_errno();
844 cffc0aa4 2019-02-05 stsp goto done;
845 04ca23f4 2018-07-16 stsp }
846 04ca23f4 2018-07-16 stsp
847 6098196c 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
848 6098196c 2019-01-04 stsp if (error)
849 6098196c 2019-01-04 stsp goto done;
850 6098196c 2019-01-04 stsp
851 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
852 d7d4f210 2018-03-12 stsp if (error != NULL)
853 04ca23f4 2018-07-16 stsp goto done;
854 f42b1b34 2018-03-12 stsp
855 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
856 3235492e 2018-04-01 stsp struct got_reference *head_ref;
857 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
858 3235492e 2018-04-01 stsp if (error != NULL)
859 3235492e 2018-04-01 stsp return error;
860 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
861 3235492e 2018-04-01 stsp got_ref_close(head_ref);
862 3235492e 2018-04-01 stsp if (error != NULL)
863 3235492e 2018-04-01 stsp return error;
864 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
865 3235492e 2018-04-01 stsp } else {
866 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
867 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
868 3235492e 2018-04-01 stsp if (error == NULL) {
869 1caad61b 2019-02-01 stsp int obj_type;
870 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
871 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
872 d1f2edc9 2018-06-13 stsp if (error != NULL)
873 1caad61b 2019-02-01 stsp goto done;
874 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
875 1caad61b 2019-02-01 stsp if (error != NULL)
876 1caad61b 2019-02-01 stsp goto done;
877 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
878 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
879 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
880 1caad61b 2019-02-01 stsp if (error != NULL)
881 1caad61b 2019-02-01 stsp goto done;
882 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
883 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
884 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
885 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
886 1caad61b 2019-02-01 stsp goto done;
887 1caad61b 2019-02-01 stsp }
888 1caad61b 2019-02-01 stsp free(id);
889 1caad61b 2019-02-01 stsp id = got_object_id_dup(
890 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
891 1caad61b 2019-02-01 stsp if (id == NULL)
892 1caad61b 2019-02-01 stsp error = got_error_from_errno();
893 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
894 1caad61b 2019-02-01 stsp if (error)
895 1caad61b 2019-02-01 stsp goto done;
896 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
897 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
898 1caad61b 2019-02-01 stsp goto done;
899 1caad61b 2019-02-01 stsp }
900 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
901 d1f2edc9 2018-06-13 stsp if (error != NULL)
902 1caad61b 2019-02-01 stsp goto done;
903 d1f2edc9 2018-06-13 stsp }
904 15a94983 2018-12-23 stsp if (commit == NULL) {
905 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
906 d1f2edc9 2018-06-13 stsp start_commit);
907 d1f2edc9 2018-06-13 stsp if (error != NULL)
908 d1f2edc9 2018-06-13 stsp return error;
909 3235492e 2018-04-01 stsp }
910 3235492e 2018-04-01 stsp }
911 d7d4f210 2018-03-12 stsp if (error != NULL)
912 04ca23f4 2018-07-16 stsp goto done;
913 04ca23f4 2018-07-16 stsp
914 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
915 04ca23f4 2018-07-16 stsp if (error != NULL)
916 04ca23f4 2018-07-16 stsp goto done;
917 04ca23f4 2018-07-16 stsp if (in_repo_path) {
918 04ca23f4 2018-07-16 stsp free(path);
919 04ca23f4 2018-07-16 stsp path = in_repo_path;
920 04ca23f4 2018-07-16 stsp }
921 199a4027 2019-02-02 stsp
922 199a4027 2019-02-02 stsp SIMPLEQ_INIT(&refs);
923 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
924 199a4027 2019-02-02 stsp if (error)
925 199a4027 2019-02-02 stsp goto done;
926 04ca23f4 2018-07-16 stsp
927 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
928 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
929 04ca23f4 2018-07-16 stsp done:
930 04ca23f4 2018-07-16 stsp free(path);
931 04ca23f4 2018-07-16 stsp free(repo_path);
932 04ca23f4 2018-07-16 stsp free(cwd);
933 f42b1b34 2018-03-12 stsp free(id);
934 cffc0aa4 2019-02-05 stsp if (worktree)
935 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
936 ad242220 2018-09-08 stsp if (repo) {
937 ad242220 2018-09-08 stsp const struct got_error *repo_error;
938 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
939 ad242220 2018-09-08 stsp if (error == NULL)
940 ad242220 2018-09-08 stsp error = repo_error;
941 ad242220 2018-09-08 stsp }
942 8bf5b3c9 2018-03-17 stsp return error;
943 5c860e29 2018-03-12 stsp }
944 5c860e29 2018-03-12 stsp
945 4ed7e80c 2018-05-20 stsp __dead static void
946 3f8b7d6a 2018-04-01 stsp usage_diff(void)
947 3f8b7d6a 2018-04-01 stsp {
948 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
949 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
950 3f8b7d6a 2018-04-01 stsp exit(1);
951 b00d56cd 2018-04-01 stsp }
952 b00d56cd 2018-04-01 stsp
953 b72f483a 2019-02-05 stsp struct print_diff_arg {
954 b72f483a 2019-02-05 stsp struct got_repository *repo;
955 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
956 b72f483a 2019-02-05 stsp int diff_context;
957 3fc0c068 2019-02-10 stsp const char *id_str;
958 3fc0c068 2019-02-10 stsp int header_shown;
959 b72f483a 2019-02-05 stsp };
960 b72f483a 2019-02-05 stsp
961 4ed7e80c 2018-05-20 stsp static const struct got_error *
962 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
963 b72f483a 2019-02-05 stsp struct got_object_id *id)
964 b72f483a 2019-02-05 stsp {
965 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
966 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
967 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
968 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
969 b72f483a 2019-02-05 stsp char *abspath = NULL;
970 b72f483a 2019-02-05 stsp struct stat sb;
971 b72f483a 2019-02-05 stsp
972 276262e8 2019-02-08 stsp if (status != GOT_STATUS_MODIFY)
973 b72f483a 2019-02-05 stsp return NULL;
974 3fc0c068 2019-02-10 stsp
975 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
976 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
977 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
978 3fc0c068 2019-02-10 stsp a->header_shown = 1;
979 3fc0c068 2019-02-10 stsp }
980 b72f483a 2019-02-05 stsp
981 b72f483a 2019-02-05 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
982 b72f483a 2019-02-05 stsp if (err)
983 b72f483a 2019-02-05 stsp goto done;
984 b72f483a 2019-02-05 stsp
985 b72f483a 2019-02-05 stsp if (asprintf(&abspath, "%s/%s",
986 b72f483a 2019-02-05 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
987 b72f483a 2019-02-05 stsp err = got_error_from_errno();
988 b72f483a 2019-02-05 stsp goto done;
989 b72f483a 2019-02-05 stsp }
990 b72f483a 2019-02-05 stsp
991 b72f483a 2019-02-05 stsp f2 = fopen(abspath, "r");
992 b72f483a 2019-02-05 stsp if (f2 == NULL) {
993 b72f483a 2019-02-05 stsp err = got_error_from_errno();
994 b72f483a 2019-02-05 stsp goto done;
995 b72f483a 2019-02-05 stsp }
996 b72f483a 2019-02-05 stsp if (lstat(abspath, &sb) == -1) {
997 b72f483a 2019-02-05 stsp err = got_error_from_errno();
998 b72f483a 2019-02-05 stsp goto done;
999 b72f483a 2019-02-05 stsp }
1000 b72f483a 2019-02-05 stsp
1001 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1002 b72f483a 2019-02-05 stsp stdout);
1003 b72f483a 2019-02-05 stsp done:
1004 b72f483a 2019-02-05 stsp if (blob1)
1005 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1006 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1007 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1008 b72f483a 2019-02-05 stsp free(abspath);
1009 927df6b7 2019-02-10 stsp return err;
1010 927df6b7 2019-02-10 stsp }
1011 927df6b7 2019-02-10 stsp
1012 927df6b7 2019-02-10 stsp static const struct got_error *
1013 927df6b7 2019-02-10 stsp get_status_path(char **status_path, struct got_worktree *worktree,
1014 927df6b7 2019-02-10 stsp const char *arg)
1015 927df6b7 2019-02-10 stsp {
1016 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1017 927df6b7 2019-02-10 stsp char *resolved, *path = NULL;
1018 927df6b7 2019-02-10 stsp size_t len;
1019 927df6b7 2019-02-10 stsp
1020 927df6b7 2019-02-10 stsp *status_path = NULL;
1021 927df6b7 2019-02-10 stsp
1022 927df6b7 2019-02-10 stsp resolved = realpath(arg, NULL);
1023 927df6b7 2019-02-10 stsp if (resolved == NULL)
1024 927df6b7 2019-02-10 stsp return got_error_from_errno();
1025 927df6b7 2019-02-10 stsp
1026 927df6b7 2019-02-10 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1027 927df6b7 2019-02-10 stsp strlen(got_worktree_get_root_path(worktree)))) {
1028 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1029 927df6b7 2019-02-10 stsp goto done;
1030 927df6b7 2019-02-10 stsp }
1031 927df6b7 2019-02-10 stsp
1032 927df6b7 2019-02-10 stsp path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1033 927df6b7 2019-02-10 stsp if (path == NULL) {
1034 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1035 927df6b7 2019-02-10 stsp goto done;
1036 927df6b7 2019-02-10 stsp }
1037 927df6b7 2019-02-10 stsp
1038 927df6b7 2019-02-10 stsp /* XXX status walk can't deal with trailing slash! */
1039 927df6b7 2019-02-10 stsp len = strlen(path);
1040 927df6b7 2019-02-10 stsp while (path[len - 1] == '/') {
1041 927df6b7 2019-02-10 stsp path[len - 1] = '\0';
1042 927df6b7 2019-02-10 stsp len--;
1043 927df6b7 2019-02-10 stsp }
1044 927df6b7 2019-02-10 stsp done:
1045 927df6b7 2019-02-10 stsp free(resolved);
1046 927df6b7 2019-02-10 stsp if (err == NULL)
1047 927df6b7 2019-02-10 stsp *status_path = path;
1048 927df6b7 2019-02-10 stsp else
1049 927df6b7 2019-02-10 stsp free(path);
1050 b72f483a 2019-02-05 stsp return err;
1051 b72f483a 2019-02-05 stsp }
1052 b72f483a 2019-02-05 stsp
1053 b72f483a 2019-02-05 stsp static const struct got_error *
1054 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1055 b00d56cd 2018-04-01 stsp {
1056 b00d56cd 2018-04-01 stsp const struct got_error *error;
1057 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1058 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1059 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1060 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1061 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1062 15a94983 2018-12-23 stsp int type1, type2;
1063 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1064 c0cc5c62 2018-10-18 stsp const char *errstr;
1065 927df6b7 2019-02-10 stsp char *path = NULL;
1066 b00d56cd 2018-04-01 stsp
1067 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1068 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1069 25eccc22 2019-01-04 stsp NULL) == -1)
1070 b00d56cd 2018-04-01 stsp err(1, "pledge");
1071 b00d56cd 2018-04-01 stsp #endif
1072 b00d56cd 2018-04-01 stsp
1073 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1074 b00d56cd 2018-04-01 stsp switch (ch) {
1075 c0cc5c62 2018-10-18 stsp case 'C':
1076 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1077 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1078 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1079 c0cc5c62 2018-10-18 stsp break;
1080 b72f483a 2019-02-05 stsp case 'r':
1081 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1082 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1083 b72f483a 2019-02-05 stsp err(1, "-r option");
1084 b72f483a 2019-02-05 stsp break;
1085 b00d56cd 2018-04-01 stsp default:
1086 b00d56cd 2018-04-01 stsp usage();
1087 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1088 b00d56cd 2018-04-01 stsp }
1089 b00d56cd 2018-04-01 stsp }
1090 b00d56cd 2018-04-01 stsp
1091 b00d56cd 2018-04-01 stsp argc -= optind;
1092 b00d56cd 2018-04-01 stsp argv += optind;
1093 b00d56cd 2018-04-01 stsp
1094 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1095 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1096 b72f483a 2019-02-05 stsp error = got_error_from_errno();
1097 b72f483a 2019-02-05 stsp goto done;
1098 b72f483a 2019-02-05 stsp }
1099 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1100 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1101 927df6b7 2019-02-10 stsp goto done;
1102 927df6b7 2019-02-10 stsp if (argc <= 1) {
1103 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1104 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1105 927df6b7 2019-02-10 stsp goto done;
1106 927df6b7 2019-02-10 stsp }
1107 b72f483a 2019-02-05 stsp if (repo_path)
1108 b72f483a 2019-02-05 stsp errx(1,
1109 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1110 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1111 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1112 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1113 927df6b7 2019-02-10 stsp goto done;
1114 927df6b7 2019-02-10 stsp }
1115 927df6b7 2019-02-10 stsp if (argc == 1) {
1116 927df6b7 2019-02-10 stsp error = get_status_path(&path, worktree, argv[0]);
1117 927df6b7 2019-02-10 stsp if (error)
1118 927df6b7 2019-02-10 stsp goto done;
1119 927df6b7 2019-02-10 stsp } else {
1120 927df6b7 2019-02-10 stsp path = strdup("");
1121 927df6b7 2019-02-10 stsp if (path == NULL) {
1122 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1123 927df6b7 2019-02-10 stsp goto done;
1124 927df6b7 2019-02-10 stsp }
1125 927df6b7 2019-02-10 stsp }
1126 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1127 15a94983 2018-12-23 stsp id_str1 = argv[0];
1128 15a94983 2018-12-23 stsp id_str2 = argv[1];
1129 b00d56cd 2018-04-01 stsp } else
1130 b00d56cd 2018-04-01 stsp usage_diff();
1131 25eccc22 2019-01-04 stsp
1132 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1133 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1134 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1135 b72f483a 2019-02-05 stsp return got_error_from_errno();
1136 b72f483a 2019-02-05 stsp }
1137 b72f483a 2019-02-05 stsp
1138 b72f483a 2019-02-05 stsp error = apply_unveil(repo_path,
1139 b72f483a 2019-02-05 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1140 25eccc22 2019-01-04 stsp if (error)
1141 25eccc22 2019-01-04 stsp goto done;
1142 b00d56cd 2018-04-01 stsp
1143 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1144 76089277 2018-04-01 stsp free(repo_path);
1145 b00d56cd 2018-04-01 stsp if (error != NULL)
1146 b00d56cd 2018-04-01 stsp goto done;
1147 b00d56cd 2018-04-01 stsp
1148 b72f483a 2019-02-05 stsp if (worktree) {
1149 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1150 b72f483a 2019-02-05 stsp char *id_str;
1151 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1152 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1153 b72f483a 2019-02-05 stsp if (error)
1154 b72f483a 2019-02-05 stsp goto done;
1155 b72f483a 2019-02-05 stsp arg.repo = repo;
1156 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1157 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1158 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1159 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1160 b72f483a 2019-02-05 stsp
1161 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1162 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1163 b72f483a 2019-02-05 stsp free(id_str);
1164 b72f483a 2019-02-05 stsp goto done;
1165 b72f483a 2019-02-05 stsp }
1166 b72f483a 2019-02-05 stsp
1167 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1168 6402fb3c 2018-09-15 stsp if (error)
1169 b00d56cd 2018-04-01 stsp goto done;
1170 b00d56cd 2018-04-01 stsp
1171 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1172 6402fb3c 2018-09-15 stsp if (error)
1173 b00d56cd 2018-04-01 stsp goto done;
1174 b00d56cd 2018-04-01 stsp
1175 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1176 15a94983 2018-12-23 stsp if (error)
1177 15a94983 2018-12-23 stsp goto done;
1178 15a94983 2018-12-23 stsp
1179 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1180 15a94983 2018-12-23 stsp if (error)
1181 15a94983 2018-12-23 stsp goto done;
1182 15a94983 2018-12-23 stsp
1183 15a94983 2018-12-23 stsp if (type1 != type2) {
1184 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1185 b00d56cd 2018-04-01 stsp goto done;
1186 b00d56cd 2018-04-01 stsp }
1187 b00d56cd 2018-04-01 stsp
1188 15a94983 2018-12-23 stsp switch (type1) {
1189 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1190 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1191 54156555 2018-12-24 stsp diff_context, repo, stdout);
1192 b00d56cd 2018-04-01 stsp break;
1193 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1194 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1195 54156555 2018-12-24 stsp diff_context, repo, stdout);
1196 b00d56cd 2018-04-01 stsp break;
1197 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1198 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1199 15a94983 2018-12-23 stsp id_str2);
1200 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1201 c0cc5c62 2018-10-18 stsp repo, stdout);
1202 b00d56cd 2018-04-01 stsp break;
1203 b00d56cd 2018-04-01 stsp default:
1204 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1205 b00d56cd 2018-04-01 stsp }
1206 b00d56cd 2018-04-01 stsp
1207 b00d56cd 2018-04-01 stsp done:
1208 15a94983 2018-12-23 stsp free(id1);
1209 15a94983 2018-12-23 stsp free(id2);
1210 927df6b7 2019-02-10 stsp free(path);
1211 b72f483a 2019-02-05 stsp if (worktree)
1212 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1213 ad242220 2018-09-08 stsp if (repo) {
1214 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1215 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1216 ad242220 2018-09-08 stsp if (error == NULL)
1217 ad242220 2018-09-08 stsp error = repo_error;
1218 ad242220 2018-09-08 stsp }
1219 b00d56cd 2018-04-01 stsp return error;
1220 404c43c4 2018-06-21 stsp }
1221 404c43c4 2018-06-21 stsp
1222 404c43c4 2018-06-21 stsp __dead static void
1223 404c43c4 2018-06-21 stsp usage_blame(void)
1224 404c43c4 2018-06-21 stsp {
1225 9270e621 2019-02-05 stsp fprintf(stderr,
1226 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1227 404c43c4 2018-06-21 stsp getprogname());
1228 404c43c4 2018-06-21 stsp exit(1);
1229 b00d56cd 2018-04-01 stsp }
1230 b00d56cd 2018-04-01 stsp
1231 404c43c4 2018-06-21 stsp static const struct got_error *
1232 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1233 404c43c4 2018-06-21 stsp {
1234 404c43c4 2018-06-21 stsp const struct got_error *error;
1235 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1236 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1237 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1238 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1239 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1240 404c43c4 2018-06-21 stsp int ch;
1241 404c43c4 2018-06-21 stsp
1242 404c43c4 2018-06-21 stsp #ifndef PROFILE
1243 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1244 36e2fb66 2019-01-04 stsp NULL) == -1)
1245 404c43c4 2018-06-21 stsp err(1, "pledge");
1246 404c43c4 2018-06-21 stsp #endif
1247 404c43c4 2018-06-21 stsp
1248 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1249 404c43c4 2018-06-21 stsp switch (ch) {
1250 404c43c4 2018-06-21 stsp case 'c':
1251 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1252 404c43c4 2018-06-21 stsp break;
1253 66bea077 2018-08-02 stsp case 'r':
1254 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1255 66bea077 2018-08-02 stsp if (repo_path == NULL)
1256 66bea077 2018-08-02 stsp err(1, "-r option");
1257 66bea077 2018-08-02 stsp break;
1258 404c43c4 2018-06-21 stsp default:
1259 404c43c4 2018-06-21 stsp usage();
1260 404c43c4 2018-06-21 stsp /* NOTREACHED */
1261 404c43c4 2018-06-21 stsp }
1262 404c43c4 2018-06-21 stsp }
1263 404c43c4 2018-06-21 stsp
1264 404c43c4 2018-06-21 stsp argc -= optind;
1265 404c43c4 2018-06-21 stsp argv += optind;
1266 404c43c4 2018-06-21 stsp
1267 a39318fd 2018-08-02 stsp if (argc == 1)
1268 404c43c4 2018-06-21 stsp path = argv[0];
1269 a39318fd 2018-08-02 stsp else
1270 404c43c4 2018-06-21 stsp usage_blame();
1271 404c43c4 2018-06-21 stsp
1272 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1273 66bea077 2018-08-02 stsp if (cwd == NULL) {
1274 66bea077 2018-08-02 stsp error = got_error_from_errno();
1275 66bea077 2018-08-02 stsp goto done;
1276 66bea077 2018-08-02 stsp }
1277 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1278 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1279 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1280 66bea077 2018-08-02 stsp goto done;
1281 0c06baac 2019-02-05 stsp else
1282 0c06baac 2019-02-05 stsp error = NULL;
1283 0c06baac 2019-02-05 stsp if (worktree) {
1284 0c06baac 2019-02-05 stsp repo_path =
1285 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1286 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1287 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1288 0c06baac 2019-02-05 stsp if (error)
1289 0c06baac 2019-02-05 stsp goto done;
1290 0c06baac 2019-02-05 stsp } else {
1291 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1292 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1293 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1294 0c06baac 2019-02-05 stsp goto done;
1295 0c06baac 2019-02-05 stsp }
1296 66bea077 2018-08-02 stsp }
1297 66bea077 2018-08-02 stsp }
1298 36e2fb66 2019-01-04 stsp
1299 36e2fb66 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1300 36e2fb66 2019-01-04 stsp if (error)
1301 36e2fb66 2019-01-04 stsp goto done;
1302 66bea077 2018-08-02 stsp
1303 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
1304 404c43c4 2018-06-21 stsp if (error != NULL)
1305 404c43c4 2018-06-21 stsp goto done;
1306 404c43c4 2018-06-21 stsp
1307 0c06baac 2019-02-05 stsp if (worktree) {
1308 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1309 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1310 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1311 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1312 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1313 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1314 6efaaa2d 2019-02-05 stsp path) == -1) {
1315 0c06baac 2019-02-05 stsp error = got_error_from_errno();
1316 0c06baac 2019-02-05 stsp goto done;
1317 0c06baac 2019-02-05 stsp }
1318 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1319 0c06baac 2019-02-05 stsp free(p);
1320 0c06baac 2019-02-05 stsp } else {
1321 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1322 0c06baac 2019-02-05 stsp }
1323 0c06baac 2019-02-05 stsp if (error)
1324 66bea077 2018-08-02 stsp goto done;
1325 66bea077 2018-08-02 stsp
1326 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1327 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1328 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1329 404c43c4 2018-06-21 stsp if (error != NULL)
1330 66bea077 2018-08-02 stsp goto done;
1331 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1332 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1333 404c43c4 2018-06-21 stsp if (error != NULL)
1334 66bea077 2018-08-02 stsp goto done;
1335 404c43c4 2018-06-21 stsp } else {
1336 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1337 15a94983 2018-12-23 stsp commit_id_str);
1338 404c43c4 2018-06-21 stsp if (error != NULL)
1339 66bea077 2018-08-02 stsp goto done;
1340 404c43c4 2018-06-21 stsp }
1341 404c43c4 2018-06-21 stsp
1342 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1343 404c43c4 2018-06-21 stsp done:
1344 66bea077 2018-08-02 stsp free(in_repo_path);
1345 66bea077 2018-08-02 stsp free(repo_path);
1346 66bea077 2018-08-02 stsp free(cwd);
1347 404c43c4 2018-06-21 stsp free(commit_id);
1348 0c06baac 2019-02-05 stsp if (worktree)
1349 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1350 ad242220 2018-09-08 stsp if (repo) {
1351 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1352 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1353 ad242220 2018-09-08 stsp if (error == NULL)
1354 ad242220 2018-09-08 stsp error = repo_error;
1355 ad242220 2018-09-08 stsp }
1356 404c43c4 2018-06-21 stsp return error;
1357 5de5890b 2018-10-18 stsp }
1358 5de5890b 2018-10-18 stsp
1359 5de5890b 2018-10-18 stsp __dead static void
1360 5de5890b 2018-10-18 stsp usage_tree(void)
1361 5de5890b 2018-10-18 stsp {
1362 c1669e2e 2019-01-09 stsp fprintf(stderr,
1363 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1364 5de5890b 2018-10-18 stsp getprogname());
1365 5de5890b 2018-10-18 stsp exit(1);
1366 5de5890b 2018-10-18 stsp }
1367 5de5890b 2018-10-18 stsp
1368 c1669e2e 2019-01-09 stsp static void
1369 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1370 c1669e2e 2019-01-09 stsp const char *root_path)
1371 c1669e2e 2019-01-09 stsp {
1372 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1373 5de5890b 2018-10-18 stsp
1374 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1375 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1376 c1669e2e 2019-01-09 stsp path++;
1377 c1669e2e 2019-01-09 stsp
1378 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1379 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1380 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1381 c1669e2e 2019-01-09 stsp }
1382 c1669e2e 2019-01-09 stsp
1383 5de5890b 2018-10-18 stsp static const struct got_error *
1384 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1385 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1386 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1387 5de5890b 2018-10-18 stsp {
1388 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1389 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1390 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1391 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1392 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1393 5de5890b 2018-10-18 stsp
1394 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1395 5de5890b 2018-10-18 stsp if (err)
1396 5de5890b 2018-10-18 stsp goto done;
1397 5de5890b 2018-10-18 stsp
1398 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1399 5de5890b 2018-10-18 stsp if (err)
1400 5de5890b 2018-10-18 stsp goto done;
1401 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1402 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1403 5de5890b 2018-10-18 stsp while (te) {
1404 5de5890b 2018-10-18 stsp char *id = NULL;
1405 84453469 2018-11-11 stsp
1406 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1407 84453469 2018-11-11 stsp break;
1408 84453469 2018-11-11 stsp
1409 5de5890b 2018-10-18 stsp if (show_ids) {
1410 5de5890b 2018-10-18 stsp char *id_str;
1411 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1412 5de5890b 2018-10-18 stsp if (err)
1413 5de5890b 2018-10-18 stsp goto done;
1414 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1415 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1416 5de5890b 2018-10-18 stsp free(id_str);
1417 5de5890b 2018-10-18 stsp goto done;
1418 5de5890b 2018-10-18 stsp }
1419 5de5890b 2018-10-18 stsp free(id_str);
1420 5de5890b 2018-10-18 stsp }
1421 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1422 5de5890b 2018-10-18 stsp free(id);
1423 c1669e2e 2019-01-09 stsp
1424 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1425 c1669e2e 2019-01-09 stsp char *child_path;
1426 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1427 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1428 c1669e2e 2019-01-09 stsp te->name) == -1) {
1429 c1669e2e 2019-01-09 stsp err = got_error_from_errno();
1430 c1669e2e 2019-01-09 stsp goto done;
1431 c1669e2e 2019-01-09 stsp }
1432 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1433 c1669e2e 2019-01-09 stsp root_path, repo);
1434 c1669e2e 2019-01-09 stsp free(child_path);
1435 c1669e2e 2019-01-09 stsp if (err)
1436 c1669e2e 2019-01-09 stsp goto done;
1437 c1669e2e 2019-01-09 stsp }
1438 c1669e2e 2019-01-09 stsp
1439 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1440 5de5890b 2018-10-18 stsp }
1441 5de5890b 2018-10-18 stsp done:
1442 5de5890b 2018-10-18 stsp if (tree)
1443 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1444 5de5890b 2018-10-18 stsp free(tree_id);
1445 5de5890b 2018-10-18 stsp return err;
1446 404c43c4 2018-06-21 stsp }
1447 404c43c4 2018-06-21 stsp
1448 5de5890b 2018-10-18 stsp static const struct got_error *
1449 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1450 5de5890b 2018-10-18 stsp {
1451 5de5890b 2018-10-18 stsp const struct got_error *error;
1452 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1453 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1454 7a2c19d6 2019-02-05 stsp const char *path;
1455 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1456 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1457 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1458 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1459 5de5890b 2018-10-18 stsp int ch;
1460 5de5890b 2018-10-18 stsp
1461 5de5890b 2018-10-18 stsp #ifndef PROFILE
1462 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1463 0f8d269b 2019-01-04 stsp NULL) == -1)
1464 5de5890b 2018-10-18 stsp err(1, "pledge");
1465 5de5890b 2018-10-18 stsp #endif
1466 5de5890b 2018-10-18 stsp
1467 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1468 5de5890b 2018-10-18 stsp switch (ch) {
1469 5de5890b 2018-10-18 stsp case 'c':
1470 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1471 5de5890b 2018-10-18 stsp break;
1472 5de5890b 2018-10-18 stsp case 'r':
1473 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1474 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1475 5de5890b 2018-10-18 stsp err(1, "-r option");
1476 5de5890b 2018-10-18 stsp break;
1477 5de5890b 2018-10-18 stsp case 'i':
1478 5de5890b 2018-10-18 stsp show_ids = 1;
1479 5de5890b 2018-10-18 stsp break;
1480 c1669e2e 2019-01-09 stsp case 'R':
1481 c1669e2e 2019-01-09 stsp recurse = 1;
1482 c1669e2e 2019-01-09 stsp break;
1483 5de5890b 2018-10-18 stsp default:
1484 5de5890b 2018-10-18 stsp usage();
1485 5de5890b 2018-10-18 stsp /* NOTREACHED */
1486 5de5890b 2018-10-18 stsp }
1487 5de5890b 2018-10-18 stsp }
1488 5de5890b 2018-10-18 stsp
1489 5de5890b 2018-10-18 stsp argc -= optind;
1490 5de5890b 2018-10-18 stsp argv += optind;
1491 5de5890b 2018-10-18 stsp
1492 5de5890b 2018-10-18 stsp if (argc == 1)
1493 5de5890b 2018-10-18 stsp path = argv[0];
1494 5de5890b 2018-10-18 stsp else if (argc > 1)
1495 5de5890b 2018-10-18 stsp usage_tree();
1496 5de5890b 2018-10-18 stsp else
1497 7a2c19d6 2019-02-05 stsp path = NULL;
1498 7a2c19d6 2019-02-05 stsp
1499 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1500 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1501 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1502 9bf7a39b 2019-02-05 stsp goto done;
1503 9bf7a39b 2019-02-05 stsp }
1504 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1505 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1506 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1507 7a2c19d6 2019-02-05 stsp goto done;
1508 7a2c19d6 2019-02-05 stsp else
1509 7a2c19d6 2019-02-05 stsp error = NULL;
1510 7a2c19d6 2019-02-05 stsp if (worktree) {
1511 7a2c19d6 2019-02-05 stsp repo_path =
1512 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1513 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1514 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1515 7a2c19d6 2019-02-05 stsp if (error)
1516 7a2c19d6 2019-02-05 stsp goto done;
1517 7a2c19d6 2019-02-05 stsp } else {
1518 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1519 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1520 7a2c19d6 2019-02-05 stsp error = got_error_from_errno();
1521 7a2c19d6 2019-02-05 stsp goto done;
1522 7a2c19d6 2019-02-05 stsp }
1523 5de5890b 2018-10-18 stsp }
1524 5de5890b 2018-10-18 stsp }
1525 5de5890b 2018-10-18 stsp
1526 0f8d269b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1527 0f8d269b 2019-01-04 stsp if (error)
1528 0f8d269b 2019-01-04 stsp goto done;
1529 0f8d269b 2019-01-04 stsp
1530 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1531 5de5890b 2018-10-18 stsp if (error != NULL)
1532 5de5890b 2018-10-18 stsp goto done;
1533 5de5890b 2018-10-18 stsp
1534 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1535 9bf7a39b 2019-02-05 stsp if (worktree) {
1536 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1537 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1538 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1539 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1540 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1541 9bf7a39b 2019-02-05 stsp error = got_error_from_errno();
1542 9bf7a39b 2019-02-05 stsp goto done;
1543 9bf7a39b 2019-02-05 stsp }
1544 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1545 9bf7a39b 2019-02-05 stsp free(p);
1546 9bf7a39b 2019-02-05 stsp if (error)
1547 9bf7a39b 2019-02-05 stsp goto done;
1548 9bf7a39b 2019-02-05 stsp } else
1549 9bf7a39b 2019-02-05 stsp path = "/";
1550 9bf7a39b 2019-02-05 stsp }
1551 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1552 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1553 9bf7a39b 2019-02-05 stsp if (error != NULL)
1554 9bf7a39b 2019-02-05 stsp goto done;
1555 9bf7a39b 2019-02-05 stsp }
1556 5de5890b 2018-10-18 stsp
1557 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1558 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1559 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1560 5de5890b 2018-10-18 stsp if (error != NULL)
1561 5de5890b 2018-10-18 stsp goto done;
1562 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1563 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1564 5de5890b 2018-10-18 stsp if (error != NULL)
1565 5de5890b 2018-10-18 stsp goto done;
1566 5de5890b 2018-10-18 stsp } else {
1567 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1568 15a94983 2018-12-23 stsp commit_id_str);
1569 5de5890b 2018-10-18 stsp if (error != NULL)
1570 5de5890b 2018-10-18 stsp goto done;
1571 5de5890b 2018-10-18 stsp }
1572 5de5890b 2018-10-18 stsp
1573 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1574 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1575 5de5890b 2018-10-18 stsp done:
1576 5de5890b 2018-10-18 stsp free(in_repo_path);
1577 5de5890b 2018-10-18 stsp free(repo_path);
1578 5de5890b 2018-10-18 stsp free(cwd);
1579 5de5890b 2018-10-18 stsp free(commit_id);
1580 7a2c19d6 2019-02-05 stsp if (worktree)
1581 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1582 5de5890b 2018-10-18 stsp if (repo) {
1583 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1584 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1585 5de5890b 2018-10-18 stsp if (error == NULL)
1586 5de5890b 2018-10-18 stsp error = repo_error;
1587 5de5890b 2018-10-18 stsp }
1588 5de5890b 2018-10-18 stsp return error;
1589 5de5890b 2018-10-18 stsp }
1590 5de5890b 2018-10-18 stsp
1591 6bad629b 2019-02-04 stsp __dead static void
1592 6bad629b 2019-02-04 stsp usage_status(void)
1593 6bad629b 2019-02-04 stsp {
1594 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1595 6bad629b 2019-02-04 stsp exit(1);
1596 6bad629b 2019-02-04 stsp }
1597 5c860e29 2018-03-12 stsp
1598 b72f483a 2019-02-05 stsp static const struct got_error *
1599 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1600 b72f483a 2019-02-05 stsp struct got_object_id *id)
1601 6bad629b 2019-02-04 stsp {
1602 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1603 b72f483a 2019-02-05 stsp return NULL;
1604 6bad629b 2019-02-04 stsp }
1605 5c860e29 2018-03-12 stsp
1606 6bad629b 2019-02-04 stsp static const struct got_error *
1607 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1608 6bad629b 2019-02-04 stsp {
1609 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1610 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1611 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1612 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1613 6bad629b 2019-02-04 stsp int ch;
1614 5c860e29 2018-03-12 stsp
1615 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1616 6bad629b 2019-02-04 stsp switch (ch) {
1617 5c860e29 2018-03-12 stsp default:
1618 6bad629b 2019-02-04 stsp usage();
1619 6bad629b 2019-02-04 stsp /* NOTREACHED */
1620 5c860e29 2018-03-12 stsp }
1621 5c860e29 2018-03-12 stsp }
1622 5c860e29 2018-03-12 stsp
1623 6bad629b 2019-02-04 stsp argc -= optind;
1624 6bad629b 2019-02-04 stsp argv += optind;
1625 5c860e29 2018-03-12 stsp
1626 6bad629b 2019-02-04 stsp #ifndef PROFILE
1627 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1628 6bad629b 2019-02-04 stsp NULL) == -1)
1629 6bad629b 2019-02-04 stsp err(1, "pledge");
1630 f42b1b34 2018-03-12 stsp #endif
1631 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1632 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1633 927df6b7 2019-02-10 stsp error = got_error_from_errno();
1634 927df6b7 2019-02-10 stsp goto done;
1635 927df6b7 2019-02-10 stsp }
1636 927df6b7 2019-02-10 stsp
1637 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1638 927df6b7 2019-02-10 stsp if (error != NULL)
1639 927df6b7 2019-02-10 stsp goto done;
1640 927df6b7 2019-02-10 stsp
1641 6bad629b 2019-02-04 stsp if (argc == 0) {
1642 927df6b7 2019-02-10 stsp path = strdup("");
1643 927df6b7 2019-02-10 stsp if (path == NULL) {
1644 6bad629b 2019-02-04 stsp error = got_error_from_errno();
1645 6bad629b 2019-02-04 stsp goto done;
1646 6bad629b 2019-02-04 stsp }
1647 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1648 927df6b7 2019-02-10 stsp error = get_status_path(&path, worktree, argv[0]);
1649 927df6b7 2019-02-10 stsp if (error)
1650 6bad629b 2019-02-04 stsp goto done;
1651 6bad629b 2019-02-04 stsp } else
1652 6bad629b 2019-02-04 stsp usage_status();
1653 6bad629b 2019-02-04 stsp
1654 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1655 6bad629b 2019-02-04 stsp if (error != NULL)
1656 6bad629b 2019-02-04 stsp goto done;
1657 6bad629b 2019-02-04 stsp
1658 c7f4312f 2019-02-05 stsp error = apply_unveil(got_repo_get_path(repo),
1659 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(worktree));
1660 6bad629b 2019-02-04 stsp if (error)
1661 6bad629b 2019-02-04 stsp goto done;
1662 6bad629b 2019-02-04 stsp
1663 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1664 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1665 6bad629b 2019-02-04 stsp done:
1666 927df6b7 2019-02-10 stsp free(cwd);
1667 927df6b7 2019-02-10 stsp free(path);
1668 6bad629b 2019-02-04 stsp return error;
1669 6bad629b 2019-02-04 stsp }