Improve isolation of new test from user environs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/examples/init.c b/examples/init.c
index 76b037a..4a379c6 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 <stdio.h>
#include <git2.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
+/* 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 02ea676..5076184 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;