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",
138 const struct got_error *
139 cmd_checkout(int argc, char *argv[])
141 const struct got_error *error = NULL;
142 struct got_repository *repo = NULL;
143 struct got_reference *head_ref = NULL;
144 struct got_worktree *worktree = NULL;
145 char *repo_path = NULL;
146 char *worktree_path = NULL;
148 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
152 char *cwd, *base, *dotgit;
154 cwd = getcwd(NULL, 0);
157 base = basename(repo_path);
160 dotgit = strstr(base, ".git");
163 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
165 return got_error(GOT_ERR_NO_MEM);
168 } else if (argc == 3) {
170 worktree_path = strdup(argv[2]);
171 if (worktree_path == NULL)
172 return got_error(GOT_ERR_NO_MEM);
176 printf("%s %s %s %s\n", getprogname(), argv[0], repo_path, worktree_path);
178 error = got_repo_open(&repo, repo_path);
181 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
185 error = got_worktree_init(worktree_path, head_ref, "/", repo);
189 error = got_worktree_open(&worktree, worktree_path);
193 error = got_worktree_checkout_files(worktree, head_ref, repo);
197 printf("checked out %s\n", worktree_path);
204 static const struct got_error *
205 print_commit_object(struct got_object *, struct got_object_id *,
206 struct got_repository *);
208 static const struct got_error *
209 print_parent_commits(struct got_commit_object *commit,
210 struct got_repository *repo)
212 struct got_parent_id *pid;
213 const struct got_error *err = NULL;
214 struct got_object *obj;
216 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
217 err = got_object_open(&obj, repo, pid->id);
220 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
221 return got_error(GOT_ERR_OBJ_TYPE);
222 err = print_commit_object(obj, pid->id, repo);
223 got_object_close(obj);
229 static const struct got_error *
230 print_commit_object(struct got_object *obj, struct got_object_id *id,
231 struct got_repository *repo)
233 struct got_commit_object *commit;
235 const struct got_error *err;
237 err = got_object_commit_open(&commit, repo, obj);
241 err = got_object_id_str(&buf, id);
245 printf("-----------------------------------------------\n");
246 printf("commit: %s\n", buf);
247 printf("Author: %s\n", commit->author);
248 printf("\n%s\n", commit->logmsg);
252 err = print_parent_commits(commit, repo);
253 got_object_commit_close(commit);
260 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
264 const struct got_error *
265 cmd_log(int argc, char *argv[])
267 const struct got_error *error;
268 struct got_repository *repo;
269 struct got_reference *head_ref;
270 struct got_object_id *id;
271 struct got_object *obj;
272 char *repo_path = NULL;
274 if (pledge("stdio rpath wpath cpath", NULL) == -1)
278 repo_path = getcwd(NULL, 0);
279 if (repo_path == NULL)
281 } else if (argc == 2)
286 error = got_repo_open(&repo, repo_path);
289 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
292 error = got_ref_resolve(&id, repo, head_ref);
296 error = got_object_open(&obj, repo, id);
299 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
300 error = print_commit_object(obj, id, repo);
304 return got_error(GOT_ERR_OBJ_TYPE);
305 got_object_close(obj);
307 got_ref_close(head_ref);
308 got_repo_close(repo);
313 const struct got_error *
314 cmd_status(int argc __unused, char *argv[] __unused)
316 git_repository *repo = NULL;
317 git_status_list *status;
318 git_status_options statusopts;
323 if (git_repository_open_ext(&repo, ".", 0, NULL))
324 errx(1, "git_repository_open: %s", giterr_last()->message);
326 if (git_repository_is_bare(repo))
327 errx(1, "bar repository");
329 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
330 errx(1, "git_status_init_options: %s", giterr_last()->message);
332 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
333 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
334 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
335 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
337 if (git_status_list_new(&status, repo, &statusopts))
338 errx(1, "git_status_list_new: %s", giterr_last()->message);
340 for (i = 0; i < git_status_list_entrycount(status); i++) {
341 const git_status_entry *se;
343 se = git_status_byindex(status, i);
344 switch (se->status) {
345 case GIT_STATUS_WT_NEW:
346 printf("? %s\n", se->index_to_workdir->new_file.path);
348 case GIT_STATUS_WT_MODIFIED:
349 printf("M %s\n", se->index_to_workdir->new_file.path);
351 case GIT_STATUS_WT_DELETED:
352 printf("R %s\n", se->index_to_workdir->new_file.path);
354 case GIT_STATUS_WT_RENAMED:
355 printf("m %s -> %s\n",
356 se->index_to_workdir->old_file.path,
357 se->index_to_workdir->new_file.path);
359 case GIT_STATUS_WT_TYPECHANGE:
360 printf("t %s\n", se->index_to_workdir->new_file.path);
362 case GIT_STATUS_INDEX_NEW:
363 printf("A %s\n", se->head_to_index->new_file.path);
365 case GIT_STATUS_INDEX_MODIFIED:
366 printf("M %s\n", se->head_to_index->old_file.path);
368 case GIT_STATUS_INDEX_DELETED:
369 printf("R %s\n", se->head_to_index->old_file.path);
371 case GIT_STATUS_INDEX_RENAMED:
372 printf("m %s -> %s\n",
373 se->head_to_index->old_file.path,
374 se->head_to_index->new_file.path);
376 case GIT_STATUS_INDEX_TYPECHANGE:
377 printf("t %s\n", se->head_to_index->old_file.path);
379 case GIT_STATUS_CURRENT:
385 git_status_list_free(status);
386 git_repository_free(repo);
387 git_libgit2_shutdown();