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