Blame


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