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