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 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 REPO_PATH [WORKTREE_PATH]\n",
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;
159 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
163 char *cwd, *base, *dotgit;
165 cwd = getcwd(NULL, 0);
168 base = basename(repo_path);
171 dotgit = strstr(base, ".git");
174 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
176 return got_error(GOT_ERR_NO_MEM);
179 } else if (argc == 3) {
181 worktree_path = strdup(argv[2]);
182 if (worktree_path == NULL)
183 return got_error(GOT_ERR_NO_MEM);
187 error = got_repo_open(&repo, repo_path);
190 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
194 error = got_worktree_init(worktree_path, head_ref, "/", repo);
198 error = got_worktree_open(&worktree, worktree_path);
202 error = got_worktree_checkout_files(worktree, head_ref, repo,
203 checkout_progress, worktree_path);
207 printf("checked out %s\n", worktree_path);
214 static const struct got_error *
215 print_commit_object(struct got_object *, struct got_object_id *,
216 struct got_repository *);
218 static const struct got_error *
219 print_parent_commits(struct got_commit_object *commit,
220 struct got_repository *repo)
222 struct got_parent_id *pid;
223 const struct got_error *err = NULL;
224 struct got_object *obj;
226 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
227 err = got_object_open(&obj, repo, pid->id);
230 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
232 err = print_commit_object(obj, pid->id, repo);
233 got_object_close(obj);
239 static const struct got_error *
240 print_commit_object(struct got_object *obj, struct got_object_id *id,
241 struct got_repository *repo)
243 struct got_commit_object *commit;
245 const struct got_error *err;
247 err = got_object_commit_open(&commit, repo, obj);
251 err = got_object_id_str(&buf, id);
255 printf("-----------------------------------------------\n");
256 printf("commit: %s\n", buf);
257 printf("Author: %s\n", commit->author);
258 printf("\n%s\n", commit->logmsg);
262 err = print_parent_commits(commit, repo);
263 got_object_commit_close(commit);
270 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
274 const struct got_error *
275 cmd_log(int argc, char *argv[])
277 const struct got_error *error;
278 struct got_repository *repo;
279 struct got_reference *head_ref;
280 struct got_object_id *id;
281 struct got_object *obj;
282 char *repo_path = NULL;
284 if (pledge("stdio rpath wpath cpath", NULL) == -1)
288 repo_path = getcwd(NULL, 0);
289 if (repo_path == NULL)
291 } else if (argc == 2)
296 error = got_repo_open(&repo, repo_path);
299 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
302 error = got_ref_resolve(&id, repo, head_ref);
306 error = got_object_open(&obj, repo, id);
309 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
310 error = print_commit_object(obj, id, repo);
314 return got_error(GOT_ERR_OBJ_TYPE);
315 got_object_close(obj);
317 got_ref_close(head_ref);
318 got_repo_close(repo);
323 const struct got_error *
324 cmd_status(int argc __unused, char *argv[] __unused)
326 git_repository *repo = NULL;
327 git_status_list *status;
328 git_status_options statusopts;
333 if (git_repository_open_ext(&repo, ".", 0, NULL))
334 errx(1, "git_repository_open: %s", giterr_last()->message);
336 if (git_repository_is_bare(repo))
337 errx(1, "bar repository");
339 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
340 errx(1, "git_status_init_options: %s", giterr_last()->message);
342 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
343 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
344 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
345 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
347 if (git_status_list_new(&status, repo, &statusopts))
348 errx(1, "git_status_list_new: %s", giterr_last()->message);
350 for (i = 0; i < git_status_list_entrycount(status); i++) {
351 const git_status_entry *se;
353 se = git_status_byindex(status, i);
354 switch (se->status) {
355 case GIT_STATUS_WT_NEW:
356 printf("? %s\n", se->index_to_workdir->new_file.path);
358 case GIT_STATUS_WT_MODIFIED:
359 printf("M %s\n", se->index_to_workdir->new_file.path);
361 case GIT_STATUS_WT_DELETED:
362 printf("R %s\n", se->index_to_workdir->new_file.path);
364 case GIT_STATUS_WT_RENAMED:
365 printf("m %s -> %s\n",
366 se->index_to_workdir->old_file.path,
367 se->index_to_workdir->new_file.path);
369 case GIT_STATUS_WT_TYPECHANGE:
370 printf("t %s\n", se->index_to_workdir->new_file.path);
372 case GIT_STATUS_INDEX_NEW:
373 printf("A %s\n", se->head_to_index->new_file.path);
375 case GIT_STATUS_INDEX_MODIFIED:
376 printf("M %s\n", se->head_to_index->old_file.path);
378 case GIT_STATUS_INDEX_DELETED:
379 printf("R %s\n", se->head_to_index->old_file.path);
381 case GIT_STATUS_INDEX_RENAMED:
382 printf("m %s -> %s\n",
383 se->head_to_index->old_file.path,
384 se->head_to_index->new_file.path);
386 case GIT_STATUS_INDEX_TYPECHANGE:
387 printf("t %s\n", se->head_to_index->old_file.path);
389 case GIT_STATUS_CURRENT:
395 git_status_list_free(status);
396 git_repository_free(repo);
397 git_libgit2_shutdown();