2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 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>
21 5c860e29 2018-03-12 stsp #include <err.h>
22 5c860e29 2018-03-12 stsp #include <errno.h>
23 5c860e29 2018-03-12 stsp #include <locale.h>
24 5c860e29 2018-03-12 stsp #include <stdio.h>
25 5c860e29 2018-03-12 stsp #include <stdlib.h>
26 5c860e29 2018-03-12 stsp #include <string.h>
27 5c860e29 2018-03-12 stsp #include <unistd.h>
28 c09a553d 2018-03-12 stsp #include <libgen.h>
30 f42b1b34 2018-03-12 stsp #include "got_error.h"
31 f42b1b34 2018-03-12 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_reference.h"
33 f42b1b34 2018-03-12 stsp #include "got_repository.h"
34 c09a553d 2018-03-12 stsp #include "got_worktree.h"
35 79109fed 2018-03-27 stsp #include "got_diff.h"
36 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
38 5c860e29 2018-03-12 stsp #ifndef nitems
39 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 5c860e29 2018-03-12 stsp struct cmd {
43 5c860e29 2018-03-12 stsp const char *cmd_name;
44 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
45 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
46 46a0db7d 2018-03-12 stsp const char *cmd_descr;
49 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
50 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
51 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
52 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
54 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
55 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
56 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
57 4ed7e80c 2018-05-20 stsp #ifdef notyet
58 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
61 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
62 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
63 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
64 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
65 1b6b95a8 2018-03-12 stsp "show repository history" },
66 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
67 b00d56cd 2018-04-01 stsp "compare files and directories" },
68 f42b1b34 2018-03-12 stsp #ifdef notyet
69 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
70 1b6b95a8 2018-03-12 stsp "show modification status of files" },
75 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
77 5c860e29 2018-03-12 stsp struct cmd *cmd;
78 5c860e29 2018-03-12 stsp unsigned int i;
80 1b6b95a8 2018-03-12 stsp int hflag = 0;
82 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
84 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
85 5c860e29 2018-03-12 stsp switch (ch) {
91 5c860e29 2018-03-12 stsp /* NOTREACHED */
95 5c860e29 2018-03-12 stsp argc -= optind;
96 5c860e29 2018-03-12 stsp argv += optind;
99 5c860e29 2018-03-12 stsp if (argc <= 0)
102 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
103 d7d4f210 2018-03-12 stsp const struct got_error *error;
105 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
107 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
111 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
113 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
114 d7d4f210 2018-03-12 stsp if (error) {
115 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
122 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
126 4ed7e80c 2018-05-20 stsp __dead static void
127 5c860e29 2018-03-12 stsp usage(void)
131 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
132 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
133 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
134 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
135 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
140 4ed7e80c 2018-05-20 stsp __dead static void
141 c09a553d 2018-03-12 stsp usage_checkout(void)
143 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
148 92a684f4 2018-03-12 stsp static void
149 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
151 92a684f4 2018-03-12 stsp char *worktree_path = arg;
153 92a684f4 2018-03-12 stsp while (path[0] == '/')
156 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
159 4ed7e80c 2018-05-20 stsp static const struct got_error *
160 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
162 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
163 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
164 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
165 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
166 c09a553d 2018-03-12 stsp char *repo_path = NULL;
167 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
168 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
171 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
172 0bb8a95e 2018-03-12 stsp switch (ch) {
174 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
178 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
182 0bb8a95e 2018-03-12 stsp argc -= optind;
183 0bb8a95e 2018-03-12 stsp argv += optind;
185 6715a751 2018-03-16 stsp #ifndef PROFILE
186 2178c42e 2018-04-22 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
187 c09a553d 2018-03-12 stsp err(1, "pledge");
189 0bb8a95e 2018-03-12 stsp if (argc == 1) {
190 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
191 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
192 76089277 2018-04-01 stsp if (repo_path == NULL)
193 76089277 2018-04-01 stsp return got_error_from_errno();
194 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
195 76089277 2018-04-01 stsp if (cwd == NULL) {
196 76089277 2018-04-01 stsp error = got_error_from_errno();
199 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
200 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
202 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
203 76089277 2018-04-01 stsp if (base == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
207 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
208 c09a553d 2018-03-12 stsp if (dotgit)
209 c09a553d 2018-03-12 stsp *dotgit = '\0';
210 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
216 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
217 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
218 76089277 2018-04-01 stsp if (repo_path == NULL) {
219 76089277 2018-04-01 stsp error = got_error_from_errno();
222 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
223 76089277 2018-04-01 stsp if (worktree_path == NULL) {
224 76089277 2018-04-01 stsp error = got_error_from_errno();
228 c09a553d 2018-03-12 stsp usage_checkout();
230 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
231 c09a553d 2018-03-12 stsp if (error != NULL)
233 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
234 c09a553d 2018-03-12 stsp if (error != NULL)
237 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
238 c09a553d 2018-03-12 stsp if (error != NULL)
241 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
242 c09a553d 2018-03-12 stsp if (error != NULL)
245 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
246 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
247 c09a553d 2018-03-12 stsp if (error != NULL)
250 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
251 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
254 76089277 2018-04-01 stsp free(repo_path);
255 c09a553d 2018-03-12 stsp free(worktree_path);
256 c09a553d 2018-03-12 stsp return error;
259 f42b1b34 2018-03-12 stsp static const struct got_error *
260 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
261 f42b1b34 2018-03-12 stsp struct got_repository *repo)
263 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
264 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
265 79109fed 2018-03-27 stsp struct got_object *obj;
266 79109fed 2018-03-27 stsp struct got_parent_id *pid;
268 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
270 79109fed 2018-03-27 stsp return err;
272 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
273 79109fed 2018-03-27 stsp got_object_close(obj);
275 79109fed 2018-03-27 stsp return err;
277 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 79109fed 2018-03-27 stsp if (pid != NULL) {
279 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
281 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
283 79109fed 2018-03-27 stsp return err;
285 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
286 79109fed 2018-03-27 stsp got_object_close(obj);
288 79109fed 2018-03-27 stsp return err;
290 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
291 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
293 79109fed 2018-03-27 stsp return err;
294 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
295 79109fed 2018-03-27 stsp got_object_close(obj);
297 79109fed 2018-03-27 stsp return err;
300 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
302 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
303 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
304 79109fed 2018-03-27 stsp return err;
307 79109fed 2018-03-27 stsp static const struct got_error *
308 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
309 79109fed 2018-03-27 stsp struct got_repository *repo, int show_patch)
311 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
314 8bf5b3c9 2018-03-17 stsp err = got_object_id_str(&buf, id);
316 8bf5b3c9 2018-03-17 stsp return err;
318 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
319 b65ae19a 2018-04-24 stsp printf("commit %s\n", buf);
320 b65ae19a 2018-04-24 stsp printf("author: %s\n", commit->author);
321 f3d135e1 2018-04-01 stsp if (strcmp(commit->author, commit->committer) != 0)
322 b65ae19a 2018-04-24 stsp printf("committer: %s\n", commit->committer);
323 8bf5b3c9 2018-03-17 stsp printf("\n%s\n", commit->logmsg);
325 971751ac 2018-03-27 stsp if (show_patch) {
326 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
327 971751ac 2018-03-27 stsp if (err == 0)
328 971751ac 2018-03-27 stsp printf("\n");
332 79109fed 2018-03-27 stsp return err;
335 f42b1b34 2018-03-12 stsp static const struct got_error *
336 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
337 64a96a6d 2018-04-01 stsp struct got_repository *repo, int show_patch, int limit)
339 f42b1b34 2018-03-12 stsp const struct got_error *err;
340 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
341 372ccdbb 2018-06-10 stsp int ncommits;
343 372ccdbb 2018-06-10 stsp err = got_commit_graph_open(&graph, root_id, repo);
345 f42b1b34 2018-03-12 stsp return err;
346 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
348 0a585a0d 2018-03-17 stsp return err;
350 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
351 372ccdbb 2018-06-10 stsp struct got_object_id *id;
353 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_next(&commit, &id, graph);
355 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
357 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
358 372ccdbb 2018-06-10 stsp graph, 1, repo);
364 372ccdbb 2018-06-10 stsp if (commit == NULL)
366 372ccdbb 2018-06-10 stsp err = print_commit(commit, id, repo, show_patch);
367 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
369 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
371 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
372 f42b1b34 2018-03-12 stsp return err;
375 4ed7e80c 2018-05-20 stsp __dead static void
376 6f3d1eb0 2018-03-12 stsp usage_log(void)
378 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
379 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
383 4ed7e80c 2018-05-20 stsp static const struct got_error *
384 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
386 f42b1b34 2018-03-12 stsp const struct got_error *error;
387 f42b1b34 2018-03-12 stsp struct got_repository *repo;
388 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
389 f42b1b34 2018-03-12 stsp struct got_object *obj;
390 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
391 d142fc45 2018-04-01 stsp char *start_commit = NULL;
393 64a96a6d 2018-04-01 stsp int show_patch = 0, limit = 0;
394 64a96a6d 2018-04-01 stsp const char *errstr;
396 6715a751 2018-03-16 stsp #ifndef PROFILE
397 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
398 f42b1b34 2018-03-12 stsp err(1, "pledge");
401 64a96a6d 2018-04-01 stsp while ((ch = getopt(argc, argv, "pc:l:")) != -1) {
402 79109fed 2018-03-27 stsp switch (ch) {
404 79109fed 2018-03-27 stsp show_patch = 1;
407 d142fc45 2018-04-01 stsp start_commit = optarg;
410 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
411 64a96a6d 2018-04-01 stsp if (errstr != NULL)
412 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
416 79109fed 2018-03-27 stsp /* NOTREACHED */
420 79109fed 2018-03-27 stsp argc -= optind;
421 79109fed 2018-03-27 stsp argv += optind;
423 79109fed 2018-03-27 stsp if (argc == 0) {
424 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
425 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
426 e1e3f570 2018-04-01 stsp return got_error_from_errno();
427 3235492e 2018-04-01 stsp } else if (argc == 1) {
428 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
429 76089277 2018-04-01 stsp if (repo_path == NULL)
430 76089277 2018-04-01 stsp return got_error_from_errno();
432 6f3d1eb0 2018-03-12 stsp usage_log();
434 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
435 76089277 2018-04-01 stsp free(repo_path);
436 d7d4f210 2018-03-12 stsp if (error != NULL)
437 d7d4f210 2018-03-12 stsp return error;
439 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
440 3235492e 2018-04-01 stsp struct got_reference *head_ref;
441 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
442 3235492e 2018-04-01 stsp if (error != NULL)
443 3235492e 2018-04-01 stsp return error;
444 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
445 3235492e 2018-04-01 stsp got_ref_close(head_ref);
446 3235492e 2018-04-01 stsp if (error != NULL)
447 3235492e 2018-04-01 stsp return error;
448 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
450 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
451 3235492e 2018-04-01 stsp if (error == NULL) {
452 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
453 3235492e 2018-04-01 stsp if (id == NULL)
454 3235492e 2018-04-01 stsp error = got_error_from_errno();
457 d7d4f210 2018-03-12 stsp if (error != NULL)
458 d7d4f210 2018-03-12 stsp return error;
459 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
460 64a96a6d 2018-04-01 stsp error = print_commits(obj, id, repo, show_patch, limit);
462 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
463 f42b1b34 2018-03-12 stsp got_object_close(obj);
465 f42b1b34 2018-03-12 stsp got_repo_close(repo);
466 8bf5b3c9 2018-03-17 stsp return error;
469 4ed7e80c 2018-05-20 stsp __dead static void
470 3f8b7d6a 2018-04-01 stsp usage_diff(void)
472 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
473 3f8b7d6a 2018-04-01 stsp getprogname());
477 4ed7e80c 2018-05-20 stsp static const struct got_error *
478 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
480 b00d56cd 2018-04-01 stsp const struct got_error *error;
481 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
482 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
483 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
484 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
485 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
488 b00d56cd 2018-04-01 stsp #ifndef PROFILE
489 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
490 b00d56cd 2018-04-01 stsp err(1, "pledge");
493 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
494 b00d56cd 2018-04-01 stsp switch (ch) {
497 b00d56cd 2018-04-01 stsp /* NOTREACHED */
501 b00d56cd 2018-04-01 stsp argc -= optind;
502 b00d56cd 2018-04-01 stsp argv += optind;
504 b00d56cd 2018-04-01 stsp if (argc == 0) {
505 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
506 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
507 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
508 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
509 e1e3f570 2018-04-01 stsp return got_error_from_errno();
510 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
511 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
512 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
513 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
514 76089277 2018-04-01 stsp if (repo_path == NULL)
515 76089277 2018-04-01 stsp return got_error_from_errno();
516 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
517 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
519 b00d56cd 2018-04-01 stsp usage_diff();
521 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
522 76089277 2018-04-01 stsp free(repo_path);
523 b00d56cd 2018-04-01 stsp if (error != NULL)
526 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
527 b00d56cd 2018-04-01 stsp if (error == NULL) {
528 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
529 b00d56cd 2018-04-01 stsp if (id1 == NULL)
530 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
532 b00d56cd 2018-04-01 stsp if (error != NULL)
535 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
536 b00d56cd 2018-04-01 stsp if (error == NULL) {
537 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
538 b00d56cd 2018-04-01 stsp if (id2 == NULL)
539 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
541 b00d56cd 2018-04-01 stsp if (error != NULL)
544 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
545 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
549 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
550 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
551 11528a82 2018-05-19 stsp error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
553 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
554 11528a82 2018-05-19 stsp error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
556 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
557 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
560 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
565 b00d56cd 2018-04-01 stsp got_object_close(obj1);
567 b00d56cd 2018-04-01 stsp got_object_close(obj2);
573 b00d56cd 2018-04-01 stsp got_repo_close(repo);
574 b00d56cd 2018-04-01 stsp return error;
577 f42b1b34 2018-03-12 stsp #ifdef notyet
578 4ed7e80c 2018-05-20 stsp static const struct got_error *
579 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
581 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
582 5c860e29 2018-03-12 stsp git_status_list *status;
583 5c860e29 2018-03-12 stsp git_status_options statusopts;
586 5c860e29 2018-03-12 stsp git_libgit2_init();
588 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
589 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
591 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
592 5c860e29 2018-03-12 stsp errx(1, "bar repository");
594 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
595 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
597 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
598 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
599 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
600 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
602 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
603 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
605 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
606 5c860e29 2018-03-12 stsp const git_status_entry *se;
608 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
609 5c860e29 2018-03-12 stsp switch (se->status) {
610 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
611 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
613 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
614 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
616 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
617 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
619 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
620 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
621 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
622 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
624 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
625 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
627 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
628 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
630 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
631 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
633 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
634 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
636 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
637 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
638 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
639 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
641 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
642 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
644 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
650 5c860e29 2018-03-12 stsp git_status_list_free(status);
651 5c860e29 2018-03-12 stsp git_repository_free(repo);
652 5c860e29 2018-03-12 stsp git_libgit2_shutdown();