Skip to content

Commit c89ab3b

Browse files
author
John Haley
committed
Add open repo guide
1 parent ff081d1 commit c89ab3b

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

guides/repositories/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,123 @@ title: Opening a Repository
55
description: How to open and free a repository
66
---
77

8+
**In order to run examples, you will need to [Install NodeGit](../../install/basics)
9+
first.**
10+
811
[Return to all guides](../)
12+
13+
* * *
14+
15+
Opening a Repository
16+
--------------------
17+
18+
This guide explains how to open a repository, and how to work with errors in a
19+
promise chain
20+
21+
[View example source](index.js)
22+
23+
### Requiring NodeGit
24+
25+
In the guides directory, we like to keep our NodeGit relative to the project
26+
root.
27+
28+
``` javascript
29+
var NodeGit = require("../../../");
30+
```
31+
32+
However, in your project you will most likely be using the following command:
33+
34+
``` javascript
35+
var NodeGit = require("nodegit");
36+
```
37+
38+
### Path to Repo
39+
40+
The only argument to the `open` method is a path to the repo on disk. Here we
41+
are calculating that from our current directory using the `path` object from
42+
node.
43+
44+
``` javascript
45+
var pathToRepo = require("path").resolve("../my-git-projects/my-project");
46+
```
47+
48+
You can also point it directly to a `.git` folder to open as well
49+
50+
``` javascript
51+
var pathToRepo = require("path").resolve("../my-git-projects/my-project/.git");
52+
```
53+
54+
This is not necessary though as the function will check the passed directory
55+
for the `.git` subdirectory
56+
57+
### Open a Repo
58+
59+
Now that we have our path to the repo we wish to open we can do so by calling
60+
the `open` method on the `NodeGit.Repository` module
61+
62+
``` javascript
63+
NodeGit.Repository.open(pathToRepo).then(function (repo) {
64+
// Inside of this function we have an open repo
65+
});
66+
```
67+
68+
*NOTE: We use promises to perform operations in NodeGit. This allows the node event
69+
loop to keep cycling through while under the hood our wrapped libgit2 code is
70+
performing the actions we requested and we're not waiting for it.
71+
72+
This allows our apps to remain responsive and performant. However if you're
73+
not used to promises then this can take some getting used to. If you need
74+
an introduction you can head over to https://www.promisejs.org/ for some
75+
tutorials.*
76+
77+
### Handling errors
78+
79+
Promises will swallow errors if there isn't code to explicitly handle them.
80+
You can do this through any of the following 3 ways.
81+
82+
#### Providing a second function to the `.then` method
83+
84+
You can pass a second function parameter to the `.then` method that will have
85+
the reason why a promise failed in it's first argument.
86+
87+
``` javascript
88+
NodeGit.Repository.open(pathToRepo).then(function (sucessfulResult) {
89+
// This is the first function of the then which contains the successfully
90+
// calculated result of the promise
91+
}, function (reasonForFailure) {
92+
// This is the second function of the then which contains the reason the
93+
// promise failed
94+
});
95+
```
96+
97+
#### Including a `.catch` in a chain
98+
99+
You can also append a `.catch` to the end of a promise chain which will
100+
receive any promise failure that isn't previously caught
101+
102+
``` javascript
103+
NodeGit.Repository.open(pathToRepo).then(function (sucessfulResult) {
104+
// This is the first function of the then which contains the successfully
105+
// calculated result of the promise
106+
})
107+
.catch(function (reasonForFailure) {
108+
// failure is handled here
109+
});
110+
```
111+
112+
#### Finishing a chain with `done`
113+
114+
If you append a `.done` at the end of your chain, you will have any error that
115+
wasn't previously handled by the above 2 methods thrown.
116+
117+
``` javascript
118+
NodeGit.Repository.open(pathToRepo).then(function (sucessfulResult) {
119+
// This is the first function of the then which contains the successfully
120+
// calculated result of the promise
121+
})
122+
.done(function () {
123+
// If we have a .done then the error will be thrown if there was an error that
124+
// wasn't caught by either providing a 2nd function to the `.then` or a
125+
// `.catch` function
126+
});
127+
```

guides/repositories/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Require in NodeGit, since we want to use the local copy, we're using a
2+
// relative path. In your project, you will use:
3+
//
4+
// var NodeGit = require("nodegit");
5+
var NodeGit = require("../../../");
6+
7+
// Using the `open` method from the `NodeGit.Repository` module, we can open
8+
// a repository using NodeGit
9+
var pathToRepo = require("path").resolve("../my-git-projects/my-project");
10+
11+
// In NodeGit we use Promises to make callbacks easier to deal with.
12+
//
13+
// For more information visit https://www.promisejs.org/
14+
NodeGit.Repository.open(pathToRepo).then(function (repo) {
15+
// In this function we have a repo object that we can perform git operations
16+
// on.
17+
// NOTE: Many NodeGit objects will appear as empty objects if inspected in
18+
// the console. This is a known issue. You can track it's progress at
19+
// https://github.com/nodegit/nodegit/issues/307
20+
})
21+
// Promises will swallow errors and not report them unless you have supplied
22+
// a second function to the `.then` or end the chain with either a `.catch` or
23+
// a `.done`
24+
.then(function (successfulResult) {
25+
// This is the first function of the then which contains the successfully
26+
// calculated result of the promise
27+
}, function (reasonForFailure) {
28+
// This is the second function of the then which contains the reason the
29+
// promise failed
30+
})
31+
.catch(function (reasonForFailure) {
32+
// You can also provide a catch function which will contain the reason why
33+
// any above promises that weren't handled have failed
34+
})
35+
.done(function() {
36+
// If we have a .done then the error will be thrown if there was an error that
37+
// wasn't caught by either providing a 2nd function to the `.then` or a
38+
// `.catch` function
39+
});

0 commit comments

Comments
 (0)