2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 const struct got_error *(*cmd_main)(int, char *[]);
43 void (*cmd_usage)(void);
44 const char *cmd_descr;
47 __dead void usage(void);
48 __dead void usage_checkout(void);
49 __dead void usage_log(void);
51 const struct got_error* cmd_checkout(int, char *[]);
52 const struct got_error* cmd_log(int, char *[]);
53 const struct got_error* cmd_status(int, char *[]);
55 struct cmd got_commands[] = {
56 { "checkout", cmd_checkout, usage_checkout,
57 "check out a new work tree from a repository" },
58 { "log", cmd_log, usage_log,
59 "show repository history" },
61 { "status", cmd_status, usage_status,
62 "show modification status of files" },
67 main(int argc, char *argv[])
74 setlocale(LC_ALL, "");
76 while ((ch = getopt(argc, argv, "h")) != -1) {
94 for (i = 0; i < nitems(got_commands); i++) {
95 const struct got_error *error;
97 cmd = &got_commands[i];
99 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
103 got_commands[i].cmd_usage();
105 error = got_commands[i].cmd_main(argc, argv);
107 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
114 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
124 "Available commands:\n", getprogname());
125 for (i = 0; i < nitems(got_commands); i++) {
126 struct cmd *cmd = &got_commands[i];
127 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
135 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
136 "[worktree-path]\n", getprogname());
141 checkout_progress(void *arg, const char *path)
143 char *worktree_path = arg;
145 while (path[0] == '/')
148 printf("A %s/%s\n", worktree_path, path);
151 const struct got_error *
152 cmd_checkout(int argc, char *argv[])
154 const struct got_error *error = NULL;
155 struct got_repository *repo = NULL;
156 struct got_reference *head_ref = NULL;
157 struct got_worktree *worktree = NULL;
158 char *repo_path = NULL;
159 char *worktree_path = NULL;
160 const char *path_prefix = "";
163 while ((ch = getopt(argc, argv, "p:")) != -1) {
166 path_prefix = optarg;
178 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
182 char *cwd, *base, *dotgit;
184 cwd = getcwd(NULL, 0);
187 base = basename(repo_path);
190 dotgit = strstr(base, ".git");
193 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 error = got_error_from_errno();
199 } else if (argc == 2) {
201 worktree_path = strdup(argv[1]);
202 if (worktree_path == NULL)
203 return got_error_from_errno();
207 error = got_repo_open(&repo, repo_path);
210 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
214 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
218 error = got_worktree_open(&worktree, worktree_path);
222 error = got_worktree_checkout_files(worktree, head_ref, repo,
223 checkout_progress, worktree_path);
227 printf("checked out %s\n", worktree_path);
234 static const struct got_error *
235 print_patch(struct got_commit_object *commit, struct got_object_id *id,
236 struct got_repository *repo)
238 const struct got_error *err = NULL;
239 struct got_tree_object *tree1 = NULL, *tree2;
240 struct got_object *obj;
241 struct got_parent_id *pid;
243 err = got_object_open(&obj, repo, commit->tree_id);
247 err = got_object_tree_open(&tree2, repo, obj);
248 got_object_close(obj);
252 pid = SIMPLEQ_FIRST(&commit->parent_ids);
254 struct got_commit_object *pcommit;
256 err = got_object_open(&obj, repo, pid->id);
260 err = got_object_commit_open(&pcommit, repo, obj);
261 got_object_close(obj);
265 err = got_object_open(&obj, repo, pcommit->tree_id);
266 got_object_commit_close(pcommit);
269 err = got_object_tree_open(&tree1, repo, obj);
270 got_object_close(obj);
275 err = got_diff_tree(tree1, tree2, repo, stdout);
277 got_object_tree_close(tree1);
278 got_object_tree_close(tree2);
282 static const struct got_error *
283 print_commit(struct got_commit_object *commit, struct got_object_id *id,
284 struct got_repository *repo, int show_patch)
286 const struct got_error *err = NULL;
289 err = got_object_id_str(&buf, id);
293 printf("-----------------------------------------------\n");
294 printf("commit: %s\n", buf);
295 printf("Author: %s\n", commit->author);
296 printf("\n%s\n", commit->logmsg);
299 err = print_patch(commit, id, repo);
308 struct commit_queue_entry {
309 TAILQ_ENTRY(commit_queue_entry) entry;
310 struct got_object_id *id;
311 struct got_commit_object *commit;
314 static const struct got_error *
315 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
316 struct got_repository *repo, int show_patch)
318 const struct got_error *err;
319 struct got_commit_object *root_commit;
320 TAILQ_HEAD(, commit_queue_entry) commits;
321 struct commit_queue_entry *entry;
323 TAILQ_INIT(&commits);
325 err = got_object_commit_open(&root_commit, repo, root_obj);
329 entry = calloc(1, sizeof(*entry));
331 return got_error_from_errno();
332 entry->id = got_object_id_dup(root_id);
333 if (entry->id == NULL) {
334 err = got_error_from_errno();
338 entry->commit = root_commit;
339 TAILQ_INSERT_HEAD(&commits, entry, entry);
341 while (!TAILQ_EMPTY(&commits)) {
342 struct got_parent_id *pid;
344 entry = TAILQ_FIRST(&commits);
345 err = print_commit(entry->commit, entry->id, repo, show_patch);
347 while (!TAILQ_EMPTY(&commits)) {
348 entry = TAILQ_FIRST(&commits);
349 TAILQ_REMOVE(&commits, entry, entry);
350 got_object_commit_close(entry->commit);
357 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
358 struct got_object *obj;
359 struct got_commit_object *pcommit;
360 struct commit_queue_entry *pentry;
362 err = got_object_open(&obj, repo, pid->id);
365 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
370 err = got_object_commit_open(&pcommit, repo, obj);
371 got_object_close(obj);
375 pentry = calloc(1, sizeof(*pentry));
376 if (pentry == NULL) {
377 err = got_error_from_errno();
378 got_object_commit_close(pcommit);
381 pentry->id = got_object_id_dup(pid->id);
382 if (pentry->id == NULL) {
383 err = got_error_from_errno();
384 got_object_commit_close(pcommit);
387 pentry->commit = pcommit;
388 TAILQ_INSERT_TAIL(&commits, pentry, entry);
391 TAILQ_REMOVE(&commits, entry, entry);
392 got_object_commit_close(entry->commit);
403 fprintf(stderr, "usage: %s log [-p] [repository-path] [commit]\n",
408 const struct got_error *
409 cmd_log(int argc, char *argv[])
411 const struct got_error *error;
412 struct got_repository *repo;
413 struct got_object_id *id = NULL;
414 struct got_object *obj;
415 char *repo_path = NULL;
416 char *commit_id_str = NULL;
421 if (pledge("stdio rpath wpath cpath", NULL) == -1)
425 while ((ch = getopt(argc, argv, "p")) != -1) {
440 repo_path = getcwd(NULL, 0);
441 if (repo_path == NULL)
443 } else if (argc == 1) {
445 } else if (argc == 2) {
447 commit_id_str = argv[1];
451 error = got_repo_open(&repo, repo_path);
455 if (commit_id_str == NULL) {
456 struct got_reference *head_ref;
457 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
460 error = got_ref_resolve(&id, repo, head_ref);
461 got_ref_close(head_ref);
464 error = got_object_open(&obj, repo, id);
466 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
468 id = got_object_get_id(obj);
470 error = got_error_from_errno();
475 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
476 error = print_commits(obj, id, repo, show_patch);
478 error = got_error(GOT_ERR_OBJ_TYPE);
479 got_object_close(obj);
481 got_repo_close(repo);
486 const struct got_error *
487 cmd_status(int argc __unused, char *argv[] __unused)
489 git_repository *repo = NULL;
490 git_status_list *status;
491 git_status_options statusopts;
496 if (git_repository_open_ext(&repo, ".", 0, NULL))
497 errx(1, "git_repository_open: %s", giterr_last()->message);
499 if (git_repository_is_bare(repo))
500 errx(1, "bar repository");
502 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
503 errx(1, "git_status_init_options: %s", giterr_last()->message);
505 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
506 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
507 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
508 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
510 if (git_status_list_new(&status, repo, &statusopts))
511 errx(1, "git_status_list_new: %s", giterr_last()->message);
513 for (i = 0; i < git_status_list_entrycount(status); i++) {
514 const git_status_entry *se;
516 se = git_status_byindex(status, i);
517 switch (se->status) {
518 case GIT_STATUS_WT_NEW:
519 printf("? %s\n", se->index_to_workdir->new_file.path);
521 case GIT_STATUS_WT_MODIFIED:
522 printf("M %s\n", se->index_to_workdir->new_file.path);
524 case GIT_STATUS_WT_DELETED:
525 printf("R %s\n", se->index_to_workdir->new_file.path);
527 case GIT_STATUS_WT_RENAMED:
528 printf("m %s -> %s\n",
529 se->index_to_workdir->old_file.path,
530 se->index_to_workdir->new_file.path);
532 case GIT_STATUS_WT_TYPECHANGE:
533 printf("t %s\n", se->index_to_workdir->new_file.path);
535 case GIT_STATUS_INDEX_NEW:
536 printf("A %s\n", se->head_to_index->new_file.path);
538 case GIT_STATUS_INDEX_MODIFIED:
539 printf("M %s\n", se->head_to_index->old_file.path);
541 case GIT_STATUS_INDEX_DELETED:
542 printf("R %s\n", se->head_to_index->old_file.path);
544 case GIT_STATUS_INDEX_RENAMED:
545 printf("m %s -> %s\n",
546 se->head_to_index->old_file.path,
547 se->head_to_index->new_file.path);
549 case GIT_STATUS_INDEX_TYPECHANGE:
550 printf("t %s\n", se->head_to_index->old_file.path);
552 case GIT_STATUS_CURRENT:
558 git_status_list_free(status);
559 git_repository_free(repo);
560 git_libgit2_shutdown();