Commit 0ea41445 by Russell Belfer

Improve isolation of new test from user environs

parent 944c1589
/*
* 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 <stdio.h>
#include <git2.h> #include <git2.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
/* not actually good error handling */
static void fail(const char *msg, const char *arg) static void fail(const char *msg, const char *arg)
{ {
if (arg) if (arg)
...@@ -21,12 +39,14 @@ static void usage(const char *error, const char *arg) ...@@ -21,12 +39,14 @@ static void usage(const char *error, const char *arg)
exit(1); exit(1);
} }
/* simple string prefix test used in argument parsing */
static size_t is_prefixed(const char *arg, const char *pfx) static size_t is_prefixed(const char *arg, const char *pfx)
{ {
size_t len = strlen(pfx); size_t len = strlen(pfx);
return !strncmp(arg, pfx, len) ? len : 0; return !strncmp(arg, pfx, len) ? len : 0;
} }
/* parse the tail of the --shared= argument */
static uint32_t parse_shared(const char *shared) static uint32_t parse_shared(const char *shared)
{ {
if (!strcmp(shared, "false") || !strcmp(shared, "umask")) if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
...@@ -54,8 +74,10 @@ static uint32_t parse_shared(const char *shared) ...@@ -54,8 +74,10 @@ static uint32_t parse_shared(const char *shared)
return 0; return 0;
} }
/* forward declaration of helper to make an empty parent-less commit */
static void create_initial_commit(git_repository *repo); static void create_initial_commit(git_repository *repo);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
git_repository *repo = NULL; git_repository *repo = NULL;
...@@ -142,6 +164,8 @@ int main(int argc, char *argv[]) ...@@ -142,6 +164,8 @@ int main(int argc, char *argv[])
fail("Could not initialize repository", dir); fail("Could not initialize repository", dir);
} }
/* Print a message to stdout like "git init" does */
if (!quiet) { if (!quiet) {
if (bare || gitdir) if (bare || gitdir)
dir = git_repository_path(repo); dir = git_repository_path(repo);
...@@ -167,6 +191,10 @@ int main(int argc, char *argv[]) ...@@ -167,6 +191,10 @@ int main(int argc, char *argv[])
return 0; 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) static void create_initial_commit(git_repository *repo)
{ {
git_signature *sig; git_signature *sig;
......
...@@ -559,6 +559,17 @@ void test_repo_init__init_with_initial_commit(void) ...@@ -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_add_bypath(index, "file.txt"));
cl_git_pass(git_index_write(index)); 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 */ /* Create a commit with the new contents of the index */
{ {
git_signature *sig; git_signature *sig;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment