|
1 | | -nodegit |
2 | | -======= |
| 1 | +NodeGit |
| 2 | +------- |
3 | 3 |
|
4 | | -> Node.js libgit2 bindings |
| 4 | +> Node bindings to the [libgit2](http://libgit2.github.com/) project. |
5 | 5 |
|
6 | | -**v0.1.1** [](https://travis-ci.org/nodegit/nodegit) |
| 6 | +**Stable: 0.1.1** |
| 7 | + |
| 8 | +[](https://travis-ci.org/nodegit/nodegit) |
8 | 10 |
|
9 | 11 | Maintained by Tim Branyen [@tbranyen](http://twitter.com/tbranyen), Michael |
10 | | -Robinson [@codeofinterest](http://twitter.com/codeofinterest), and Nick Kallen [@nk](http://twitter.com/nk), with help from |
11 | | -[awesome |
| 12 | +Robinson [@codeofinterest](http://twitter.com/codeofinterest), and Nick Kallen |
| 13 | +[@nk](http://twitter.com/nk), with help from [awesome |
12 | 14 | contributors](https://github.com/tbranyen/nodegit/contributors)! |
13 | 15 |
|
14 | | -API Documentation |
15 | | ------------------------- |
| 16 | +## API Documentation. ## |
16 | 17 |
|
17 | | -Documentation may be found here: [`nodegit` documentation](http://www.nodegit.org/nodegit/). |
| 18 | +http://www.nodegit.org/nodegit/ |
18 | 19 |
|
19 | | -Building and installing |
20 | | ------------------------ |
| 20 | +## Building and Installing. ## |
21 | 21 |
|
22 | | -### Dependencies ### |
| 22 | +Dependencies: |
23 | 23 |
|
24 | | -To install `nodegit` you need `Node.js`, `python` and `cmake` (>=2.8). |
| 24 | +* [Python 2](https://www.python.org/) |
| 25 | +* [CMake >= 2.8](http://www.cmake.org/) |
25 | 26 |
|
26 | | -### Easy install (Recommended) ### |
27 | | -This will install and configure everything you need to use `nodegit`. |
| 27 | +``` bash |
| 28 | +npm install nodegit |
| 29 | +``` |
28 | 30 |
|
29 | | -```` bash |
30 | | -$ npm run-script gen && npm install && npm test |
31 | | -```` |
| 31 | +### Building manually: ### |
32 | 32 |
|
33 | | -### Mac OS X/Linux/Unix ### |
| 33 | +If you wish to help contribute to nodegit it is useful to build locally. |
34 | 34 |
|
35 | | -#### Install `nodegit` by cloning source from GitHub and running `node install`: #### |
| 35 | +``` bash |
| 36 | +# Fetch this project. |
| 37 | +git clone git://github.com/tbranyen/nodegit.git |
36 | 38 |
|
37 | | -```` bash |
38 | | -# Install system dependencies |
39 | | -$ brew install cmake libzip |
40 | | -$ npm install -g node-gyp |
41 | | -```` |
| 39 | +# Enter the repository. |
| 40 | +cd nodegit |
42 | 41 |
|
43 | | -```` bash |
44 | | -$ git clone git://github.com/tbranyen/nodegit.git |
45 | | -$ cd nodegit |
46 | | -$ npm install ejs && npm run-script gen && npm install |
47 | | -```` |
48 | | -### Ubuntu ### |
| 42 | +# Install the template engine, run the code generation script, and install. |
| 43 | +npm install ejs && npm run codegen && npm install |
| 44 | +``` |
49 | 45 |
|
50 | | -```` bash |
51 | | -# Install system dependencies as root |
52 | | -$ sudo apt-get install libzip-dev |
53 | | -```` |
| 46 | +If you encounter errors, you most likely have not configured the dependencies |
| 47 | +correctly. |
54 | 48 |
|
55 | | -### Windows via Cygwin ### |
| 49 | +### Installing dependencies: ### |
56 | 50 |
|
57 | | -#### `nodegit` has been compiled and tested to work with the setup required to build and run `Node.js` itself. #### |
| 51 | +Using Brew on OS X: |
| 52 | + |
| 53 | +``` bash |
| 54 | +brew install cmake libzip |
| 55 | +``` |
58 | 56 |
|
59 | | -Instructions on compiling `Node.js` on a Windows platform can be found here: |
60 | | -[https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-(Windows)](https://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29) |
| 57 | +Using APT on Ubuntu: |
61 | 58 |
|
62 | | -API Example Usage |
63 | | ------------------ |
| 59 | +``` bash |
| 60 | +sudo apt-get install cmake libzip-dev build-essential |
| 61 | +``` |
64 | 62 |
|
65 | | -Below are two examples. [There are several more](https://github.com/nodegit/nodegit/tree/master/example). |
| 63 | +## API examples. ## |
66 | 64 |
|
67 | | -### Git Log Emulation ### |
| 65 | +### Emulating git log: ### |
68 | 66 |
|
69 | | -```JavaScript |
70 | | -var git = require('../'), |
71 | | - path = require('path'); |
| 67 | +``` javascript |
| 68 | +var open = require("/home/tim/git/nodegit/nodegit").Repo.open; |
72 | 69 |
|
73 | | -git.Repo.open(path.resolve(__dirname, '/tmp/repo/.git'), function(error, repo) { |
74 | | - if (error) throw error; |
| 70 | +// Open the repository directory. |
| 71 | +open("tmp", function(err, repo) { |
| 72 | + if (err) { |
| 73 | + throw err; |
| 74 | + } |
75 | 75 |
|
76 | | - repo.getMaster(function(error, branch) { |
77 | | - if (error) throw error; |
| 76 | + // Open the master branch. |
| 77 | + repo.getMaster(function(err, branch) { |
| 78 | + if (err) { |
| 79 | + throw err; |
| 80 | + } |
78 | 81 |
|
79 | | - // History returns an event. |
| 82 | + // Create a new history event emitter. |
80 | 83 | var history = branch.history(); |
81 | 84 |
|
82 | | - // History emits 'commit' event for each commit in the branch's history |
83 | | - history.on('commit', function(commit) { |
84 | | - console.log('commit ' + commit.sha()); |
85 | | - console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>'); |
86 | | - console.log('Date:', commit.date()); |
87 | | - console.log('\n ' + commit.message()); |
| 85 | + // Create a counter to only show up to 9 entries. |
| 86 | + var count = 0; |
| 87 | + |
| 88 | + // Listen for commit events from the history. |
| 89 | + history.on("commit", function(commit) { |
| 90 | + // Disregard commits past 9. |
| 91 | + if (++count >= 9) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // Show the commit sha. |
| 96 | + console.log("commit " + commit.sha()); |
| 97 | + |
| 98 | + // Store the author object. |
| 99 | + var author = commit.author(); |
| 100 | + |
| 101 | + // Display author information. |
| 102 | + console.log("Author:\t" + author.name() + " <", author.email() + ">"); |
| 103 | + |
| 104 | + // Show the commit date. |
| 105 | + console.log("Date:\t" + commit.date()); |
| 106 | + |
| 107 | + // Give some space and show the message. |
| 108 | + console.log("\n " + commit.message()); |
88 | 109 | }); |
89 | 110 |
|
90 | | - // Don't forget to call `start()`! |
| 111 | + // Start emitting events. |
91 | 112 | history.start(); |
92 | 113 | }); |
93 | 114 | }); |
94 | | - |
95 | 115 | ``` |
96 | 116 |
|
97 | | -### Clone a repo and read a file ### |
| 117 | +### Cloning a repository and reading a file: ### |
| 118 | + |
| 119 | +``` javascript |
| 120 | +var clone = require("/home/tim/git/nodegit/nodegit").Repo.clone; |
| 121 | + |
| 122 | +// Clone a given repository into a specific folder. |
| 123 | +clone("https://github.com/nodegit/nodegit", "tmp", null, function(err, repo) { |
| 124 | + if (err) { |
| 125 | + throw err; |
| 126 | + } |
98 | 127 |
|
99 | | -```JavaScript |
100 | | -git.Repo.clone("https://github.com/nodegit/nodegit.git", path, null, function(error, repo) { |
101 | | - if (error) throw error; |
| 128 | + // Use a known commit sha from this repository. |
| 129 | + var sha = "59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5"; |
102 | 130 |
|
103 | | - repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) { |
104 | | - if (error) throw error; |
| 131 | + // Look up this known commit. |
| 132 | + repo.getCommit(sha, function(err, commit) { |
| 133 | + if (err) { |
| 134 | + throw error; |
| 135 | + } |
105 | 136 |
|
106 | | - commit.getEntry('README.md', function(error, entry) { |
107 | | - if (error) throw error; |
| 137 | + // Look up a specific file within that commit. |
| 138 | + commit.getEntry("README.md", function(err, entry) { |
| 139 | + if (err) { |
| 140 | + throw error; |
| 141 | + } |
108 | 142 |
|
109 | | - entry.getBlob(function(error, blob) { |
110 | | - if (error) throw error; |
| 143 | + // Get the blob contents from the file. |
| 144 | + entry.getBlob(function(err, blob) { |
| 145 | + if (err) { |
| 146 | + throw err; |
| 147 | + } |
111 | 148 |
|
112 | | - console.log(entry.name(), entry.sha(), blob.size() + 'b'); |
113 | | - console.log('========================================================\n\n'); |
114 | | - var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n'); |
115 | | - console.log(firstTenLines); |
116 | | - console.log('...'); |
| 149 | + // Show the name, sha, and filesize in byes. |
| 150 | + console.log(entry.name() + entry.sha() + blob.size() + "b"); |
| 151 | + |
| 152 | + // Show a spacer. |
| 153 | + console.log(Array(72).join("=") + "\n\n"); |
| 154 | + |
| 155 | + // Show the entire file. |
| 156 | + console.log(String(blob)); |
117 | 157 | }); |
118 | 158 | }); |
119 | 159 | }); |
120 | 160 | }); |
121 | 161 | ``` |
122 | 162 |
|
123 | | -[](http://githalytics.com/tbranyen/nodegit) |
| 163 | +## Unit tests. ## |
| 164 | + |
| 165 | +You will need to build locally before running the tests. See above. |
| 166 | + |
| 167 | +``` bash |
| 168 | +npm test |
| 169 | +``` |
0 commit comments