|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <git2.h> |
| 10 | +#include "common.h" |
| 11 | +#include "cmd.h" |
| 12 | +#include "error.h" |
| 13 | +#include "sighandler.h" |
| 14 | +#include "progress.h" |
| 15 | + |
| 16 | +#include "fs_path.h" |
| 17 | +#include "futils.h" |
| 18 | + |
| 19 | +#define COMMAND_NAME "init" |
| 20 | + |
| 21 | +static char *branch, *git_dir, *template_dir, *path; |
| 22 | +static int quiet, bare; |
| 23 | + |
| 24 | +static const cli_opt_spec opts[] = { |
| 25 | + CLI_COMMON_OPT, |
| 26 | + |
| 27 | + { CLI_OPT_TYPE_SWITCH, "quiet", 'q', &quiet, 1, |
| 28 | + CLI_OPT_USAGE_DEFAULT, NULL, "quiet mode; don't display informational messages" }, |
| 29 | + { CLI_OPT_TYPE_SWITCH, "bare", 0, &bare, 1, |
| 30 | + CLI_OPT_USAGE_DEFAULT, NULL, "don't create a working directory" }, |
| 31 | + { CLI_OPT_TYPE_VALUE, "initial-branch", 'b', &branch, 0, |
| 32 | + CLI_OPT_USAGE_DEFAULT, "name", "initial branch name" }, |
| 33 | + { CLI_OPT_TYPE_VALUE, "separate-git-dir", 0, &git_dir, 0, |
| 34 | + CLI_OPT_USAGE_DEFAULT, "git-dir", "path to separate git directory" }, |
| 35 | + { CLI_OPT_TYPE_VALUE, "template", 0, &template_dir, 0, |
| 36 | + CLI_OPT_USAGE_DEFAULT, "template-dir", "path to git directory templates" }, |
| 37 | + { CLI_OPT_TYPE_LITERAL }, |
| 38 | + { CLI_OPT_TYPE_ARG, "directory", 0, &path, 0, |
| 39 | + CLI_OPT_USAGE_DEFAULT, "directory", "directory to create repository in" }, |
| 40 | + { 0 } |
| 41 | +}; |
| 42 | + |
| 43 | +static void print_help(void) |
| 44 | +{ |
| 45 | + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts, 0); |
| 46 | + printf("\n"); |
| 47 | + |
| 48 | + printf("Create a new git repository.\n"); |
| 49 | + printf("\n"); |
| 50 | + |
| 51 | + printf("Options:\n"); |
| 52 | + |
| 53 | + cli_opt_help_fprint(stdout, opts); |
| 54 | +} |
| 55 | + |
| 56 | +int cmd_init(int argc, char **argv) |
| 57 | +{ |
| 58 | + git_repository *repo = NULL; |
| 59 | + git_repository_init_options init_opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; |
| 60 | + cli_opt invalid_opt; |
| 61 | + const char *repo_path; |
| 62 | + int ret = 0; |
| 63 | + |
| 64 | + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) |
| 65 | + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); |
| 66 | + |
| 67 | + if (cli_opt__show_help) { |
| 68 | + print_help(); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + init_opts.flags |= GIT_REPOSITORY_INIT_MKPATH | |
| 73 | + GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE; |
| 74 | + |
| 75 | + if (bare && git_dir) |
| 76 | + return cli_error_usage("the '--bare' and '--separate-git-dir' options cannot be used together"); |
| 77 | + |
| 78 | + if (bare) |
| 79 | + init_opts.flags |= GIT_REPOSITORY_INIT_BARE; |
| 80 | + |
| 81 | + init_opts.template_path = template_dir; |
| 82 | + init_opts.initial_head = branch; |
| 83 | + |
| 84 | + if (git_dir) { |
| 85 | + init_opts.flags |= GIT_REPOSITORY_INIT_NO_DOTGIT_DIR; |
| 86 | + init_opts.workdir_path = path; |
| 87 | + |
| 88 | + repo_path = git_dir; |
| 89 | + } else { |
| 90 | + repo_path = path; |
| 91 | + } |
| 92 | + |
| 93 | + if (git_repository_init_ext(&repo, repo_path, &init_opts) < 0) { |
| 94 | + ret = cli_error_git(); |
| 95 | + } else if (!quiet) { |
| 96 | + printf("Initialized empty Git repository in %s\n", |
| 97 | + git_repository_path(repo)); |
| 98 | + } |
| 99 | + |
| 100 | + git_repository_free(repo); |
| 101 | + return ret; |
| 102 | +} |
0 commit comments