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