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>
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.
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.
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>
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>
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"
48 5c860e29 2018-03-12 stsp #ifndef nitems
49 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
53 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
56 99437157 2018-11-11 stsp catch_sigint(int signo)
58 99437157 2018-11-11 stsp sigint_received = 1;
62 99437157 2018-11-11 stsp catch_sigpipe(int signo)
64 99437157 2018-11-11 stsp sigpipe_received = 1;
68 5c860e29 2018-03-12 stsp struct cmd {
69 5c860e29 2018-03-12 stsp const char *cmd_name;
70 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
71 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
72 46a0db7d 2018-03-12 stsp const char *cmd_descr;
75 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
77 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
80 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
81 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
82 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
83 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
84 d00136be 2019-03-26 stsp __dead static void usage_add(void);
85 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
86 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
87 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
89 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
90 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
94 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
96 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
97 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
98 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
99 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
100 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
102 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
103 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
104 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
105 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
106 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
107 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
108 1b6b95a8 2018-03-12 stsp "show repository history" },
109 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
110 b00d56cd 2018-04-01 stsp "compare files and directories" },
111 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
112 a67e2392 2019-03-26 stsp "show when lines in a file were changed" },
113 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
114 a67e2392 2019-03-26 stsp "list files and directories in repository" },
115 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
116 1b6b95a8 2018-03-12 stsp "show modification status of files" },
117 d0eebce4 2019-03-11 stsp { "ref", cmd_ref, usage_ref,
118 d0eebce4 2019-03-11 stsp "manage references in repository" },
119 d00136be 2019-03-26 stsp { "add", cmd_add, usage_add,
120 bd14628f 2019-05-12 stsp "add new files to version control" },
121 2ec1f75b 2019-03-26 stsp { "rm", cmd_rm, usage_rm,
122 2ec1f75b 2019-03-26 stsp "remove a versioned file" },
123 a129376b 2019-03-28 stsp { "revert", cmd_revert, usage_revert,
124 a129376b 2019-03-28 stsp "revert uncommitted changes" },
125 c4296144 2019-05-09 stsp { "commit", cmd_commit, usage_commit,
126 5501382f 2019-05-10 stsp "write changes from work tree to repository" },
130 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
132 5c860e29 2018-03-12 stsp struct cmd *cmd;
133 5c860e29 2018-03-12 stsp unsigned int i;
135 1b6b95a8 2018-03-12 stsp int hflag = 0;
137 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
139 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
140 5c860e29 2018-03-12 stsp switch (ch) {
146 5c860e29 2018-03-12 stsp /* NOTREACHED */
150 5c860e29 2018-03-12 stsp argc -= optind;
151 5c860e29 2018-03-12 stsp argv += optind;
152 1e70621d 2018-03-27 stsp optind = 0;
154 5c860e29 2018-03-12 stsp if (argc <= 0)
157 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
158 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
160 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
161 d7d4f210 2018-03-12 stsp const struct got_error *error;
163 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
165 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
169 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
171 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
172 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
173 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
180 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
184 4ed7e80c 2018-05-20 stsp __dead static void
185 5c860e29 2018-03-12 stsp usage(void)
189 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
190 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
191 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
192 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
193 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
198 0266afb7 2019-01-04 stsp static const struct got_error *
199 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
200 a0937847 2019-05-11 stsp const char *worktree_path, int create_worktree)
202 0266afb7 2019-01-04 stsp const struct got_error *error;
203 3c45a30a 2019-05-12 jcs static char err_msg[MAXPATHLEN + 36];
205 a0937847 2019-05-11 stsp if (create_worktree) {
206 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
207 a0937847 2019-05-11 stsp error = got_path_mkdir(worktree_path);
209 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
210 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
212 3c45a30a 2019-05-12 jcs error = NULL;
214 3c45a30a 2019-05-12 jcs snprintf(err_msg, sizeof(err_msg),
215 3c45a30a 2019-05-12 jcs "%s: directory exists but is not empty",
216 3c45a30a 2019-05-12 jcs worktree_path);
217 3c45a30a 2019-05-12 jcs error = got_error_msg(GOT_ERR_BAD_PATH,
222 a0937847 2019-05-11 stsp if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
223 a0937847 2019-05-11 stsp return error;
226 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
227 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", repo_path);
229 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
230 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", worktree_path);
232 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
233 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", "/tmp");
235 0266afb7 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
236 0266afb7 2019-01-04 stsp if (error != NULL)
237 0266afb7 2019-01-04 stsp return error;
239 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
240 230a42bd 2019-05-11 jcs return got_error_prefix_errno("unveil");
242 0266afb7 2019-01-04 stsp return NULL;
245 4ed7e80c 2018-05-20 stsp __dead static void
246 c09a553d 2018-03-12 stsp usage_checkout(void)
248 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
249 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
253 92a684f4 2018-03-12 stsp static void
254 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
256 92a684f4 2018-03-12 stsp char *worktree_path = arg;
258 92a684f4 2018-03-12 stsp while (path[0] == '/')
261 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
264 99437157 2018-11-11 stsp static const struct got_error *
265 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
267 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
268 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
269 99437157 2018-11-11 stsp return NULL;
272 8069f636 2019-01-12 stsp static const struct got_error *
273 8069f636 2019-01-12 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
274 8069f636 2019-01-12 stsp struct got_repository *repo)
276 8069f636 2019-01-12 stsp const struct got_error *err;
277 8069f636 2019-01-12 stsp struct got_reference *head_ref = NULL;
278 8069f636 2019-01-12 stsp struct got_object_id *head_commit_id = NULL;
279 8069f636 2019-01-12 stsp struct got_commit_graph *graph = NULL;
281 36a38700 2019-05-10 stsp err = got_ref_open(&head_ref, repo,
282 2f17228e 2019-05-12 stsp got_worktree_get_head_ref_name(worktree), 0);
284 36a38700 2019-05-10 stsp return err;
286 8069f636 2019-01-12 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
287 8069f636 2019-01-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
291 8069f636 2019-01-12 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
295 8069f636 2019-01-12 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
299 8069f636 2019-01-12 stsp struct got_object_id *id;
301 8069f636 2019-01-12 stsp if (sigint_received || sigpipe_received)
304 8069f636 2019-01-12 stsp err = got_commit_graph_iter_next(&id, graph);
306 8069f636 2019-01-12 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
307 8069f636 2019-01-12 stsp err = got_error(GOT_ERR_ANCESTRY);
310 8069f636 2019-01-12 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
312 8069f636 2019-01-12 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
318 8069f636 2019-01-12 stsp if (id == NULL)
320 8069f636 2019-01-12 stsp if (got_object_id_cmp(id, commit_id) == 0)
324 8069f636 2019-01-12 stsp if (head_ref)
325 8069f636 2019-01-12 stsp got_ref_close(head_ref);
327 8069f636 2019-01-12 stsp got_commit_graph_close(graph);
328 8069f636 2019-01-12 stsp return err;
332 4ed7e80c 2018-05-20 stsp static const struct got_error *
333 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
335 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
336 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
337 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
338 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
339 c09a553d 2018-03-12 stsp char *repo_path = NULL;
340 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
341 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
342 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
343 72151b04 2019-05-11 stsp int ch, same_path_prefix;
345 8069f636 2019-01-12 stsp while ((ch = getopt(argc, argv, "c:p:")) != -1) {
346 0bb8a95e 2018-03-12 stsp switch (ch) {
348 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
349 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
350 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
353 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
356 2deda0b9 2019-03-07 stsp usage_checkout();
357 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
361 0bb8a95e 2018-03-12 stsp argc -= optind;
362 0bb8a95e 2018-03-12 stsp argv += optind;
364 6715a751 2018-03-16 stsp #ifndef PROFILE
365 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
366 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
367 c09a553d 2018-03-12 stsp err(1, "pledge");
369 0bb8a95e 2018-03-12 stsp if (argc == 1) {
370 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
371 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
372 76089277 2018-04-01 stsp if (repo_path == NULL)
373 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
374 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
375 76089277 2018-04-01 stsp if (cwd == NULL) {
376 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
379 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
380 230a42bd 2019-05-11 jcs base = basename(path_prefix);
381 230a42bd 2019-05-11 jcs if (base == NULL) {
382 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
383 230a42bd 2019-05-11 jcs path_prefix);
387 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
388 230a42bd 2019-05-11 jcs if (base == NULL) {
389 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("basename",
394 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
395 c09a553d 2018-03-12 stsp if (dotgit)
396 c09a553d 2018-03-12 stsp *dotgit = '\0';
397 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
398 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
403 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
404 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
405 76089277 2018-04-01 stsp if (repo_path == NULL) {
406 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
409 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
410 76089277 2018-04-01 stsp if (worktree_path == NULL) {
411 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[1]);
415 c09a553d 2018-03-12 stsp usage_checkout();
417 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
418 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
420 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
421 c09a553d 2018-03-12 stsp if (error != NULL)
424 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
428 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
429 c09a553d 2018-03-12 stsp if (error != NULL)
432 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
433 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
436 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
437 c09a553d 2018-03-12 stsp if (error != NULL)
440 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
441 e5dc7198 2018-12-29 stsp path_prefix);
442 e5dc7198 2018-12-29 stsp if (error != NULL)
444 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
445 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
449 8069f636 2019-01-12 stsp if (commit_id_str) {
450 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
451 8069f636 2019-01-12 stsp error = got_object_resolve_id_str(&commit_id, repo,
452 8069f636 2019-01-12 stsp commit_id_str);
453 8069f636 2019-01-12 stsp if (error != NULL)
455 8069f636 2019-01-12 stsp error = check_ancestry(worktree, commit_id, repo);
456 8069f636 2019-01-12 stsp if (error != NULL) {
457 8069f636 2019-01-12 stsp free(commit_id);
460 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
461 8069f636 2019-01-12 stsp commit_id);
462 8069f636 2019-01-12 stsp free(commit_id);
467 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
468 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
469 c09a553d 2018-03-12 stsp if (error != NULL)
472 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
475 8069f636 2019-01-12 stsp free(commit_id_str);
476 76089277 2018-04-01 stsp free(repo_path);
477 507dc3bb 2018-12-29 stsp free(worktree_path);
478 507dc3bb 2018-12-29 stsp return error;
481 507dc3bb 2018-12-29 stsp __dead static void
482 507dc3bb 2018-12-29 stsp usage_update(void)
484 c4cdcb68 2019-04-03 stsp fprintf(stderr, "usage: %s update [-c commit] [path]\n",
485 507dc3bb 2018-12-29 stsp getprogname());
489 507dc3bb 2018-12-29 stsp static void
490 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
492 784955db 2019-01-12 stsp int *did_something = arg;
494 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
497 1545c615 2019-02-10 stsp *did_something = 1;
498 507dc3bb 2018-12-29 stsp while (path[0] == '/')
500 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
503 be7061eb 2018-12-30 stsp static const struct got_error *
504 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
506 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
507 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
508 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
509 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
510 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
511 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
512 784955db 2019-01-12 stsp int ch, did_something = 0;
514 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
515 507dc3bb 2018-12-29 stsp switch (ch) {
517 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
518 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
519 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
522 2deda0b9 2019-03-07 stsp usage_update();
523 507dc3bb 2018-12-29 stsp /* NOTREACHED */
527 507dc3bb 2018-12-29 stsp argc -= optind;
528 507dc3bb 2018-12-29 stsp argv += optind;
530 507dc3bb 2018-12-29 stsp #ifndef PROFILE
531 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
532 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
533 507dc3bb 2018-12-29 stsp err(1, "pledge");
535 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
536 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
537 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
540 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
544 507dc3bb 2018-12-29 stsp if (argc == 0) {
545 c4cdcb68 2019-04-03 stsp path = strdup("");
546 c4cdcb68 2019-04-03 stsp if (path == NULL) {
547 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
550 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
551 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
555 507dc3bb 2018-12-29 stsp usage_update();
557 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
558 507dc3bb 2018-12-29 stsp if (error != NULL)
561 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
562 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
566 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
567 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
568 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
569 507dc3bb 2018-12-29 stsp if (error != NULL)
571 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
572 9c4b8182 2019-01-02 stsp if (error != NULL)
574 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
575 507dc3bb 2018-12-29 stsp if (error != NULL)
578 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
579 507dc3bb 2018-12-29 stsp commit_id_str);
580 507dc3bb 2018-12-29 stsp if (error != NULL)
584 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
585 be7061eb 2018-12-30 stsp if (error != NULL)
588 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
589 507dc3bb 2018-12-29 stsp commit_id) != 0) {
590 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
591 507dc3bb 2018-12-29 stsp commit_id);
596 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
597 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
598 507dc3bb 2018-12-29 stsp if (error != NULL)
601 784955db 2019-01-12 stsp if (did_something)
602 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
604 784955db 2019-01-12 stsp printf("Already up-to-date\n");
606 c09a553d 2018-03-12 stsp free(worktree_path);
607 c4cdcb68 2019-04-03 stsp free(path);
608 507dc3bb 2018-12-29 stsp free(commit_id);
609 9c4b8182 2019-01-02 stsp free(commit_id_str);
610 c09a553d 2018-03-12 stsp return error;
613 f42b1b34 2018-03-12 stsp static const struct got_error *
614 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
615 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
617 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
618 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
619 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
620 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
622 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
623 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
625 79109fed 2018-03-27 stsp return err;
627 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
628 79f35eb3 2018-06-11 stsp if (qid != NULL) {
629 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
631 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
633 79109fed 2018-03-27 stsp return err;
635 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
636 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
637 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
639 0f2b3dca 2018-12-22 stsp return err;
641 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
643 79109fed 2018-03-27 stsp return err;
646 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
650 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
651 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
654 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
655 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
656 0f2b3dca 2018-12-22 stsp free(id_str1);
657 0f2b3dca 2018-12-22 stsp free(id_str2);
658 79109fed 2018-03-27 stsp return err;
661 4bb494d5 2018-06-16 stsp static char *
662 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
664 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
665 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
671 79109fed 2018-03-27 stsp static const struct got_error *
672 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
673 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
674 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
676 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
677 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
678 6c281f94 2018-06-11 stsp char datebuf[26];
679 45d799e2 2018-12-23 stsp time_t committer_time;
680 45d799e2 2018-12-23 stsp const char *author, *committer;
681 199a4027 2019-02-02 stsp char *refs_str = NULL;
682 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
684 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
686 199a4027 2019-02-02 stsp const char *name;
687 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
689 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
690 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
692 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
694 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
696 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
698 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
700 199a4027 2019-02-02 stsp s = refs_str;
701 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
702 199a4027 2019-02-02 stsp name) == -1) {
703 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
709 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
711 8bf5b3c9 2018-03-17 stsp return err;
713 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
714 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
715 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
716 832c249c 2018-06-10 stsp free(id_str);
717 d3d493d7 2019-02-21 stsp id_str = NULL;
718 d3d493d7 2019-02-21 stsp free(refs_str);
719 d3d493d7 2019-02-21 stsp refs_str = NULL;
720 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
721 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
722 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
723 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
724 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
725 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
726 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
727 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
728 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
729 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
730 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
732 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
733 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
734 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
736 a0603db2 2018-06-10 stsp return err;
737 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
738 a0603db2 2018-06-10 stsp free(id_str);
742 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
743 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
744 230a42bd 2019-05-11 jcs return got_error_prefix_errno("strdup");
746 621015ac 2018-07-23 stsp logmsg = logmsg0;
748 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
750 832c249c 2018-06-10 stsp printf(" %s\n", line);
751 832c249c 2018-06-10 stsp } while (line);
752 621015ac 2018-07-23 stsp free(logmsg0);
754 971751ac 2018-03-27 stsp if (show_patch) {
755 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
756 971751ac 2018-03-27 stsp if (err == 0)
757 971751ac 2018-03-27 stsp printf("\n");
760 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
761 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fflush");
762 07862c20 2018-09-15 stsp return err;
765 f42b1b34 2018-03-12 stsp static const struct got_error *
766 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
767 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
768 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
770 f42b1b34 2018-03-12 stsp const struct got_error *err;
771 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
773 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
774 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
776 f42b1b34 2018-03-12 stsp return err;
777 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
781 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
782 372ccdbb 2018-06-10 stsp struct got_object_id *id;
784 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
787 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
789 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
790 9ba79e04 2018-06-11 stsp err = NULL;
793 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
795 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
801 b43fbaa0 2018-06-11 stsp if (id == NULL)
804 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
807 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
809 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
810 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
814 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
815 f42b1b34 2018-03-12 stsp return err;
818 4ed7e80c 2018-05-20 stsp __dead static void
819 6f3d1eb0 2018-03-12 stsp usage_log(void)
821 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
822 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
826 4ed7e80c 2018-05-20 stsp static const struct got_error *
827 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
829 f42b1b34 2018-03-12 stsp const struct got_error *error;
830 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
831 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
832 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
833 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
834 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
835 d142fc45 2018-04-01 stsp char *start_commit = NULL;
836 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
837 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
838 64a96a6d 2018-04-01 stsp const char *errstr;
839 199a4027 2019-02-02 stsp struct got_reflist_head refs;
841 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
843 6715a751 2018-03-16 stsp #ifndef PROFILE
844 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
847 f42b1b34 2018-03-12 stsp err(1, "pledge");
850 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
851 79109fed 2018-03-27 stsp switch (ch) {
853 79109fed 2018-03-27 stsp show_patch = 1;
856 d142fc45 2018-04-01 stsp start_commit = optarg;
859 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
861 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
862 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
865 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
866 64a96a6d 2018-04-01 stsp if (errstr != NULL)
867 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
870 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
873 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
874 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
875 04ca23f4 2018-07-16 stsp err(1, "-r option");
876 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
879 2deda0b9 2019-03-07 stsp usage_log();
880 79109fed 2018-03-27 stsp /* NOTREACHED */
884 79109fed 2018-03-27 stsp argc -= optind;
885 79109fed 2018-03-27 stsp argv += optind;
887 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
888 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
889 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
893 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
894 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
896 cffc0aa4 2019-02-05 stsp error = NULL;
898 e7301579 2019-03-18 stsp if (argc == 0) {
899 cbd1af7a 2019-03-18 stsp path = strdup("");
900 e7301579 2019-03-18 stsp if (path == NULL) {
901 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
904 e7301579 2019-03-18 stsp } else if (argc == 1) {
905 e7301579 2019-03-18 stsp if (worktree) {
906 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
911 e7301579 2019-03-18 stsp path = strdup(argv[0]);
912 e7301579 2019-03-18 stsp if (path == NULL) {
913 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
918 cbd1af7a 2019-03-18 stsp usage_log();
920 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
921 5486daa2 2019-05-11 stsp repo_path = worktree ?
922 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
924 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
925 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
929 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
930 d7d4f210 2018-03-12 stsp if (error != NULL)
933 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
934 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
938 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
939 3235492e 2018-04-01 stsp struct got_reference *head_ref;
940 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
941 3235492e 2018-04-01 stsp if (error != NULL)
942 3235492e 2018-04-01 stsp return error;
943 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
944 3235492e 2018-04-01 stsp got_ref_close(head_ref);
945 3235492e 2018-04-01 stsp if (error != NULL)
946 3235492e 2018-04-01 stsp return error;
947 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
949 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
950 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
951 3235492e 2018-04-01 stsp if (error == NULL) {
952 1caad61b 2019-02-01 stsp int obj_type;
953 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
954 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
955 d1f2edc9 2018-06-13 stsp if (error != NULL)
957 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
958 1caad61b 2019-02-01 stsp if (error != NULL)
960 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
961 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
962 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
963 1caad61b 2019-02-01 stsp if (error != NULL)
965 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
966 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
967 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
968 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
972 1caad61b 2019-02-01 stsp id = got_object_id_dup(
973 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
974 1caad61b 2019-02-01 stsp if (id == NULL)
975 230a42bd 2019-05-11 jcs error = got_error_prefix_errno(
976 230a42bd 2019-05-11 jcs "got_object_id_dup");
977 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
980 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
981 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
984 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
985 d1f2edc9 2018-06-13 stsp if (error != NULL)
988 15a94983 2018-12-23 stsp if (commit == NULL) {
989 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
990 d1f2edc9 2018-06-13 stsp start_commit);
991 d1f2edc9 2018-06-13 stsp if (error != NULL)
992 d1f2edc9 2018-06-13 stsp return error;
995 d7d4f210 2018-03-12 stsp if (error != NULL)
998 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
999 04ca23f4 2018-07-16 stsp if (error != NULL)
1000 04ca23f4 2018-07-16 stsp goto done;
1001 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1002 04ca23f4 2018-07-16 stsp free(path);
1003 04ca23f4 2018-07-16 stsp path = in_repo_path;
1006 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1007 199a4027 2019-02-02 stsp if (error)
1008 199a4027 2019-02-02 stsp goto done;
1010 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1011 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1013 04ca23f4 2018-07-16 stsp free(path);
1014 04ca23f4 2018-07-16 stsp free(repo_path);
1015 04ca23f4 2018-07-16 stsp free(cwd);
1017 cffc0aa4 2019-02-05 stsp if (worktree)
1018 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1019 ad242220 2018-09-08 stsp if (repo) {
1020 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1021 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1022 ad242220 2018-09-08 stsp if (error == NULL)
1023 ad242220 2018-09-08 stsp error = repo_error;
1025 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1026 8bf5b3c9 2018-03-17 stsp return error;
1029 4ed7e80c 2018-05-20 stsp __dead static void
1030 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1032 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1033 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1037 b72f483a 2019-02-05 stsp struct print_diff_arg {
1038 b72f483a 2019-02-05 stsp struct got_repository *repo;
1039 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1040 b72f483a 2019-02-05 stsp int diff_context;
1041 3fc0c068 2019-02-10 stsp const char *id_str;
1042 3fc0c068 2019-02-10 stsp int header_shown;
1045 4ed7e80c 2018-05-20 stsp static const struct got_error *
1046 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1047 b72f483a 2019-02-05 stsp struct got_object_id *id)
1049 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1050 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1051 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1052 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1053 b72f483a 2019-02-05 stsp char *abspath = NULL;
1054 b72f483a 2019-02-05 stsp struct stat sb;
1056 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1057 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1058 b72f483a 2019-02-05 stsp return NULL;
1060 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1061 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1062 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1063 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1066 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1067 d00136be 2019-03-26 stsp err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1069 d00136be 2019-03-26 stsp goto done;
1073 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1074 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1075 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1076 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1077 2ec1f75b 2019-03-26 stsp goto done;
1080 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1081 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1082 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("fopen", abspath);
1083 2ec1f75b 2019-03-26 stsp goto done;
1085 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1086 230a42bd 2019-05-11 jcs err = got_error_prefix_errno2("lstat", abspath);
1087 2ec1f75b 2019-03-26 stsp goto done;
1090 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1092 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1095 b72f483a 2019-02-05 stsp if (blob1)
1096 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1097 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1098 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
1099 b72f483a 2019-02-05 stsp free(abspath);
1100 927df6b7 2019-02-10 stsp return err;
1103 927df6b7 2019-02-10 stsp static const struct got_error *
1104 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1106 b00d56cd 2018-04-01 stsp const struct got_error *error;
1107 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1108 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1109 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1110 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1111 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
1112 15a94983 2018-12-23 stsp int type1, type2;
1113 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1114 c0cc5c62 2018-10-18 stsp const char *errstr;
1115 927df6b7 2019-02-10 stsp char *path = NULL;
1117 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1118 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1119 25eccc22 2019-01-04 stsp NULL) == -1)
1120 b00d56cd 2018-04-01 stsp err(1, "pledge");
1123 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1124 b00d56cd 2018-04-01 stsp switch (ch) {
1126 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1127 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1128 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1131 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1132 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1133 b72f483a 2019-02-05 stsp err(1, "-r option");
1134 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1137 2deda0b9 2019-03-07 stsp usage_diff();
1138 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1142 b00d56cd 2018-04-01 stsp argc -= optind;
1143 b00d56cd 2018-04-01 stsp argv += optind;
1145 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1146 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1147 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1148 b72f483a 2019-02-05 stsp goto done;
1150 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1151 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1152 927df6b7 2019-02-10 stsp goto done;
1153 927df6b7 2019-02-10 stsp if (argc <= 1) {
1154 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1155 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1156 927df6b7 2019-02-10 stsp goto done;
1158 b72f483a 2019-02-05 stsp if (repo_path)
1160 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1161 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1162 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1163 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1164 927df6b7 2019-02-10 stsp goto done;
1166 927df6b7 2019-02-10 stsp if (argc == 1) {
1167 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1169 927df6b7 2019-02-10 stsp if (error)
1170 927df6b7 2019-02-10 stsp goto done;
1172 927df6b7 2019-02-10 stsp path = strdup("");
1173 927df6b7 2019-02-10 stsp if (path == NULL) {
1174 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1175 927df6b7 2019-02-10 stsp goto done;
1178 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1179 15a94983 2018-12-23 stsp id_str1 = argv[0];
1180 15a94983 2018-12-23 stsp id_str2 = argv[1];
1182 b00d56cd 2018-04-01 stsp usage_diff();
1184 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1185 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1186 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1187 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
1190 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1191 76089277 2018-04-01 stsp free(repo_path);
1192 b00d56cd 2018-04-01 stsp if (error != NULL)
1193 b00d56cd 2018-04-01 stsp goto done;
1195 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1196 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1197 c02c541e 2019-03-29 stsp if (error)
1198 c02c541e 2019-03-29 stsp goto done;
1200 b72f483a 2019-02-05 stsp if (worktree) {
1201 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1202 b72f483a 2019-02-05 stsp char *id_str;
1203 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1204 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1205 b72f483a 2019-02-05 stsp if (error)
1206 b72f483a 2019-02-05 stsp goto done;
1207 b72f483a 2019-02-05 stsp arg.repo = repo;
1208 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1209 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1210 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1211 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1213 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1214 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1215 b72f483a 2019-02-05 stsp free(id_str);
1216 b72f483a 2019-02-05 stsp goto done;
1219 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
1220 6402fb3c 2018-09-15 stsp if (error)
1221 b00d56cd 2018-04-01 stsp goto done;
1223 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
1224 6402fb3c 2018-09-15 stsp if (error)
1225 b00d56cd 2018-04-01 stsp goto done;
1227 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1228 15a94983 2018-12-23 stsp if (error)
1229 15a94983 2018-12-23 stsp goto done;
1231 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1232 15a94983 2018-12-23 stsp if (error)
1233 15a94983 2018-12-23 stsp goto done;
1235 15a94983 2018-12-23 stsp if (type1 != type2) {
1236 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1237 b00d56cd 2018-04-01 stsp goto done;
1240 15a94983 2018-12-23 stsp switch (type1) {
1241 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1242 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1243 54156555 2018-12-24 stsp diff_context, repo, stdout);
1245 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1246 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1247 54156555 2018-12-24 stsp diff_context, repo, stdout);
1249 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1250 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1252 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1253 c0cc5c62 2018-10-18 stsp repo, stdout);
1256 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1260 15a94983 2018-12-23 stsp free(id1);
1261 15a94983 2018-12-23 stsp free(id2);
1262 927df6b7 2019-02-10 stsp free(path);
1263 b72f483a 2019-02-05 stsp if (worktree)
1264 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1265 ad242220 2018-09-08 stsp if (repo) {
1266 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1267 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1268 ad242220 2018-09-08 stsp if (error == NULL)
1269 ad242220 2018-09-08 stsp error = repo_error;
1271 b00d56cd 2018-04-01 stsp return error;
1274 404c43c4 2018-06-21 stsp __dead static void
1275 404c43c4 2018-06-21 stsp usage_blame(void)
1277 9270e621 2019-02-05 stsp fprintf(stderr,
1278 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1279 404c43c4 2018-06-21 stsp getprogname());
1283 404c43c4 2018-06-21 stsp static const struct got_error *
1284 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1286 404c43c4 2018-06-21 stsp const struct got_error *error;
1287 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1288 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1289 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1290 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1291 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1294 404c43c4 2018-06-21 stsp #ifndef PROFILE
1295 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1296 36e2fb66 2019-01-04 stsp NULL) == -1)
1297 404c43c4 2018-06-21 stsp err(1, "pledge");
1300 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1301 404c43c4 2018-06-21 stsp switch (ch) {
1303 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1306 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1307 66bea077 2018-08-02 stsp if (repo_path == NULL)
1308 66bea077 2018-08-02 stsp err(1, "-r option");
1309 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1312 2deda0b9 2019-03-07 stsp usage_blame();
1313 404c43c4 2018-06-21 stsp /* NOTREACHED */
1317 404c43c4 2018-06-21 stsp argc -= optind;
1318 404c43c4 2018-06-21 stsp argv += optind;
1320 a39318fd 2018-08-02 stsp if (argc == 1)
1321 404c43c4 2018-06-21 stsp path = argv[0];
1323 404c43c4 2018-06-21 stsp usage_blame();
1325 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1326 66bea077 2018-08-02 stsp if (cwd == NULL) {
1327 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1328 66bea077 2018-08-02 stsp goto done;
1330 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1331 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1332 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1333 66bea077 2018-08-02 stsp goto done;
1335 0c06baac 2019-02-05 stsp error = NULL;
1336 0c06baac 2019-02-05 stsp if (worktree) {
1337 0c06baac 2019-02-05 stsp repo_path =
1338 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1339 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1340 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1341 0c06baac 2019-02-05 stsp if (error)
1342 0c06baac 2019-02-05 stsp goto done;
1344 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1345 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1346 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1347 0c06baac 2019-02-05 stsp goto done;
1352 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1353 c02c541e 2019-03-29 stsp if (error != NULL)
1354 36e2fb66 2019-01-04 stsp goto done;
1356 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1357 c02c541e 2019-03-29 stsp if (error)
1358 404c43c4 2018-06-21 stsp goto done;
1360 0c06baac 2019-02-05 stsp if (worktree) {
1361 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1362 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1363 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1364 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1365 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1366 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1367 6efaaa2d 2019-02-05 stsp path) == -1) {
1368 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1369 0c06baac 2019-02-05 stsp goto done;
1371 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1374 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1376 0c06baac 2019-02-05 stsp if (error)
1377 66bea077 2018-08-02 stsp goto done;
1379 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1380 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1381 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1382 404c43c4 2018-06-21 stsp if (error != NULL)
1383 66bea077 2018-08-02 stsp goto done;
1384 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1385 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1386 404c43c4 2018-06-21 stsp if (error != NULL)
1387 66bea077 2018-08-02 stsp goto done;
1389 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1390 15a94983 2018-12-23 stsp commit_id_str);
1391 404c43c4 2018-06-21 stsp if (error != NULL)
1392 66bea077 2018-08-02 stsp goto done;
1395 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1397 66bea077 2018-08-02 stsp free(in_repo_path);
1398 66bea077 2018-08-02 stsp free(repo_path);
1399 66bea077 2018-08-02 stsp free(cwd);
1400 404c43c4 2018-06-21 stsp free(commit_id);
1401 0c06baac 2019-02-05 stsp if (worktree)
1402 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1403 ad242220 2018-09-08 stsp if (repo) {
1404 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1405 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1406 ad242220 2018-09-08 stsp if (error == NULL)
1407 ad242220 2018-09-08 stsp error = repo_error;
1409 404c43c4 2018-06-21 stsp return error;
1412 5de5890b 2018-10-18 stsp __dead static void
1413 5de5890b 2018-10-18 stsp usage_tree(void)
1415 c1669e2e 2019-01-09 stsp fprintf(stderr,
1416 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1417 5de5890b 2018-10-18 stsp getprogname());
1421 c1669e2e 2019-01-09 stsp static void
1422 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1423 c1669e2e 2019-01-09 stsp const char *root_path)
1425 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1427 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1428 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1431 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1432 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1433 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1436 5de5890b 2018-10-18 stsp static const struct got_error *
1437 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1438 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1439 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1441 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1442 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1443 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1444 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1445 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1447 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1449 5de5890b 2018-10-18 stsp goto done;
1451 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1453 5de5890b 2018-10-18 stsp goto done;
1454 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1455 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1456 5de5890b 2018-10-18 stsp while (te) {
1457 5de5890b 2018-10-18 stsp char *id = NULL;
1459 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1462 5de5890b 2018-10-18 stsp if (show_ids) {
1463 5de5890b 2018-10-18 stsp char *id_str;
1464 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1466 5de5890b 2018-10-18 stsp goto done;
1467 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1468 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1469 5de5890b 2018-10-18 stsp free(id_str);
1470 5de5890b 2018-10-18 stsp goto done;
1472 5de5890b 2018-10-18 stsp free(id_str);
1474 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1477 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1478 c1669e2e 2019-01-09 stsp char *child_path;
1479 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1480 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1481 c1669e2e 2019-01-09 stsp te->name) == -1) {
1482 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1483 c1669e2e 2019-01-09 stsp goto done;
1485 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1486 c1669e2e 2019-01-09 stsp root_path, repo);
1487 c1669e2e 2019-01-09 stsp free(child_path);
1489 c1669e2e 2019-01-09 stsp goto done;
1492 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1496 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1497 5de5890b 2018-10-18 stsp free(tree_id);
1498 5de5890b 2018-10-18 stsp return err;
1501 5de5890b 2018-10-18 stsp static const struct got_error *
1502 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1504 5de5890b 2018-10-18 stsp const struct got_error *error;
1505 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1506 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1507 7a2c19d6 2019-02-05 stsp const char *path;
1508 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1509 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1510 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1511 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1514 5de5890b 2018-10-18 stsp #ifndef PROFILE
1515 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1516 0f8d269b 2019-01-04 stsp NULL) == -1)
1517 5de5890b 2018-10-18 stsp err(1, "pledge");
1520 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1521 5de5890b 2018-10-18 stsp switch (ch) {
1523 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1526 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1527 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1528 5de5890b 2018-10-18 stsp err(1, "-r option");
1529 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1532 5de5890b 2018-10-18 stsp show_ids = 1;
1535 c1669e2e 2019-01-09 stsp recurse = 1;
1538 2deda0b9 2019-03-07 stsp usage_tree();
1539 5de5890b 2018-10-18 stsp /* NOTREACHED */
1543 5de5890b 2018-10-18 stsp argc -= optind;
1544 5de5890b 2018-10-18 stsp argv += optind;
1546 5de5890b 2018-10-18 stsp if (argc == 1)
1547 5de5890b 2018-10-18 stsp path = argv[0];
1548 5de5890b 2018-10-18 stsp else if (argc > 1)
1549 5de5890b 2018-10-18 stsp usage_tree();
1551 7a2c19d6 2019-02-05 stsp path = NULL;
1553 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1554 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1555 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1556 9bf7a39b 2019-02-05 stsp goto done;
1558 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1559 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1560 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1561 7a2c19d6 2019-02-05 stsp goto done;
1563 7a2c19d6 2019-02-05 stsp error = NULL;
1564 7a2c19d6 2019-02-05 stsp if (worktree) {
1565 7a2c19d6 2019-02-05 stsp repo_path =
1566 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1567 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1568 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1569 7a2c19d6 2019-02-05 stsp if (error)
1570 7a2c19d6 2019-02-05 stsp goto done;
1572 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1573 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1574 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1575 7a2c19d6 2019-02-05 stsp goto done;
1580 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1581 5de5890b 2018-10-18 stsp if (error != NULL)
1582 c02c541e 2019-03-29 stsp goto done;
1584 a0937847 2019-05-11 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1585 c02c541e 2019-03-29 stsp if (error)
1586 5de5890b 2018-10-18 stsp goto done;
1588 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1589 9bf7a39b 2019-02-05 stsp if (worktree) {
1590 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1591 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1592 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1593 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1594 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1595 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
1596 9bf7a39b 2019-02-05 stsp goto done;
1598 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1600 9bf7a39b 2019-02-05 stsp if (error)
1601 9bf7a39b 2019-02-05 stsp goto done;
1603 9bf7a39b 2019-02-05 stsp path = "/";
1605 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1606 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1607 9bf7a39b 2019-02-05 stsp if (error != NULL)
1608 9bf7a39b 2019-02-05 stsp goto done;
1611 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1612 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1613 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1614 5de5890b 2018-10-18 stsp if (error != NULL)
1615 5de5890b 2018-10-18 stsp goto done;
1616 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1617 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1618 5de5890b 2018-10-18 stsp if (error != NULL)
1619 5de5890b 2018-10-18 stsp goto done;
1621 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1622 15a94983 2018-12-23 stsp commit_id_str);
1623 5de5890b 2018-10-18 stsp if (error != NULL)
1624 5de5890b 2018-10-18 stsp goto done;
1627 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1628 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1630 5de5890b 2018-10-18 stsp free(in_repo_path);
1631 5de5890b 2018-10-18 stsp free(repo_path);
1632 5de5890b 2018-10-18 stsp free(cwd);
1633 5de5890b 2018-10-18 stsp free(commit_id);
1634 7a2c19d6 2019-02-05 stsp if (worktree)
1635 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1636 5de5890b 2018-10-18 stsp if (repo) {
1637 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1638 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1639 5de5890b 2018-10-18 stsp if (error == NULL)
1640 5de5890b 2018-10-18 stsp error = repo_error;
1642 5de5890b 2018-10-18 stsp return error;
1645 6bad629b 2019-02-04 stsp __dead static void
1646 6bad629b 2019-02-04 stsp usage_status(void)
1648 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1652 b72f483a 2019-02-05 stsp static const struct got_error *
1653 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1654 b72f483a 2019-02-05 stsp struct got_object_id *id)
1656 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1657 b72f483a 2019-02-05 stsp return NULL;
1660 6bad629b 2019-02-04 stsp static const struct got_error *
1661 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1663 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1664 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1665 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1666 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1669 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1670 6bad629b 2019-02-04 stsp switch (ch) {
1672 2deda0b9 2019-03-07 stsp usage_status();
1673 6bad629b 2019-02-04 stsp /* NOTREACHED */
1677 6bad629b 2019-02-04 stsp argc -= optind;
1678 6bad629b 2019-02-04 stsp argv += optind;
1680 6bad629b 2019-02-04 stsp #ifndef PROFILE
1681 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1682 6bad629b 2019-02-04 stsp NULL) == -1)
1683 6bad629b 2019-02-04 stsp err(1, "pledge");
1685 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1686 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1687 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1688 927df6b7 2019-02-10 stsp goto done;
1691 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1692 927df6b7 2019-02-10 stsp if (error != NULL)
1693 927df6b7 2019-02-10 stsp goto done;
1695 6bad629b 2019-02-04 stsp if (argc == 0) {
1696 927df6b7 2019-02-10 stsp path = strdup("");
1697 927df6b7 2019-02-10 stsp if (path == NULL) {
1698 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1699 6bad629b 2019-02-04 stsp goto done;
1701 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1702 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1703 927df6b7 2019-02-10 stsp if (error)
1704 6bad629b 2019-02-04 stsp goto done;
1706 6bad629b 2019-02-04 stsp usage_status();
1708 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1709 6bad629b 2019-02-04 stsp if (error != NULL)
1710 6bad629b 2019-02-04 stsp goto done;
1712 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1713 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1714 6bad629b 2019-02-04 stsp if (error)
1715 6bad629b 2019-02-04 stsp goto done;
1717 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1718 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1720 927df6b7 2019-02-10 stsp free(cwd);
1721 927df6b7 2019-02-10 stsp free(path);
1722 d0eebce4 2019-03-11 stsp return error;
1725 d0eebce4 2019-03-11 stsp __dead static void
1726 d0eebce4 2019-03-11 stsp usage_ref(void)
1728 d0eebce4 2019-03-11 stsp fprintf(stderr,
1729 d0eebce4 2019-03-11 stsp "usage: %s ref [-r repository] -l | -d name | name object\n",
1730 d0eebce4 2019-03-11 stsp getprogname());
1734 d0eebce4 2019-03-11 stsp static const struct got_error *
1735 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1737 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1738 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1739 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1741 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
1742 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
1744 d0eebce4 2019-03-11 stsp return err;
1746 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1747 d0eebce4 2019-03-11 stsp char *refstr;
1748 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
1749 d0eebce4 2019-03-11 stsp if (refstr == NULL)
1750 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_ref_to_str");
1751 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1752 d0eebce4 2019-03-11 stsp free(refstr);
1755 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1756 d0eebce4 2019-03-11 stsp return NULL;
1759 d0eebce4 2019-03-11 stsp static const struct got_error *
1760 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
1762 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1763 d0eebce4 2019-03-11 stsp struct got_reference *ref;
1765 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
1767 d0eebce4 2019-03-11 stsp return err;
1769 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
1770 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1771 d0eebce4 2019-03-11 stsp return err;
1774 d0eebce4 2019-03-11 stsp static const struct got_error *
1775 d0eebce4 2019-03-11 stsp add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1777 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
1778 d0eebce4 2019-03-11 stsp struct got_object_id *id;
1779 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
1781 d0eebce4 2019-03-11 stsp err = got_object_resolve_id_str(&id, repo, id_str);
1783 d0eebce4 2019-03-11 stsp return err;
1785 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
1787 d0eebce4 2019-03-11 stsp goto done;
1789 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
1792 d0eebce4 2019-03-11 stsp got_ref_close(ref);
1794 d0eebce4 2019-03-11 stsp return err;
1797 d0eebce4 2019-03-11 stsp static const struct got_error *
1798 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
1800 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
1801 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
1802 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
1803 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
1804 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
1805 d0eebce4 2019-03-11 stsp const char *delref = NULL;
1807 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
1808 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1809 d0eebce4 2019-03-11 stsp switch (ch) {
1811 d0eebce4 2019-03-11 stsp delref = optarg;
1814 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
1815 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1816 d0eebce4 2019-03-11 stsp err(1, "-r option");
1817 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1820 d0eebce4 2019-03-11 stsp do_list = 1;
1823 d0eebce4 2019-03-11 stsp usage_ref();
1824 d0eebce4 2019-03-11 stsp /* NOTREACHED */
1828 d0eebce4 2019-03-11 stsp if (do_list && delref)
1829 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
1831 d0eebce4 2019-03-11 stsp argc -= optind;
1832 d0eebce4 2019-03-11 stsp argv += optind;
1834 d0eebce4 2019-03-11 stsp if (do_list || delref) {
1835 d0eebce4 2019-03-11 stsp if (argc > 0)
1836 d0eebce4 2019-03-11 stsp usage_ref();
1837 d0eebce4 2019-03-11 stsp } else if (argc != 2)
1838 d0eebce4 2019-03-11 stsp usage_ref();
1840 d0eebce4 2019-03-11 stsp #ifndef PROFILE
1841 e0b57350 2019-03-12 stsp if (do_list) {
1842 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1843 e0b57350 2019-03-12 stsp NULL) == -1)
1844 e0b57350 2019-03-12 stsp err(1, "pledge");
1846 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1847 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
1848 e0b57350 2019-03-12 stsp err(1, "pledge");
1851 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
1852 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
1853 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1854 d0eebce4 2019-03-11 stsp goto done;
1857 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1858 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
1859 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1860 d0eebce4 2019-03-11 stsp goto done;
1862 d0eebce4 2019-03-11 stsp error = NULL;
1863 d0eebce4 2019-03-11 stsp if (worktree) {
1864 d0eebce4 2019-03-11 stsp repo_path =
1865 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
1866 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
1867 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1868 d0eebce4 2019-03-11 stsp if (error)
1869 d0eebce4 2019-03-11 stsp goto done;
1871 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
1872 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
1873 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1874 d0eebce4 2019-03-11 stsp goto done;
1879 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
1880 d0eebce4 2019-03-11 stsp if (error != NULL)
1881 d0eebce4 2019-03-11 stsp goto done;
1883 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
1884 a0937847 2019-05-11 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1885 c02c541e 2019-03-29 stsp if (error)
1886 c02c541e 2019-03-29 stsp goto done;
1888 d0eebce4 2019-03-11 stsp if (do_list)
1889 d0eebce4 2019-03-11 stsp error = list_refs(repo);
1890 d0eebce4 2019-03-11 stsp else if (delref)
1891 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
1893 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
1896 d0eebce4 2019-03-11 stsp got_repo_close(repo);
1897 d0eebce4 2019-03-11 stsp if (worktree)
1898 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
1899 d0eebce4 2019-03-11 stsp free(cwd);
1900 d0eebce4 2019-03-11 stsp free(repo_path);
1901 d00136be 2019-03-26 stsp return error;
1904 d00136be 2019-03-26 stsp __dead static void
1905 d00136be 2019-03-26 stsp usage_add(void)
1907 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1911 d00136be 2019-03-26 stsp static const struct got_error *
1912 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
1914 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
1915 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
1916 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
1917 1dd54920 2019-05-11 stsp char *cwd = NULL;
1918 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
1919 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
1922 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
1924 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1925 d00136be 2019-03-26 stsp switch (ch) {
1927 d00136be 2019-03-26 stsp usage_add();
1928 d00136be 2019-03-26 stsp /* NOTREACHED */
1932 d00136be 2019-03-26 stsp argc -= optind;
1933 d00136be 2019-03-26 stsp argv += optind;
1935 723c305c 2019-05-11 jcs if (argc < 1)
1936 d00136be 2019-03-26 stsp usage_add();
1938 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
1939 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1940 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1941 723c305c 2019-05-11 jcs if (path == NULL) {
1942 723c305c 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[x]);
1945 1dd54920 2019-05-11 stsp free(path);
1948 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
1949 d00136be 2019-03-26 stsp if (cwd == NULL) {
1950 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1951 d00136be 2019-03-26 stsp goto done;
1954 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
1955 d00136be 2019-03-26 stsp if (error)
1956 d00136be 2019-03-26 stsp goto done;
1958 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1959 031a5338 2019-03-26 stsp if (error != NULL)
1960 031a5338 2019-03-26 stsp goto done;
1962 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1963 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
1964 d00136be 2019-03-26 stsp if (error)
1965 d00136be 2019-03-26 stsp goto done;
1967 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
1968 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
1969 723c305c 2019-05-11 jcs if (path == NULL) {
1970 723c305c 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[x]);
1974 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
1975 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
1976 1dd54920 2019-05-11 stsp if (error) {
1977 1dd54920 2019-05-11 stsp free(path);
1981 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
1982 1dd54920 2019-05-11 stsp NULL, repo);
1985 031a5338 2019-03-26 stsp got_repo_close(repo);
1986 d00136be 2019-03-26 stsp if (worktree)
1987 d00136be 2019-03-26 stsp got_worktree_close(worktree);
1988 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
1989 1dd54920 2019-05-11 stsp free((char *)pe->path);
1990 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
1991 2ec1f75b 2019-03-26 stsp free(cwd);
1992 2ec1f75b 2019-03-26 stsp return error;
1995 2ec1f75b 2019-03-26 stsp __dead static void
1996 2ec1f75b 2019-03-26 stsp usage_rm(void)
1998 2ec1f75b 2019-03-26 stsp fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2002 2ec1f75b 2019-03-26 stsp static const struct got_error *
2003 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2005 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2006 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2007 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2008 2ec1f75b 2019-03-26 stsp char *cwd = NULL, *path = NULL;
2009 2ec1f75b 2019-03-26 stsp int ch, delete_local_mods = 0;
2011 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2012 2ec1f75b 2019-03-26 stsp switch (ch) {
2014 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2017 2ec1f75b 2019-03-26 stsp usage_add();
2018 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2022 2ec1f75b 2019-03-26 stsp argc -= optind;
2023 2ec1f75b 2019-03-26 stsp argv += optind;
2025 2ec1f75b 2019-03-26 stsp if (argc != 1)
2026 2ec1f75b 2019-03-26 stsp usage_rm();
2028 2ec1f75b 2019-03-26 stsp path = realpath(argv[0], NULL);
2029 2ec1f75b 2019-03-26 stsp if (path == NULL) {
2030 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2031 2ec1f75b 2019-03-26 stsp goto done;
2033 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2035 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2036 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2037 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2038 2ec1f75b 2019-03-26 stsp goto done;
2040 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2041 2ec1f75b 2019-03-26 stsp if (error)
2042 2ec1f75b 2019-03-26 stsp goto done;
2044 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2046 2ec1f75b 2019-03-26 stsp goto done;
2048 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2049 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2050 2ec1f75b 2019-03-26 stsp if (error)
2051 2ec1f75b 2019-03-26 stsp goto done;
2053 2ec1f75b 2019-03-26 stsp error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2054 2ec1f75b 2019-03-26 stsp print_status, NULL, repo);
2055 a129376b 2019-03-28 stsp if (error)
2056 a129376b 2019-03-28 stsp goto done;
2059 a129376b 2019-03-28 stsp got_repo_close(repo);
2060 a129376b 2019-03-28 stsp if (worktree)
2061 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2062 a129376b 2019-03-28 stsp free(path);
2063 a129376b 2019-03-28 stsp free(cwd);
2064 a129376b 2019-03-28 stsp return error;
2067 a129376b 2019-03-28 stsp __dead static void
2068 a129376b 2019-03-28 stsp usage_revert(void)
2070 a129376b 2019-03-28 stsp fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2074 a129376b 2019-03-28 stsp static void
2075 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2077 a129376b 2019-03-28 stsp while (path[0] == '/')
2079 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2082 a129376b 2019-03-28 stsp static const struct got_error *
2083 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2085 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2086 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2087 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2088 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2091 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2092 a129376b 2019-03-28 stsp switch (ch) {
2094 a129376b 2019-03-28 stsp usage_revert();
2095 a129376b 2019-03-28 stsp /* NOTREACHED */
2099 a129376b 2019-03-28 stsp argc -= optind;
2100 a129376b 2019-03-28 stsp argv += optind;
2102 a129376b 2019-03-28 stsp if (argc != 1)
2103 a129376b 2019-03-28 stsp usage_revert();
2105 a129376b 2019-03-28 stsp path = realpath(argv[0], NULL);
2106 a129376b 2019-03-28 stsp if (path == NULL) {
2107 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2108 a129376b 2019-03-28 stsp goto done;
2110 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2112 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2113 a129376b 2019-03-28 stsp if (cwd == NULL) {
2114 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2115 a129376b 2019-03-28 stsp goto done;
2117 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2118 2ec1f75b 2019-03-26 stsp if (error)
2119 2ec1f75b 2019-03-26 stsp goto done;
2121 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2122 a129376b 2019-03-28 stsp if (error != NULL)
2123 a129376b 2019-03-28 stsp goto done;
2125 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2126 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2127 a129376b 2019-03-28 stsp if (error)
2128 a129376b 2019-03-28 stsp goto done;
2130 a129376b 2019-03-28 stsp error = got_worktree_revert(worktree, path,
2131 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2132 a129376b 2019-03-28 stsp if (error)
2133 a129376b 2019-03-28 stsp goto done;
2136 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2137 2ec1f75b 2019-03-26 stsp if (worktree)
2138 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2139 2ec1f75b 2019-03-26 stsp free(path);
2140 d00136be 2019-03-26 stsp free(cwd);
2141 6bad629b 2019-02-04 stsp return error;
2144 c4296144 2019-05-09 stsp __dead static void
2145 c4296144 2019-05-09 stsp usage_commit(void)
2147 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2152 33ad4cbe 2019-05-12 jcs spawn_editor(const char *file)
2154 33ad4cbe 2019-05-12 jcs char *argp[] = { "sh", "-c", NULL, NULL };
2155 33ad4cbe 2019-05-12 jcs char *editor, *editp;
2157 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2158 33ad4cbe 2019-05-12 jcs int st = -1;
2160 33ad4cbe 2019-05-12 jcs editor = getenv("VISUAL");
2161 33ad4cbe 2019-05-12 jcs if (editor == NULL)
2162 33ad4cbe 2019-05-12 jcs editor = getenv("EDITOR");
2163 33ad4cbe 2019-05-12 jcs if (editor == NULL)
2164 33ad4cbe 2019-05-12 jcs editor = "ed";
2166 33ad4cbe 2019-05-12 jcs if (asprintf(&editp, "%s %s", editor, file) == -1)
2169 33ad4cbe 2019-05-12 jcs argp[2] = editp;
2171 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2172 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2173 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2175 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2177 33ad4cbe 2019-05-12 jcs goto doneediting;
2179 33ad4cbe 2019-05-12 jcs execv(_PATH_BSHELL, argp);
2180 33ad4cbe 2019-05-12 jcs _exit(127);
2183 33ad4cbe 2019-05-12 jcs free(editp);
2185 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2186 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2189 33ad4cbe 2019-05-12 jcs doneediting:
2190 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2191 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2192 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2194 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2195 33ad4cbe 2019-05-12 jcs errno = EINTR;
2199 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2202 33ad4cbe 2019-05-12 jcs static const struct got_error *
2203 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2206 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2207 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2208 33ad4cbe 2019-05-12 jcs char *tmppath = NULL;
2209 33ad4cbe 2019-05-12 jcs char *tmpfile = NULL;
2210 33ad4cbe 2019-05-12 jcs char *cmdline_log = (char *)arg;
2211 33ad4cbe 2019-05-12 jcs char buf[1024];
2212 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2214 33ad4cbe 2019-05-12 jcs size_t len;
2217 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2218 33ad4cbe 2019-05-12 jcs if (cmdline_log != NULL && strlen(cmdline_log) != 0) {
2219 33ad4cbe 2019-05-12 jcs len = strlen(cmdline_log) + 1;
2220 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2221 33ad4cbe 2019-05-12 jcs strlcpy(*logmsg, cmdline_log, len);
2222 33ad4cbe 2019-05-12 jcs return NULL;
2225 33ad4cbe 2019-05-12 jcs tmppath = getenv("TMPDIR");
2226 33ad4cbe 2019-05-12 jcs if (tmppath == NULL || strlen(tmppath) == 0)
2227 33ad4cbe 2019-05-12 jcs tmppath = "/tmp";
2229 33ad4cbe 2019-05-12 jcs if (asprintf(&tmpfile, "%s/got-XXXXXXXXXX", tmppath) == -1) {
2230 33ad4cbe 2019-05-12 jcs err = got_error_prefix_errno("asprintf");
2231 33ad4cbe 2019-05-12 jcs return err;
2234 33ad4cbe 2019-05-12 jcs fd = mkstemp(tmpfile);
2235 33ad4cbe 2019-05-12 jcs if (fd < 0) {
2236 33ad4cbe 2019-05-12 jcs err = got_error_prefix_errno("mktemp");
2237 33ad4cbe 2019-05-12 jcs free(tmpfile);
2238 33ad4cbe 2019-05-12 jcs return err;
2241 33ad4cbe 2019-05-12 jcs dprintf(fd, "\n"
2242 33ad4cbe 2019-05-12 jcs "# changes to be committed:\n");
2244 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2245 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2246 33ad4cbe 2019-05-12 jcs dprintf(fd, "# %c %s\n", ct->status, pe->path);
2250 33ad4cbe 2019-05-12 jcs if (stat(tmpfile, &st) == -1) {
2251 33ad4cbe 2019-05-12 jcs err = got_error_prefix_errno2("stat", tmpfile);
2255 33ad4cbe 2019-05-12 jcs if (spawn_editor(tmpfile) == -1) {
2256 33ad4cbe 2019-05-12 jcs err = got_error_prefix_errno("failed spawning editor");
2260 33ad4cbe 2019-05-12 jcs if (stat(tmpfile, &st2) == -1) {
2261 33ad4cbe 2019-05-12 jcs err = got_error_prefix_errno("stat");
2265 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
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");
2271 33ad4cbe 2019-05-12 jcs /* remove comments */
2272 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2275 33ad4cbe 2019-05-12 jcs fp = fopen(tmpfile, "r");
2276 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2277 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2279 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2281 33ad4cbe 2019-05-12 jcs fclose(fp);
2283 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2284 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2288 33ad4cbe 2019-05-12 jcs if (len == 0) {
2289 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2290 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2297 33ad4cbe 2019-05-12 jcs if (tmpfile) {
2298 33ad4cbe 2019-05-12 jcs unlink(tmpfile);
2299 33ad4cbe 2019-05-12 jcs free(tmpfile);
2302 33ad4cbe 2019-05-12 jcs return err;
2305 c4296144 2019-05-09 stsp static const struct got_error *
2306 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2308 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2309 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2310 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2311 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2312 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2313 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2314 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2317 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2318 c4296144 2019-05-09 stsp switch (ch) {
2320 c4296144 2019-05-09 stsp logmsg = optarg;
2323 c4296144 2019-05-09 stsp usage_commit();
2324 c4296144 2019-05-09 stsp /* NOTREACHED */
2328 c4296144 2019-05-09 stsp argc -= optind;
2329 c4296144 2019-05-09 stsp argv += optind;
2331 c4296144 2019-05-09 stsp if (argc == 1) {
2332 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2333 c4296144 2019-05-09 stsp if (path == NULL) {
2334 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath", argv[0]);
2335 c4296144 2019-05-09 stsp goto done;
2337 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2338 c4296144 2019-05-09 stsp } else if (argc != 0)
2339 c4296144 2019-05-09 stsp usage_commit();
2341 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2342 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2343 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2344 35bd8fed 2019-05-09 stsp goto done;
2347 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2348 c4296144 2019-05-09 stsp if (cwd == NULL) {
2349 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
2350 c4296144 2019-05-09 stsp goto done;
2352 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2353 c4296144 2019-05-09 stsp if (error)
2354 c4296144 2019-05-09 stsp goto done;
2356 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2357 c4296144 2019-05-09 stsp if (error != NULL)
2358 c4296144 2019-05-09 stsp goto done;
2361 c4296144 2019-05-09 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2362 a0937847 2019-05-11 stsp got_worktree_get_root_path(worktree), 0);
2363 c4296144 2019-05-09 stsp if (error)
2364 c4296144 2019-05-09 stsp goto done;
2367 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2368 33ad4cbe 2019-05-12 jcs collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2369 c4296144 2019-05-09 stsp if (error)
2370 c4296144 2019-05-09 stsp goto done;
2372 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2373 c4296144 2019-05-09 stsp if (error)
2374 c4296144 2019-05-09 stsp goto done;
2375 c4296144 2019-05-09 stsp printf("created commit %s\n", id_str);
2378 c4296144 2019-05-09 stsp got_repo_close(repo);
2379 c4296144 2019-05-09 stsp if (worktree)
2380 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2381 c4296144 2019-05-09 stsp free(path);
2382 c4296144 2019-05-09 stsp free(cwd);
2383 c4296144 2019-05-09 stsp free(id_str);
2384 c4296144 2019-05-09 stsp return error;