init.c 6.82 KB
Newer Older
1
/*
2
 * libgit2 "init" example - shows how to initialize a new repo
3
 *
4 5 6 7 8 9 10 11 12
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 14 15 16 17
 */

#include "common.h"

/**
18 19 20 21 22 23 24 25 26 27 28
 * This is a sample program that is similar to "git init".  See the
 * documentation for that (try "git help init") to understand what this
 * program is emulating.
 *
 * This demonstrates using the libgit2 APIs to initialize a new repository.
 *
 * This also contains a special additional option that regular "git init"
 * does not support which is "--initial-commit" to make a first empty commit.
 * That is demonstrated in the "create_initial_commit" helper function.
 */

29
/** Forward declarations of helpers */
30
struct init_opts {
31 32 33 34 35 36 37 38 39
	int no_options;
	int quiet;
	int bare;
	int initial_commit;
	uint32_t shared;
	const char *template;
	const char *gitdir;
	const char *dir;
};
40
static void create_initial_commit(git_repository *repo);
41
static void parse_opts(struct init_opts *o, int argc, char *argv[]);
42

43
int lg2_init(git_repository *repo, int argc, char *argv[])
44
{
45
	struct init_opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
46

47
	parse_opts(&o, argc, argv);
48

49
	/* Initialize repository. */
50

51
	if (o.no_options) {
52 53
		/**
		 * No options were specified, so let's demonstrate the default
54 55
		 * simple case of git_repository_init() API usage...
		 */
56 57
		check_lg2(git_repository_init(&repo, o.dir, 0),
			"Could not initialize repository", NULL);
58 59
	}
	else {
60 61
		/**
		 * Some command line options were specified, so we'll use the
62 63
		 * extended init API to handle them
		 */
64 65
		git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
		initopts.flags = GIT_REPOSITORY_INIT_MKPATH;
66

67 68
		if (o.bare)
			initopts.flags |= GIT_REPOSITORY_INIT_BARE;
69

70 71 72
		if (o.template) {
			initopts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
			initopts.template_path = o.template;
73 74
		}

75
		if (o.gitdir) {
76 77
			/**
			 * If you specified a separate git directory, then initialize
78 79 80
			 * the repository at that path and use the second path as the
			 * working directory of the repository (with a git-link file)
			 */
81 82
			initopts.workdir_path = o.dir;
			o.dir = o.gitdir;
83 84
		}

85 86
		if (o.shared != 0)
			initopts.mode = o.shared;
87

88 89
		check_lg2(git_repository_init_ext(&repo, o.dir, &initopts),
				"Could not initialize repository", NULL);
90 91
	}

92
	/** Print a message to stdout like "git init" does. */
93

94 95 96
	if (!o.quiet) {
		if (o.bare || o.gitdir)
			o.dir = git_repository_path(repo);
97
		else
98
			o.dir = git_repository_workdir(repo);
99

100
		printf("Initialized empty Git repository in %s\n", o.dir);
101 102
	}

103 104
	/**
	 * As an extension to the basic "git init" command, this example
105 106 107 108
	 * gives the option to create an empty initial commit.  This is
	 * mostly to demonstrate what it takes to do that, but also some
	 * people like to have that empty base commit in their repo.
	 */
109
	if (o.initial_commit) {
110 111 112 113 114 115 116 117 118
		create_initial_commit(repo);
		printf("Created empty initial commit\n");
	}

	git_repository_free(repo);

	return 0;
}

119 120
/**
 * Unlike regular "git init", this example shows how to create an initial
121 122 123
 * empty commit in the repository.  This is the helper function that does
 * that.
 */
