Blame


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