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