Blame examples/init.c

Packit ae9e2a
/*
Packit ae9e2a
 * libgit2 "init" example - shows how to initialize a new repo
Packit ae9e2a
 *
Packit ae9e2a
 * Written by the libgit2 contributors
Packit ae9e2a
 *
Packit ae9e2a
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit ae9e2a
 * and related and neighboring rights to this software to the public domain
Packit ae9e2a
 * worldwide. This software is distributed without any warranty.
Packit ae9e2a
 *
Packit ae9e2a
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit ae9e2a
 * with this software. If not, see
Packit ae9e2a
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * This is a sample program that is similar to "git init".  See the
Packit ae9e2a
 * documentation for that (try "git help init") to understand what this
Packit ae9e2a
 * program is emulating.
Packit ae9e2a
 *
Packit ae9e2a
 * This demonstrates using the libgit2 APIs to initialize a new repository.
Packit ae9e2a
 *
Packit ae9e2a
 * This also contains a special additional option that regular "git init"
Packit ae9e2a
 * does not support which is "--initial-commit" to make a first empty commit.
Packit ae9e2a
 * That is demonstrated in the "create_initial_commit" helper function.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/** Forward declarations of helpers */
Packit ae9e2a
struct opts {
Packit ae9e2a
	int no_options;
Packit ae9e2a
	int quiet;
Packit ae9e2a
	int bare;
Packit ae9e2a
	int initial_commit;
Packit ae9e2a
	uint32_t shared;
Packit ae9e2a
	const char *template;
Packit ae9e2a
	const char *gitdir;
Packit ae9e2a
	const char *dir;
Packit ae9e2a
};
Packit ae9e2a
static void create_initial_commit(git_repository *repo);
Packit ae9e2a
static void parse_opts(struct opts *o, int argc, char *argv[]);
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
int main(int argc, char *argv[])
Packit ae9e2a
{
Packit ae9e2a
	git_repository *repo = NULL;
Packit ae9e2a
	struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
Packit ae9e2a
Packit ae9e2a
	git_libgit2_init();
Packit ae9e2a
Packit ae9e2a
	parse_opts(&o, argc, argv);
Packit ae9e2a
Packit ae9e2a
	/* Initialize repository. */
Packit ae9e2a
Packit ae9e2a
	if (o.no_options) {
Packit ae9e2a
		/**
Packit ae9e2a
		 * No options were specified, so let's demonstrate the default
Packit ae9e2a
		 * simple case of git_repository_init() API usage...
Packit ae9e2a
		 */
Packit ae9e2a
		check_lg2(git_repository_init(&repo, o.dir, 0),
Packit ae9e2a
			"Could not initialize repository", NULL);
Packit ae9e2a
	}
Packit ae9e2a
	else {
Packit ae9e2a
		/**
Packit ae9e2a
		 * Some command line options were specified, so we'll use the
Packit ae9e2a
		 * extended init API to handle them
Packit ae9e2a
		 */
Packit ae9e2a
		git_repository_init_options initopts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
Packit ae9e2a
		initopts.flags = GIT_REPOSITORY_INIT_MKPATH;
Packit ae9e2a
Packit ae9e2a
		if (o.bare)
Packit ae9e2a
			initopts.flags |= GIT_REPOSITORY_INIT_BARE;
Packit ae9e2a
Packit ae9e2a
		if (o.template) {
Packit ae9e2a
			initopts.flags |= GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
Packit ae9e2a
			initopts.template_path = o.template;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		if (o.gitdir) {
Packit ae9e2a
			/**
Packit ae9e2a
			 * If you specified a separate git directory, then initialize
Packit ae9e2a
			 * the repository at that path and use the second path as the
Packit ae9e2a
			 * working directory of the repository (with a git-link file)
Packit ae9e2a
			 */
Packit ae9e2a
			initopts.workdir_path = o.dir;
Packit ae9e2a
			o.dir = o.gitdir;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		if (o.shared != 0)
Packit ae9e2a
			initopts.mode = o.shared;
Packit ae9e2a
Packit ae9e2a
		check_lg2(git_repository_init_ext(&repo, o.dir, &initopts),
Packit ae9e2a
				"Could not initialize repository", NULL);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/** Print a message to stdout like "git init" does. */
Packit ae9e2a
Packit ae9e2a
	if (!o.quiet) {
Packit ae9e2a
		if (o.bare || o.gitdir)
Packit ae9e2a
			o.dir = git_repository_path(repo);
Packit ae9e2a
		else
Packit ae9e2a
			o.dir = git_repository_workdir(repo);
Packit ae9e2a
Packit ae9e2a
		printf("Initialized empty Git repository in %s\n", o.dir);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * As an extension to the basic "git init" command, this example
Packit ae9e2a
	 * gives the option to create an empty initial commit.  This is
Packit ae9e2a
	 * mostly to demonstrate what it takes to do that, but also some
Packit ae9e2a
	 * people like to have that empty base commit in their repo.
Packit ae9e2a
	 */
Packit ae9e2a
	if (o.initial_commit) {
Packit ae9e2a
		create_initial_commit(repo);
Packit ae9e2a
		printf("Created empty initial commit\n");
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
	git_libgit2_shutdown();
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Unlike regular "git init", this example shows how to create an initial
Packit ae9e2a
 * empty commit in the repository.  This is the helper function that does
Packit ae9e2a
 * that.
Packit ae9e2a
 */
Packit ae9e2a
static void create_initial_commit(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_signature *sig;
Packit ae9e2a
	git_index *index;
Packit ae9e2a
	git_oid tree_id, commit_id;
Packit ae9e2a
	git_tree *tree;
Packit ae9e2a
Packit ae9e2a
	/** First use the config to initialize a commit signature for the user. */
Packit ae9e2a
Packit ae9e2a
	if (git_signature_default(&sig, repo) < 0)
Packit ae9e2a
		fatal("Unable to create a commit signature.",
Packit ae9e2a
		      "Perhaps 'user.name' and 'user.email' are not set");
Packit ae9e2a
Packit ae9e2a
	/* Now let's create an empty tree for this commit */
Packit ae9e2a
Packit ae9e2a
	if (git_repository_index(&index, repo) < 0)
Packit ae9e2a
		fatal("Could not open repository index", NULL);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Outside of this example, you could call git_index_add_bypath()
Packit ae9e2a
	 * here to put actual files into the index.  For our purposes, we'll
Packit ae9e2a
	 * leave it empty for now.
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	if (git_index_write_tree(&tree_id, index) < 0)
Packit ae9e2a
		fatal("Unable to write initial tree from index", NULL);
Packit ae9e2a
Packit ae9e2a
	git_index_free(index);
Packit ae9e2a
Packit ae9e2a
	if (git_tree_lookup(&tree, repo, &tree_id) < 0)
Packit ae9e2a
		fatal("Could not look up initial tree", NULL);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Ready to create the initial commit.
Packit ae9e2a
	 *
Packit ae9e2a
	 * Normally creating a commit would involve looking up the current
Packit ae9e2a
	 * HEAD commit and making that be the parent of the initial commit,
Packit ae9e2a
	 * but here this is the first commit so there will be no parent.
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	if (git_commit_create_v(
Packit ae9e2a
			&commit_id, repo, "HEAD", sig, sig,
Packit ae9e2a
			NULL, "Initial commit", tree, 0) < 0)
Packit ae9e2a
		fatal("Could not create the initial commit", NULL);
Packit ae9e2a
Packit ae9e2a
	/** Clean up so we don't leak memory. */
Packit ae9e2a
Packit ae9e2a
	git_tree_free(tree);
Packit ae9e2a
	git_signature_free(sig);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void usage(const char *error, const char *arg)
Packit ae9e2a
{
Packit ae9e2a
	fprintf(stderr, "error: %s '%s'\n", error, arg);
Packit ae9e2a
	fprintf(stderr,
Packit ae9e2a
			"usage: init [-q | --quiet] [--bare] [--template=<dir>]\n"
Packit ae9e2a
			"            [--shared[=perms]] [--initial-commit]\n"
Packit ae9e2a
			"            [--separate-git-dir] <directory>\n");
Packit ae9e2a
	exit(1);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Parse the tail of the --shared= argument. */
Packit ae9e2a
static uint32_t parse_shared(const char *shared)
Packit ae9e2a
{
Packit ae9e2a
	if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
Packit ae9e2a
		return GIT_REPOSITORY_INIT_SHARED_UMASK;
Packit ae9e2a
Packit ae9e2a
	else if (!strcmp(shared, "true") || !strcmp(shared, "group"))
Packit ae9e2a
		return GIT_REPOSITORY_INIT_SHARED_GROUP;
Packit ae9e2a
Packit ae9e2a
	else if (!strcmp(shared, "all") || !strcmp(shared, "world") ||
Packit ae9e2a
			 !strcmp(shared, "everybody"))
Packit ae9e2a
		return GIT_REPOSITORY_INIT_SHARED_ALL;
Packit ae9e2a
Packit ae9e2a
	else if (shared[0] == '0') {
Packit ae9e2a
		long val;
Packit ae9e2a
		char *end = NULL;
Packit ae9e2a
		val = strtol(shared + 1, &end, 8);
Packit ae9e2a
		if (end == shared + 1 || *end != 0)
Packit ae9e2a
			usage("invalid octal value for --shared", shared);
Packit ae9e2a
		return (uint32_t)val;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	else
Packit ae9e2a
		usage("unknown value for --shared", shared);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void parse_opts(struct opts *o, int argc, char *argv[])
Packit ae9e2a
{
Packit ae9e2a
	struct args_info args = ARGS_INFO_INIT;
Packit ae9e2a
	const char *sharedarg;
Packit ae9e2a
Packit ae9e2a
	/** Process arguments. */
Packit ae9e2a
Packit ae9e2a
	for (args.pos = 1; args.pos < argc; ++args.pos) {
Packit ae9e2a
		char *a = argv[args.pos];
Packit ae9e2a
Packit ae9e2a
		if (a[0] == '-')
Packit ae9e2a
			o->no_options = 0;
Packit ae9e2a
Packit ae9e2a
		if (a[0] != '-') {
Packit ae9e2a
			if (o->dir != NULL)
Packit ae9e2a
				usage("extra argument", a);
Packit ae9e2a
			o->dir = a;
Packit ae9e2a
		}
Packit ae9e2a
		else if (!strcmp(a, "-q") || !strcmp(a, "--quiet"))
Packit ae9e2a
			o->quiet = 1;
Packit ae9e2a
		else if (!strcmp(a, "--bare"))
Packit ae9e2a
			o->bare = 1;
Packit ae9e2a
		else if (!strcmp(a, "--shared"))
Packit ae9e2a
			o->shared = GIT_REPOSITORY_INIT_SHARED_GROUP;
Packit ae9e2a
		else if (!strcmp(a, "--initial-commit"))
Packit ae9e2a
			o->initial_commit = 1;
Packit ae9e2a
		else if (match_str_arg(&sharedarg, &args, "--shared"))
Packit ae9e2a
			o->shared = parse_shared(sharedarg);
Packit ae9e2a
		else if (!match_str_arg(&o->template, &args, "--template") ||
Packit ae9e2a
		         !match_str_arg(&o->gitdir, &args, "--separate-git-dir"))
Packit ae9e2a
			usage("unknown option", a);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!o->dir)
Packit ae9e2a
		usage("must specify directory to init", NULL);
Packit ae9e2a
}