Skip to content

Commit 39ae3d4

Browse files
committed
Add repo open and discover examples
1 parent e6839cf commit 39ae3d4

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

docs/guides/101-samples/index.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ int error = git_repository_init(&repo, "/tmp/…", true);
5252
<h3 id="repositories_init_options">Init (Options)</h3>
5353

5454
```c
55+
int error;
56+
git_repository *repo = NULL;
5557
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
58+
5659
/* Customize options */
57-
git_repository *repo = NULL;
58-
int error = git_repository_init_ext(&repo, "/tmp/…", &opts);
60+
opts.flags |= GIT_REPOSITORY_INIT_MKPATH; /* mkdir as needed to create repo */
61+
opts.description = "My repository has a custom description";
62+
63+
error = git_repository_init_ext(&repo, "/tmp/…", &opts);
5964
```
6065

6166
([`git_repository_init_ext`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_init_ext),
@@ -139,8 +144,8 @@ error = git_repository_init(&repo, "/tmp/…", true);
139144

140145
/* Create an 'origin' remote with the mirror fetch refspec */
141146
git_remote *origin = NULL;
142-
error = git_remote_create_with_fetchspec(&origin, repo, "origin",
143-
"http://…", "+refs/*:refs/*");
147+
error = git_remote_create_with_fetchspec(
148+
&origin, repo, "origin", "http://…", "+refs/*:refs/*");
144149

145150
/* Set remote.origin.mirror = true for compatibility with git-core */
146151
git_config *cfg = NULL;
@@ -168,14 +173,60 @@ int error = git_repository_open(&repo, "/tmp/…");
168173
<h3 id="repositories_opening_options">Opening (Options)</h3>
169174

170175
```c
176+
int error;
171177
git_repository *repo = NULL;
172-
int error = git_repository_open_ext(&repo, "/tmp/…",
173-
GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
178+
179+
/* Open repository, walking up from given directory to find root */
180+
error = git_repository_open_ext(&repo, "/tmp/…", 0, NULL);
181+
182+
/* Open repository in given directory (or fail if not a repository) */
183+
error = git_repository_open_ext(
184+
&repo, "/tmp/…", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
185+
186+
/* Open repository with "ceiling" directories list to limit walking up */
187+
error = git_repository_open_ext(
188+
&repo, "/home/acct/…, GIT_REPOSITORY_OPEN_CROSS_FS, "/tmp:/usr:/home");
174189
```
175190
176191
([`git_repository_open_ext`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open_ext),
177192
[`git_repository_open_flag_t`](http://libgit2.github.com/libgit2/#HEAD/type/git_repository_open_flag_t))
178193
194+
<h3 id="repositories_open_bare">Opening (Bare)</h3>
195+
196+
A fast way of opening a bare repository when the exact path is known.
197+
198+
```c
199+
git_repository *repo = NULL;
200+
int error = git_repository_open_bare(&repo, "/var/data/…/repo.git");
201+
```
202+
203+
([`git_repository_open_bare`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open_bare))
204+
205+
<h3 id="repositories_openable">Is Directory A Repository?</h3>
206+
207+
```c
208+
/* Pass NULL for the output parameter to not open the repo immediately */
209+
int error = git_repository_open_ext(
210+
NULL, "/tmp/…", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
211+
```
212+
213+
([`git_repository_open_ext`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open_ext),
214+
[`git_repository_open_flag_t`](http://libgit2.github.com/libgit2/#HEAD/type/git_repository_open_flag_t))
215+
216+
<h3 id="repositories_discover">Find Repository Root</h3>
217+
218+
Check if a given path is inside a repository and return the repository
219+
root directory if found.
220+
221+
```c
222+
git_buf root = {0};
223+
int error = git_repository_discover(&root, "/tmp/…", 0, NULL);
224+
225+
git_buf_free(&root); /* returned path data must be freed after use */
226+
```
227+
228+
([`git_repository_discover`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_discover))
229+
179230
180231
181232
<h2 id="objects">Objects</h2>

0 commit comments

Comments
 (0)