124 125 126 127 128 129 130
static void create_initial_commit(git_repository *repo)
{
	git_signature *sig;
	git_index *index;
	git_oid tree_id, commit_id;
	git_tree *tree;

131
	/** First use the config to initialize a commit signature for the user. */
132 133

	if (git_signature_default(&sig, repo) < 0)
134 135
		fatal("Unable to create a commit signature.",
		      "Perhaps 'user.name' and 'user.email' are not set");
136 137 138 139

	/* Now let's create an empty tree for this commit */

	if (git_repository_index(&index, repo) < 0)
140
		fatal("Could not open repository index", NULL);
141

142 143
	/**
	 * Outside of this example, you could call git_index_add_bypath()
144 145 146 147 148
	 * here to put actual files into the index.  For our purposes, we'll
	 * leave it empty for now.
	 */

	if (git_index_write_tree(&tree_id, index) < 0)
149
		fatal("Unable to write initial tree from index", NULL);
150 151 152 153

	git_index_free(index);

	if (git_tree_lookup(&tree, repo, &tree_id) < 0)
154
		fatal("Could not look up initial tree", NULL);
155

156 157
	/**
	 * Ready to create the initial commit.
158 159 160 161 162 163 164 165 166
	 *
	 * Normally creating a commit would involve looking up the current
	 * HEAD commit and making that be the parent of the initial commit,
	 * but here this is the first commit so there will be no parent.
	 */

	if (git_commit_create_v(
			&commit_id, repo, "HEAD", sig, sig,
			NULL, "Initial commit", tree, 0) < 0)
167
		fatal("Could not create the initial commit", NULL);
168

169
	/** Clean up so we don't leak memory. */
170 171 172 173

	git_tree_free(tree);
	git_signature_free(sig);
}
174 175 176 177 178 179 180 181 182 183 184

static void usage(const char *error, const char *arg)
{
	fprintf(stderr, "error: %s '%s'\n", error, arg);
	fprintf(stderr,
			"usage: init [-q | --quiet] [--bare] [--template=<dir>]\n"
			"            [--shared[=perms]] [--initial-commit]\n"
			"            [--separate-git-dir] <directory>\n");
	exit(1);
}

185
/** Parse the tail of the --shared= argument. */
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static uint32_t parse_shared(const char *shared)
{
	if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
		return GIT_REPOSITORY_INIT_SHARED_UMASK;

	else if (!strcmp(shared, "true") || !strcmp(shared, "group"))
		return GIT_REPOSITORY_INIT_SHARED_GROUP;

	else if (!strcmp(shared, "all") || !strcmp(shared, "world") ||
			 !strcmp(shared, "everybody"))
		return GIT_REPOSITORY_INIT_SHARED_ALL;

	else if (shared[0] == '0') {
		long val;
		char *end = NULL;
		val = strtol(shared + 1, &end, 8);
		if (end == shared + 1 || *end != 0)
			usage("invalid octal value for --shared", shared);
		return (uint32_t)val;
	}

	else
		usage("unknown value for --shared", shared);

	return 0;
}

213
static void parse_opts(struct init_opts *o, int argc, char *argv[])
214 215 216 217
{
	struct args_info args = ARGS_INFO_INIT;
	const char *sharedarg;

218
	/** Process arguments. */
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

	for (args.pos = 1; args.pos < argc; ++args.pos) {
		char *a = argv[args.pos];

		if (a[0] == '-')
			o->no_options = 0;

		if (a[0] != '-') {
			if (o->dir != NULL)
				usage("extra argument", a);
			o->dir = a;
		}
		else if (!strcmp(a, "-q") || !strcmp(a, "--quiet"))
			o->quiet = 1;
		else if (!strcmp(a, "--bare"))
			o->bare = 1;
		else if (!strcmp(a, "--shared"))
			o->shared = GIT_REPOSITORY_INIT_SHARED_GROUP;
		else if (!strcmp(a, "--initial-commit"))
			o->initial_commit = 1;
		else if (match_str_arg(&sharedarg, &args, "--shared"))
			o->shared = parse_shared(sharedarg);
		else if (!match_str_arg(&o->template, &args, "--template") ||
		         !match_str_arg(&o->gitdir, &args, "--separate-git-dir"))
			usage("unknown option", a);
	}

	if (!o->dir)
247
		usage("must specify directory to init", "");
248
}