You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `--shared` option has a somewhat complicated argument. Here's the code to parse that.
28
+
Next we have a bit of a shortcut; if we were called with no options, we can use the simplest API for doing this, since its defaults match those of the command line.
29
+
That `check_lg2` utility checks the value passed in the first parameter, and if it's not zero, prints the other two parameters and exits the program.
30
+
Not the greatest error handling, but it'll do for these examples.
54
31
55
32
```c
56
-
static uint32_t parse_shared(const char *shared)
57
-
{
58
-
if (!strcmp(shared, "false") || !strcmp(shared, "umask"))
59
-
return GIT_REPOSITORY_INIT_SHARED_UMASK;
60
-
61
-
else if (!strcmp(shared, "true") || !strcmp(shared, "group"))
62
-
return GIT_REPOSITORY_INIT_SHARED_GROUP;
63
-
64
-
else if (!strcmp(shared, "all") || !strcmp(shared, "world"))
65
-
return GIT_REPOSITORY_INIT_SHARED_ALL;
66
-
67
-
else if (shared[0] == '0') {
68
-
long val;
69
-
char *end = NULL;
70
-
val = strtol(shared+1, &end, 8);
71
-
if (end == shared + 1 || *end != 0)
72
-
usage("invalid octal value for --shared", shared);
73
-
return (uint32_t)val;
74
-
}
75
-
76
-
else
77
-
usage("unknown value for --shared", shared);
78
-
79
-
return 0;
80
-
}
33
+
if (o.no_options) {
34
+
check_lg2(git_repository_init(&repo, o.dir, 0),
35
+
"Could not initialize repository", NULL);
36
+
}
81
37
```
82
38
83
-
## Main Entry Point
39
+
If the situation is more complex, you can use the extended API to handle it.
40
+
The fields in [the options structure][initopts] are designed to provide much of what `git init` does.
41
+
Note that it's important to use the `_INIT` structure initializers; these structures have version numbers so future libgit2's can maintain backwards compatibility.
84
42
85
-
Let's take the main function in pieces.
86
-
First, as C89 requires, we declare all of our variables at the top of the scope.
Libgit2's repository is always oriented at the `.git` directory, so specifying an external git directory turns things a bit upside-down:
107
60
108
61
```c
109
-
for (i = 1; i < argc; ++i) {
110
-
char *a = argv[i];
111
-
112
-
if (a[0] == '-')
113
-
no_options = 0;
114
-
115
-
if (a[0] != '-') {
116
-
if (dir != NULL)
117
-
usage("extra argument", a);
118
-
dir = a;
119
-
}
120
-
else if (!strcmp(a, "-q") || !strcmp(a, "--quiet"))
121
-
quiet = 1;
122
-
else if (!strcmp(a, "--bare"))
123
-
bare = 1;
124
-
```
62
+
if (o.gitdir) {
63
+
initopts.workdir_path = o.dir;
64
+
o.dir = o.gitdir;
65
+
}
125
66
126
-
Here's how that `is_prefixed` call come into play.
127
-
We use its return code to both detect a `--opt=val` argument, but also to find the `val` part of the string.
128
-
129
-
```c
130
-
else if ((pfxlen = is_prefixed(a, "--template=")) > 0)
131
-
template = a + pfxlen;
132
-
else if (!strcmp(a, "--separate-git-dir"))
133
-
gitdir = argv[++i];
134
-
else if ((pfxlen = is_prefixed(a, "--separate-git-dir=")) > 0)
135
-
gitdir = a + pfxlen;
136
-
else if (!strcmp(a, "--shared"))
137
-
shared = GIT_REPOSITORY_INIT_SHARED_GROUP;
138
-
else if ((pfxlen = is_prefixed(a, "--shared=")) > 0)
139
-
shared = parse_shared(a + pfxlen);
140
-
else if (!strcmp(a, "--initial-commit"))
141
-
initial_commit = 1;
142
-
else
143
-
usage("unknown option", a);
144
-
}
145
-
146
-
if (!dir)
147
-
usage("must specify directory to init", NULL);
67
+
if (o.shared != 0)
68
+
initopts.mode = o.shared;
148
69
```
149
70
150
-
Now for the meat of the program; let's initialize that repository.
151
-
If we were called with no options, we can use the simplest API for doing this, since its defaults match those of the command line.
71
+
Now the call that does all the work: [`git_repository_init_ext`][grie].
72
+
The output (if the call succeeds) lands in `repo`, which is a `git_repository*`, which we can then go on and use.
152
73
153
-
```c
154
-
if (no_options) {
155
-
if (git_repository_init(&repo, dir, 0) < 0)
156
-
fail("Could not initialize repository", dir);
157
-
} else {
158
-
```
159
-
160
-
If the situation is more complex, you can use the extended API to handle it.
161
-
The fields in [the options structure](http://libgit2.github.com/libgit2/#HEAD/type/git_repository_init_options) are designed to provide much of what `git init` does.
162
-
Note that it's important to use the `_INIT` structure initializers; these structures have version numbers so future libgit2's can maintain backwards compatibility.
0 commit comments