UNPKG

15.2 kBMarkdownView Raw
1# node-gitlog
2
3Git log parser for Node.JS
4
5[![build status](https://api.travis-ci.org/domharrington/node-gitlog.svg)](http://travis-ci.org/domharrington/node-gitlog)
6[![dependency status](https://david-dm.org/domharrington/node-gitlog.svg)](https://david-dm.org/domharrington/node-gitlog)
7
8## Installation
9
10```sh
11npm install gitlog --save
12```
13
14## Usage
15
16```js
17const gitlog = require("gitlog").default;
18
19const options = {
20 repo: __dirname + "/test-repo-folder",
21 number: 20,
22 author: "Dom Harrington",
23 fields: ["hash", "abbrevHash", "subject", "authorName", "authorDateRel"],
24 execOptions: { maxBuffer: 1000 * 1024 },
25};
26
27// Synchronous
28const commits = gitlog(options);
29console.log(commits);
30
31// Asynchronous (with Callback)
32gitlog(options, function (error, commits) {
33 // Commits is an array of commits in the repo
34 console.log(commits);
35});
36
37const { gitlogPromise } = require("gitlog");
38
39// Asynchronous (with Promise)
40gitlogPromise(options)
41 .then((commits) => console.log(commits))
42 .catch((err) => console.log(err));
43```
44
45`gitlog` comes with full typescript support!
46
47```ts
48import gitlog, { GitlogOptions } from "gitlog";
49
50// Option 1: Just use the function, returned commit type has specified fields
51gitlog({
52 repo: "foo",
53 fields: ["subject", "authorName", "authorDate"],
54});
55
56// Option 2: Use Options type to create options
57const options: GitlogOptions<"subject" | "authorName" | "authorDate"> = {
58 repo: "foo",
59 fields: ["subject", "authorName", "authorDate"],
60};
61
62gitlog(options);
63
64// Option 3: Typescript Magic
65const options = {
66 repo: "foo",
67 fields: ["subject", "authorName", "authorDate"] as const,
68};
69
70gitlog(options);
71
72// NOT SUPPORTED: Without "as const" gitlog can't create a good return type
73const options = {
74 repo: "foo",
75 fields: ["subject", "authorName", "authorDate"],
76};
77
78gitlog(options);
79```
80
81## Options
82
83See [git log](http://git-scm.com/docs/git-log)
84
85### repo
86
87The location of the repo, required field.
88
89### number
90
91The number of commits to return, defaults to 10.
92
93### since/after
94
95Show commits more recent than a specific date.
96
97### until/before
98
99Show commits older than a specific date.
100
101### author/committer
102
103Limit the commits output to ones with author/committer header lines that match the specified pattern.
104
105### nameStatus
106
107Below fields was returned from the log:
108
109- files - changed files names (array)
110- status - changed files status (array)
111
112This option is enabled by default.
113
114### findCopiesHarder
115
116Much more likely to set status codes to 'C' if files are exact copies of each other.
117
118This option is disabled by default.
119
120### includeMergeCommitFiles
121
122Pass the `-m` option to includes files in a merge commit.
123
124This option is disabled by default.
125
126### all
127
128Find commits on all branches instead of just on the current one.
129
130This option is disabled by default.
131
132### branch ([revision range](https://git-scm.com/docs/git-log#git-log-ltrevisionrangegt))
133
134Show only commits in the specified branch or revision range.
135
136By default uses the current branch and defaults to `HEAD` (i.e. the whole history leading to the current commit).
137
138### fileLineRange
139
140Optional field for getting only the commits that affected a specific line range of a given file.
141
142### file
143
144Optional file filter for the `git log` command
145
146### execOptions
147
148Type: `Object`
149
150Specify some options to be passed to the [.exec()](http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) method:
151
152- `cwd` String _Current working directory of the child process_
153- `env` Object _Environment key-value pairs_
154- `setsid` Boolean
155- `encoding` String _(Default: 'utf8')_
156- `timeout` Number _(Default: 0)_
157- `maxBuffer` Number _(Default: 200\*1024)_
158- `killSignal` String _(Default: 'SIGTERM')_
159
160### optional fields
161
162An array of fields to return from the log, here are the possible options:
163
164- `hash` - the long hash of the commit e.g. 7dd0b07625203f69cd55d779d873f1adcffaa84a
165- `abbrevHash` - the abbreviated commit hash e.g. 7dd0b07
166- `treeHash` - the tree hash of the commit
167- `abbrevTreeHash` - the abbreviated commit hash
168- `parentHashes` - the parent hashes
169- `abbrevParentHashes` - the abbreviated parent hashes
170- `authorName` - author name of the commit
171- `authorEmail` - author email of the commit
172- `authorDate` - author date of the commit
173- `authorDateRel` - relative author date of the commit
174- `committerName` - committer name
175- `committerEmail` - committer email
176- `committerDate` - committer date
177- `committerDateRel` - relative committer date
178- `subject` - commit message (first line)
179- `body` - commit body
180- `rawBody` - raw body (subject + body)
181- `tag` - raw tag information of commit
182
183Defaults to 'abbrevHash', 'hash', 'subject' and 'authorName'.
184
185## How it works
186
187This module works by executing a child process (using `child_process.exec()`) to the `git` executable, then parsing the stdout into commits. This is done using the `--pretty` command line option which allows you to provide a custom formatter to `git log`. To enable easy parsing the format is delimited by a tab (`\t`) character.
188
189## Example
190
191The following is an example of what a parsed commit might look like.
192
193```json
194{
195 "hash": "6a7ef5e3b3d9c77743140443c8f9e792b0715721",
196 "abbrevHash": "6a7ef5e",
197 "treeHash": "f1bf51b15b48a00c33727f364afef695029864c0",
198 "abbrevTreeHash": "f1bf51b",
199 "parentHashes": "cfe06dbdb8d0a193640977e016a04678f8f3b04f",
200 "abbrevParentHashes": "cfe06dbdb8d0a193640977e016a04678f8f3b04f",
201 "authorName": "Dom Harrington",
202 "authorEmail": "dom@harringtonxxxxx",
203 "authorDate": "2015-04-09 09:39:23 +0100",
204 "authorDateRel": "6 days ago",
205 "committerName": "Dom Harrington",
206 "committerEmail": "dom@harringtonxxxxx",
207 "committerDate": "Thu Apr 9 09:39:23 2015 +0100",
208 "committerDateRel": "6 days ago",
209 "subject": "1.0.0",
210 "status": ["M"],
211 "files": ["package.json"]
212}
213```
214
215## Contributors ✨
216
217Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
218
219<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
220<!-- prettier-ignore-start -->
221<!-- markdownlint-disable -->
222<table>
223 <tr>
224 <td align="center"><a href="https://github.com/domharrington"><img src="https://avatars0.githubusercontent.com/u/848223?v=4?s=100" width="100px;" alt=""/><br /><sub><b>domharrington</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=domharrington" title="Code">πŸ’»</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=domharrington" title="Documentation">πŸ“–</a> <a href="#example-domharrington" title="Examples">πŸ’‘</a> <a href="#ideas-domharrington" title="Ideas, Planning, & Feedback">πŸ€”</a></td>
225 <td align="center"><a href="http://hipstersmoothie.com"><img src="https://avatars3.githubusercontent.com/u/1192452?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andrew Lisowski</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=hipstersmoothie" title="Code">πŸ’»</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=hipstersmoothie" title="Documentation">πŸ“–</a> <a href="#infra-hipstersmoothie" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="#maintenance-hipstersmoothie" title="Maintenance">🚧</a></td>
226 <td align="center"><a href="http://metaodi.ch"><img src="https://avatars1.githubusercontent.com/u/538415?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stefan Oderbolz</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/issues?q=author%3Ametaodi" title="Bug reports">πŸ›</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=metaodi" title="Code">πŸ’»</a></td>
227 <td align="center"><a href="https://github.com/palortoff"><img src="https://avatars1.githubusercontent.com/u/10258543?v=4?s=100" width="100px;" alt=""/><br /><sub><b>palortoff</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/issues?q=author%3Apalortoff" title="Bug reports">πŸ›</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=palortoff" title="Code">πŸ’»</a></td>
228 <td align="center"><a href="https://malys.github.io/"><img src="https://avatars1.githubusercontent.com/u/463016?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Malys</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/issues?q=author%3Amalys" title="Bug reports">πŸ›</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=malys" title="Code">πŸ’»</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=malys" title="Documentation">πŸ“–</a></td>
229 <td align="center"><a href="http://CodeGymSleep.com/"><img src="https://avatars3.githubusercontent.com/u/6986032?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mike Mellor</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=mikemellor11" title="Code">πŸ’»</a></td>
230 <td align="center"><a href="https://github.com/hmedney"><img src="https://avatars3.githubusercontent.com/u/1221751?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hunter Medney</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=hmedney" title="Code">πŸ’»</a></td>
231 </tr>
232 <tr>
233 <td align="center"><a href="https://github.com/Gilwyad"><img src="https://avatars3.githubusercontent.com/u/1919041?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Peter Baranyi</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=Gilwyad" title="Code">πŸ’»</a> <a href="https://github.com/domharrington/node-gitlog/issues?q=author%3AGilwyad" title="Bug reports">πŸ›</a></td>
234 <td align="center"><a href="https://twitter.com/B_Blackwo"><img src="https://avatars0.githubusercontent.com/u/7598058?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Benjamin Blackwood</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=BBlackwo" title="Documentation">πŸ“–</a></td>
235 <td align="center"><a href="https://github.com/Asheboy"><img src="https://avatars1.githubusercontent.com/u/1822529?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ash Summers</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=Asheboy" title="Code">πŸ’»</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=Asheboy" title="Tests">⚠️</a></td>
236 <td align="center"><a href="https://github.com/007design"><img src="https://avatars0.githubusercontent.com/u/1998403?v=4?s=100" width="100px;" alt=""/><br /><sub><b>007design</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=007design" title="Code">πŸ’»</a></td>
237 <td align="center"><a href="https://github.com/tobaccoplaybook"><img src="https://avatars2.githubusercontent.com/u/21124900?v=4?s=100" width="100px;" alt=""/><br /><sub><b>tobaccoplaybook</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=tobaccoplaybook" title="Code">πŸ’»</a></td>
238 <td align="center"><a href="http://nicola.io"><img src="https://avatars1.githubusercontent.com/u/1424850?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicola</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=nicola" title="Code">πŸ’»</a></td>
239 <td align="center"><a href="http://bluelovers.net"><img src="https://avatars0.githubusercontent.com/u/167966?v=4?s=100" width="100px;" alt=""/><br /><sub><b>bluelovers</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=bluelovers" title="Code">πŸ’»</a></td>
240 </tr>
241 <tr>
242 <td align="center"><a href="https://github.com/afram"><img src="https://avatars0.githubusercontent.com/u/2238230?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Marwan Butrous</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=afram" title="Code">πŸ’»</a></td>
243 <td align="center"><a href="https://github.com/FishOrBear"><img src="https://avatars0.githubusercontent.com/u/19372111?v=4?s=100" width="100px;" alt=""/><br /><sub><b>FishOrBear</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=FishOrBear" title="Code">πŸ’»</a></td>
244 <td align="center"><a href="https://github.com/Asjidkalam"><img src="https://avatars1.githubusercontent.com/u/16708391?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Asjid Kalam</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=Asjidkalam" title="Code">πŸ’»</a></td>
245 <td align="center"><a href="https://418sec.com/"><img src="https://avatars0.githubusercontent.com/u/55323451?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jamie Slome</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=JamieSlome" title="Code">πŸ’»</a></td>
246 <td align="center"><a href="https://huntr.dev/"><img src="https://avatars0.githubusercontent.com/u/61279246?v=4?s=100" width="100px;" alt=""/><br /><sub><b>huntr-helper</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=huntr-helper" title="Code">πŸ’»</a></td>
247 <td align="center"><a href="https://salmonmode.github.io/"><img src="https://avatars3.githubusercontent.com/u/13908130?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chris NeJame</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=SalmonMode" title="Documentation">πŸ“–</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=SalmonMode" title="Tests">⚠️</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=SalmonMode" title="Code">πŸ’»</a></td>
248 <td align="center"><a href="https://github.com/ron-checkmarx"><img src="https://avatars2.githubusercontent.com/u/67099202?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ron</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=ron-checkmarx" title="Tests">⚠️</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=ron-checkmarx" title="Code">πŸ’»</a></td>
249 </tr>
250 <tr>
251 <td align="center"><a href="https://www.linkedin.com/in/juanignaciogarzon/"><img src="https://avatars.githubusercontent.com/u/9467722?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juan Ignacio GarzΓ³n</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=jigarzon" title="Documentation">πŸ“–</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=jigarzon" title="Code">πŸ’»</a></td>
252 <td align="center"><a href="https://github.com/vlovich"><img src="https://avatars.githubusercontent.com/u/201287?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vitali Lovich</b></sub></a><br /><a href="https://github.com/domharrington/node-gitlog/commits?author=vlovich" title="Tests">⚠️</a> <a href="https://github.com/domharrington/node-gitlog/commits?author=vlovich" title="Code">πŸ’»</a></td>
253 </tr>
254</table>
255
256<!-- markdownlint-restore -->
257<!-- prettier-ignore-end -->
258
259<!-- ALL-CONTRIBUTORS-LIST:END -->
260
261This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!