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"
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 const struct got_error *(*cmd_main)(int, char *[]);
42 void (*cmd_usage)(void);
43 const char *cmd_descr;
46 __dead void usage(void);
47 __dead void usage_checkout(void);
48 __dead void usage_log(void);
50 const struct got_error* cmd_checkout(int, char *[]);
51 const struct got_error* cmd_log(int, char *[]);
52 const struct got_error* cmd_status(int, char *[]);
54 struct cmd got_commands[] = {
55 { "checkout", cmd_checkout, usage_checkout,
56 "check out a new work tree from a repository" },
57 { "log", cmd_log, usage_log,
58 "show repository history" },
60 { "status", cmd_status, usage_status,
61 "show modification status of files" },
66 main(int argc, char *argv[])
73 setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "h")) != -1) {
92 for (i = 0; i < nitems(got_commands); i++) {
93 const struct got_error *error;
95 cmd = &got_commands[i];
97 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
101 got_commands[i].cmd_usage();
103 error = got_commands[i].cmd_main(argc, argv);
105 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
112 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
121 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
122 "Available commands:\n", getprogname());
123 for (i = 0; i < nitems(got_commands); i++) {
124 struct cmd *cmd = &got_commands[i];
125 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
133 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
134 "[worktree-path]\n", getprogname());
139 checkout_progress(void *arg, const char *path)
141 char *worktree_path = arg;
143 while (path[0] == '/')
146 printf("A %s/%s\n", worktree_path, path);
149 const struct got_error *
150 cmd_checkout(int argc, char *argv[])
152 const struct got_error *error = NULL;
153 struct got_repository *repo = NULL;
154 struct got_reference *head_ref = NULL;
155 struct got_worktree *worktree = NULL;
156 char *repo_path = NULL;
157 char *worktree_path = NULL;
158 const char *path_prefix = "";
162 while ((ch = getopt(argc, argv, "p:")) != -1) {
165 path_prefix = optarg;
177 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
181 char *cwd, *base, *dotgit;
183 cwd = getcwd(NULL, 0);
186 base = basename(repo_path);
189 dotgit = strstr(base, ".git");
192 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 return got_error(GOT_ERR_NO_MEM);
197 } else if (argc == 2) {
199 worktree_path = strdup(argv[1]);
200 if (worktree_path == NULL)
201 return got_error(GOT_ERR_NO_MEM);
205 error = got_repo_open(&repo, repo_path);
208 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
212 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
216 error = got_worktree_open(&worktree, worktree_path);
220 error = got_worktree_checkout_files(worktree, head_ref, repo,
221 checkout_progress, worktree_path);
225 printf("checked out %s\n", worktree_path);
232 static const struct got_error *
233 print_commit_object(struct got_object *, struct got_object_id *,
234 struct got_repository *);
236 static const struct got_error *
237 print_parent_commits(struct got_commit_object *commit,
238 struct got_repository *repo)
240 struct got_parent_id *pid;
241 const struct got_error *err = NULL;
242 struct got_object *obj;
244 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
245 err = got_object_open(&obj, repo, pid->id);
248 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
249 return got_error(GOT_ERR_OBJ_TYPE);
250 err = print_commit_object(obj, pid->id, repo);
251 got_object_close(obj);
257 static const struct got_error *
258 print_commit_object(struct got_object *obj, struct got_object_id *id,
259 struct got_repository *repo)
261 struct got_commit_object *commit;
263 const struct got_error *err;
265 err = got_object_commit_open(&commit, repo, obj);
269 err = got_object_id_str(&buf, id);
273 printf("-----------------------------------------------\n");
274 printf("commit: %s\n", buf);
275 printf("Author: %s\n", commit->author);
276 printf("\n%s\n", commit->logmsg);
280 err = print_parent_commits(commit, repo);
281 got_object_commit_close(commit);
288 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
292 const struct got_error *
293 cmd_log(int argc, char *argv[])
295 const struct got_error *error;
296 struct got_repository *repo;
297 struct got_reference *head_ref;
298 struct got_object_id *id;
299 struct got_object *obj;
300 char *repo_path = NULL;
303 if (pledge("stdio rpath wpath cpath", NULL) == -1)
307 repo_path = getcwd(NULL, 0);
308 if (repo_path == NULL)
310 } else if (argc == 2)
315 error = got_repo_open(&repo, repo_path);
318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
321 error = got_ref_resolve(&id, repo, head_ref);
325 error = got_object_open(&obj, repo, id);
328 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
329 error = print_commit_object(obj, id, repo);
333 return got_error(GOT_ERR_OBJ_TYPE);
334 got_object_close(obj);
336 got_ref_close(head_ref);
337 got_repo_close(repo);
342 const struct got_error *
343 cmd_status(int argc __unused, char *argv[] __unused)
345 git_repository *repo = NULL;
346 git_status_list *status;
347 git_status_options statusopts;
352 if (git_repository_open_ext(&repo, ".", 0, NULL))
353 errx(1, "git_repository_open: %s", giterr_last()->message);
355 if (git_repository_is_bare(repo))
356 errx(1, "bar repository");
358 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
359 errx(1, "git_status_init_options: %s", giterr_last()->message);
361 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
362 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
363 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
364 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
366 if (git_status_list_new(&status, repo, &statusopts))
367 errx(1, "git_status_list_new: %s", giterr_last()->message);
369 for (i = 0; i < git_status_list_entrycount(status); i++) {
370 const git_status_entry *se;
372 se = git_status_byindex(status, i);
373 switch (se->status) {
374 case GIT_STATUS_WT_NEW:
375 printf("? %s\n", se->index_to_workdir->new_file.path);
377 case GIT_STATUS_WT_MODIFIED:
378 printf("M %s\n", se->index_to_workdir->new_file.path);
380 case GIT_STATUS_WT_DELETED:
381 printf("R %s\n", se->index_to_workdir->new_file.path);
383 case GIT_STATUS_WT_RENAMED:
384 printf("m %s -> %s\n",
385 se->index_to_workdir->old_file.path,
386 se->index_to_workdir->new_file.path);
388 case GIT_STATUS_WT_TYPECHANGE:
389 printf("t %s\n", se->index_to_workdir->new_file.path);
391 case GIT_STATUS_INDEX_NEW:
392 printf("A %s\n", se->head_to_index->new_file.path);
394 case GIT_STATUS_INDEX_MODIFIED:
395 printf("M %s\n", se->head_to_index->old_file.path);
397 case GIT_STATUS_INDEX_DELETED:
398 printf("R %s\n", se->head_to_index->old_file.path);
400 case GIT_STATUS_INDEX_RENAMED:
401 printf("m %s -> %s\n",
402 se->head_to_index->old_file.path,
403 se->head_to_index->new_file.path);
405 case GIT_STATUS_INDEX_TYPECHANGE:
406 printf("t %s\n", se->head_to_index->old_file.path);
408 case GIT_STATUS_CURRENT:
414 git_status_list_free(status);
415 git_repository_free(repo);
416 git_libgit2_shutdown();