@@ -5,4 +5,123 @@ title: Opening a Repository
55description : 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+ ```
0 commit comments