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
+
1
18
#include <stdio.h>
2
19
#include <git2.h>
3
20
#include <stdlib.h>
4
21
#include <string.h>
5
22
#include <time.h>
6
23
24
+ /* not actually good error handling */
7
25
static void fail (const char * msg , const char * arg )
8
26
{
9
27
if (arg )
@@ -21,12 +39,14 @@ static void usage(const char *error, const char *arg)
21
39
exit (1 );
22
40
}
23
41
42
+ /* simple string prefix test used in argument parsing */
24
43
static size_t is_prefixed (const char * arg , const char * pfx )
25
44
{
26
45
size_t len = strlen (pfx );
27
46
return !strncmp (arg , pfx , len ) ? len : 0 ;
28
47
}
29
48
49
+ /* parse the tail of the --shared= argument */
30
50
static uint32_t parse_shared (const char * shared )
31
51
{
32
52
if (!strcmp (shared , "false" ) || !strcmp (shared , "umask" ))
@@ -54,8 +74,10 @@ static uint32_t parse_shared(const char *shared)
54
74
return 0 ;
55
75
}
56
76
77
+ /* forward declaration of helper to make an empty parent-less commit */
57
78
static void create_initial_commit (git_repository * repo );
58
79
80
+
59
81
int main (int argc , char * argv [])
60
82
{
61
83
git_repository * repo = NULL ;
@@ -142,6 +164,8 @@ int main(int argc, char *argv[])
142
164
fail ("Could not initialize repository" , dir );
143
165
}
144
166
167
+ /* Print a message to stdout like "git init" does */
168
+
145
169
if (!quiet ) {
146
170
if (bare || gitdir )
147
171
dir = git_repository_path (repo );
@@ -167,6 +191,10 @@ int main(int argc, char *argv[])
167
191
return 0 ;
168
192
}
169
193
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
+ */
170
198
static void create_initial_commit (git_repository * repo )
171
199
{
172
200
git_signature * sig ;
0 commit comments