Skip to content

Commit 6d9dbb8

Browse files
committed
Merge pull request #43 from libgit2/rb/more-open-special-cases
Add repository open and discover examples
2 parents e6839cf + 8f5494b commit 6d9dbb8

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

docs/guides/101-samples/index.md

Lines changed: 61 additions & 8 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;
@@ -156,7 +161,7 @@ error = git_clone_into(repo, origin, NULL, NULL);
156161
[`git_config_set_bool`](http://libgit2.github.com/libgit2/#HEAD/group/config/git_config_set_bool),
157162
[`git_clone_into`](http://libgit2.github.com/libgit2/#HEAD/group/clone/git_clone_into))
158163

159-
<h3 id="repositories_opening_simple">Opening (Simple)</h3>
164+
<h3 id="repositories_open_simple">Open (Simple)</h3>
160165

161166
```c
162167
git_repository *repo = NULL;
@@ -165,12 +170,60 @@ int error = git_repository_open(&repo, "/tmp/…");
165170

166171
([`git_repository_open`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open))
167172

168-
<h3 id="repositories_opening_options">Opening (Options)</h3>
173+
<h3 id="repositories_open_options">Open (Options)</h3>
174+
175+
```c
176+
int error;
177+
git_repository *repo = 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");
189+
```
190+
191+
([`git_repository_open_ext`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open_ext),
192+
[`git_repository_open_flag_t`](http://libgit2.github.com/libgit2/#HEAD/type/git_repository_open_flag_t))
193+
194+
<h3 id="repositories_open_bare">Open (Bare)</h3>
195+
196+
A fast way of opening a bare repository when the exact path is known.
169197
170198
```c
171199
git_repository *repo = NULL;
172-
int error = git_repository_open_ext(&repo, "/tmp/…",
173-
GIT_REPOSITORY_OPEN_NO_SEARCH, 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_discover">Find Repository</h3>
206+
207+
Check if a given path is inside a repository and return the repository
208+
root directory if found.
209+
210+
```c
211+
git_buf root = {0};
212+
int error = git_repository_discover(&root, "/tmp/…", 0, NULL);
213+
214+
git_buf_free(&root); /* returned path data must be freed after use */
215+
```
216+
217+
([`git_repository_discover`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_discover))
218+
219+
<h3 id="repositories_openable">Check If Repository</h3>
220+
221+
```c
222+
/* Pass NULL for the output parameter to check for but not open the repo */
223+
if (git_repository_open_ext(
224+
NULL, "/tmp/…", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) == 0) {
225+
/* directory looks like an openable repository */;
226+
}
174227
```
175228
176229
([`git_repository_open_ext`](http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_open_ext),

0 commit comments

Comments
 (0)