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