From 0ea41445f4028460aefcd55cb37f10870ea9311c Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 16 Aug 2013 15:03:15 -0700 Subject: [PATCH] Improve isolation of new test from user environs --- examples/init.c | 28 ++++++++++++++++++++++++++++ tests-clar/repo/init.c | 11 +++++++++++ 2 files changed, 39 insertions(+) diff --git a/examples/init.c b/examples/init.c index 76b037a88..4a379c6e3 100644 --- a/examples/init.c +++ b/examples/init.c @@ -1,9 +1,27 @@ +/* + * 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. + * + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + #include #include #include #include #include +/* not actually good error handling */ static void fail(const char *msg, const char *arg) { if (arg) @@ -21,12 +39,14 @@ static void usage(const char *error, const char *arg) exit(1); } +/* simple string prefix test used in argument parsing */ static size_t is_prefixed(const char *arg, const char *pfx) { size_t len = strlen(pfx); return !strncmp(arg, pfx, len) ? len : 0; } +/* parse the tail of the --shared= argument */ static uint32_t parse_shared(const char *shared) { if (!strcmp(shared, "false") || !strcmp(shared, "umask")) @@ -54,8 +74,10 @@ static uint32_t parse_shared(const char *shared) return 0; } +/* forward declaration of helper to make an empty parent-less commit */ static void create_initial_commit(git_repository *repo); + int main(int argc, char *argv[]) { git_repository *repo = NULL; @@ -142,6 +164,8 @@ int main(int argc, char *argv[]) fail("Could not initialize repository", dir); } + /* Print a message to stdout like "git init" does */ + if (!quiet) { if (bare || gitdir) dir = git_repository_path(repo); @@ -167,6 +191,10 @@ int main(int argc, char *argv[]) return 0; } +/* Unlike regular "git init", this example shows how to create an initial + * empty commit in the repository. This is the helper function that does + * that. + */ static void create_initial_commit(git_repository *repo) { git_signature *sig; diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c index 02ea676fb..5076184b8 100644 --- a/tests-clar/repo/init.c +++ b/tests-clar/repo/init.c @@ -559,6 +559,17 @@ void test_repo_init__init_with_initial_commit(void) cl_git_pass(git_index_add_bypath(index, "file.txt")); cl_git_pass(git_index_write(index)); + /* Make sure we're ready to use git_signature_default :-) */ + { + git_config *cfg, *local; + cl_git_pass(git_repository_config(&cfg, _repo)); + cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL)); + cl_git_pass(git_config_set_string(local, "user.name", "Test User")); + cl_git_pass(git_config_set_string(local, "user.email", "t@example.com")); + git_config_free(local); + git_config_free(cfg); + } + /* Create a commit with the new contents of the index */ { git_signature *sig;