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