Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ea4144

Browse files
committed
Improve isolation of new test from user environs
1 parent 944c158 commit 0ea4144

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

examples/init.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
/*
2+
* This is a sample program that is similar to "git init". See the
3+
* documentation for that (try "git help init") to understand what this
4+
* program is emulating.
5+
*
6+
* This demonstrates using the libgit2 APIs to initialize a new repository.
7+
*
8+
* This also contains a special additional option that regular "git init"
9+
* does not support which is "--initial-commit" to make a first empty commit.
10+
* That is demonstrated in the "create_initial_commit" helper function.
11+
*
12+
* Copyright (C) the libgit2 contributors. All rights reserved.
13+
*
14+
* This file is part of libgit2, distributed under the GNU GPL v2 with
15+
* a Linking Exception. For full terms see the included COPYING file.
16+
*/
17+
118
#include <stdio.h>
219
#include <git2.h>
320
#include <stdlib.h>
421
#include <string.h>
522
#include <time.h>
623

24+
/* not actually good error handling */
725
static void fail(const char *msg, const char *arg)
826
{
927
if (arg)
@@ -21,12 +39,14 @@ static void usage(const char *error, const char *arg)
2139
exit(1);
2240
}
2341

42+
/* simple string prefix test used in argument parsing */
2443
static size_t is_prefixed(const char *arg, const char *pfx)
2544
{
2645
size_t len = strlen(pfx);
2746
return !strncmp(arg, pfx, len) ? len : 0;
2847
}
2948

49+
/* parse the tail of the --shared= argument */
3050
static uint32_t parse_shared(const char *shared)
3151
{
3252
if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
@@ -54,8 +74,10 @@ static uint32_t parse_shared(const char *shared)
5474
return 0;
5575
}
5676

77+
/* forward declaration of helper to make an empty parent-less commit */
5778
static void create_initial_commit(git_repository *repo);
5879

80+
5981
int main(int argc, char *argv[])
6082
{
6183
git_repository *repo = NULL;
@@ -142,6 +164,8 @@ int main(int argc, char *argv[])
142164
fail("Could not initialize repository", dir);
143165
}
144166

167+
/* Print a message to stdout like "git init" does */
168+
145169
if (!quiet) {
146170
if (bare || gitdir)
147171
dir = git_repository_path(repo);
@@ -167,6 +191,10 @@ int main(int argc, char *argv[])
167191
return 0;
168192
}
169193

194+
/* Unlike regular "git init", this example shows how to create an initial
195+
* empty commit in the repository. This is the helper function that does
196+
* that.
197+
*/
170198
static void create_initial_commit(git_repository *repo)
171199
{
172200
git_signature *sig;

tests-clar/repo/init.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,17 @@ void test_repo_init__init_with_initial_commit(void)
559559
cl_git_pass(git_index_add_bypath(index, "file.txt"));
560560
cl_git_pass(git_index_write(index));
561561

562+
/* Make sure we're ready to use git_signature_default :-) */
563+
{
564+
git_config *cfg, *local;
565+
cl_git_pass(git_repository_config(&cfg, _repo));
566+
cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL));
567+
cl_git_pass(git_config_set_string(local, "user.name", "Test User"));
568+
cl_git_pass(git_config_set_string(local, "user.email", "[email protected]"));
569+
git_config_free(local);
570+
git_config_free(cfg);
571+
}
572+
562573
/* Create a commit with the new contents of the index */
563574
{
564575
git_signature *sig;

0 commit comments

Comments
 (0)