Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
0ea41445
Commit
0ea41445
authored
Aug 16, 2013
by
Russell Belfer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve isolation of new test from user environs
parent
944c1589
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
0 deletions
+39
-0
examples/init.c
+28
-0
tests-clar/repo/init.c
+11
-0
No files found.
examples/init.c
View file @
0ea41445
/*
* 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
;
...
...
tests-clar/repo/init.c
View file @
0ea41445
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment