UNPKG

18.7 kBMarkdownView Raw
1# gulp-git
2
3[![Build Status](https://travis-ci.org/stevelacy/gulp-git.png?branch=master)](https://travis-ci.org/stevelacy/gulp-git)
4[![NPM version](https://badge.fury.io/js/gulp-git.png)](http://badge.fury.io/js/gulp-git)
5
6<table>
7<tr>
8<td>Package</td><td>gulp-git</td>
9</tr>
10<tr>
11<td>Description</td>
12<td>Git plugin for gulp (gulpjs.com)</td>
13</tr>
14<tr>
15<td>Node Version</td>
16<td>>= 0.9</td>
17</tr>
18<tr>
19<td>Gulp Version</td>
20<td>3.x</td>
21</tr>
22</table>
23
24## Usage
25### Install
26 npm install gulp-git --save
27
28#### 0.4.0 introduced Breaking Changes!
29Git actions which did not require a [Vinyl](https://github.com/wearefractal/vinyl) file were refactored.
30Please review the following docs for changes:
31##Example
32
33```javascript
34var gulp = require('gulp');
35var git = require('gulp-git');
36
37// Run git init
38// src is the root folder for git to initialize
39gulp.task('init', function(){
40 git.init(function (err) {
41 if (err) throw err;
42 });
43});
44
45// Run git init with options
46gulp.task('init', function(){
47 git.init({args: '--quiet --bare'}, function (err) {
48 if (err) throw err;
49 });
50});
51
52// Run git add
53// src is the file(s) to add (or ./*)
54gulp.task('add', function(){
55 return gulp.src('./git-test/*')
56 .pipe(git.add());
57});
58
59// Run git add with options
60gulp.task('add', function(){
61 return gulp.src('./git-test/*')
62 .pipe(git.add({args: '-f -i -p'}));
63});
64
65// Run git commit
66// src are the files to commit (or ./*)
67gulp.task('commit', function(){
68 return gulp.src('./git-test/*')
69 .pipe(git.commit('initial commit'));
70});
71
72// Run git commit with a computed commit message
73gulp.task('commit', function(){
74 let newVersion;
75 function computeNewVersion() { newVersion = /* ... */ }
76 return gulp.src('./git-test/*')
77 .pipe(computeNewVersion())
78 .pipe(git.commit(() => `Bumps to version ${newVersion}`));
79});
80
81// Run git commit with options
82gulp.task('commit', function(){
83 return gulp.src('./git-test/*')
84 .pipe(git.commit('initial commit', {args: '-A --amend -s'}));
85});
86
87// Run git commit without checking for a message using raw arguments
88gulp.task('commit', function(){
89 return gulp.src('./git-test/*')
90 .pipe(git.commit(undefined, {
91 args: '-m "initial commit"',
92 disableMessageRequirement: true
93 }));
94});
95
96// Run git commit without appending a path to the commits
97gulp.task('commit', function(){
98 return gulp.src('./git-test/*')
99 .pipe(git.commit('initial commit', {
100 disableAppendPaths: true
101 }));
102});
103
104// Run git commit, passing multiple messages as if calling
105// git commit -m "initial commit" -m "additional message"
106gulp.task('commit', function(){
107 return gulp.src('./git-test/*')
108 .pipe(git.commit(['initial commit', 'additional message']));
109});
110
111// Run git commit, emiting 'data' event during progress
112// This is useful when you have long running githooks
113// and want to show progress to your users on screen
114gulp.task('commit', function(){
115 return gulp.src('./git-test/*')
116 .pipe(git.commit('initial commit', {emitData:true}))
117 .on('data',function(data) {
118 console.log(data);
119 });
120});
121
122// Run git remote add
123// remote is the remote repo
124// repo is the https url of the repo
125gulp.task('addremote', function(){
126 git.addRemote('origin', 'https://github.com/stevelacy/git-test', function (err) {
127 if (err) throw err;
128 });
129});
130
131// Run git remote remove
132// remote is the remote repo
133gulp.task('removeremote', function(){
134 git.removeRemote('origin', function (err) {
135 if (err) throw err;
136 });
137});
138
139// Run git push
140// remote is the remote repo
141// branch is the remote branch to push to
142gulp.task('push', function(){
143 git.push('origin', 'master', function (err) {
144 if (err) throw err;
145 });
146});
147
148// Run git push
149// branch is the current branch & remote branch to push to
150gulp.task('push', function(){
151 git.push('origin', function (err) {
152 if (err) throw err;
153 });
154});
155
156// Run git push with options
157// branch is the remote branch to push to
158gulp.task('push', function(){
159 git.push('origin', 'master', {args: " -f"}, function (err) {
160 if (err) throw err;
161 });
162});
163
164// Run git push with multiple branches and tags
165gulp.task('push', function(){
166 git.push('origin', ['master', 'develop'], {args: " --tags"}, function (err) {
167 if (err) throw err;
168 });
169});
170
171// Run git pull
172// remote is the remote repo
173// branch is the remote branch to pull from
174gulp.task('pull', function(){
175 git.pull('origin', 'master', {args: '--rebase'}, function (err) {
176 if (err) throw err;
177 });
178});
179
180// Run git pull from multiple branches
181gulp.task('pull', function(){
182 git.pull('origin', ['master', 'develop'], function (err) {
183 if (err) throw err;
184 });
185});
186
187// Run git fetch
188// Fetch refs from all remotes
189gulp.task('fetch', function(){
190 git.fetch('', '', {args: '--all'}, function (err) {
191 if (err) throw err;
192 });
193});
194
195// Run git fetch
196// Fetch refs from origin
197gulp.task('fetch', function(){
198 git.fetch('origin', '', function (err) {
199 if (err) throw err;
200 });
201});
202
203// Clone a remote repo
204gulp.task('clone', function(){
205 git.clone('https://github.com/stevelacy/gulp-git', function (err) {
206 if (err) throw err;
207 });
208});
209
210// Clone remote repo to sub folder ($CWD/sub/folder/git-test)
211gulp.task('clonesub', function() {
212 git.clone('https://github.com/stevelacy/git-test', {args: './sub/folder'}, function(err) {
213 // handle err
214 });
215});
216
217// Tag the repo with a version
218gulp.task('tag', function(){
219 git.tag('v1.1.1', 'Version message', function (err) {
220 if (err) throw err;
221 });
222});
223
224// Tag the repo with a version and empty message
225gulp.task('tag', function(){
226 git.tag('v1.1.1', '', function (err) {
227 if (err) throw err;
228 });
229});
230
231// Tag the repo With signed key
232gulp.task('tagsec', function(){
233 git.tag('v1.1.1', 'Version message with signed key', {signed: true}, function (err) {
234 if (err) throw err;
235 });
236});
237
238// Create a git branch
239gulp.task('branch', function(){
240 git.branch('newBranch', function (err) {
241 if (err) throw err;
242 });
243});
244
245// Checkout a git branch
246gulp.task('checkout', function(){
247 git.checkout('branchName', function (err) {
248 if (err) throw err;
249 });
250});
251
252// Create and switch to a git branch
253gulp.task('checkout', function(){
254 git.checkout('branchName', {args:'-b'}, function (err) {
255 if (err) throw err;
256 });
257});
258
259// Merge branches to master
260gulp.task('merge', function(){
261 git.merge('branchName', function (err) {
262 if (err) throw err;
263 });
264});
265
266// Reset a commit
267gulp.task('reset', function(){
268 git.reset('SHA', function (err) {
269 if (err) throw err;
270 });
271});
272
273// Show the formatted git diff
274gulp.task('diff', function(){
275 gulp.src('./*')
276 .pipe(git.diff('master', {log: true}))
277 .pipe(gulp.dest('./diff.out'));
278});
279
280// Git rm a file or folder
281gulp.task('rm', function(){
282 return gulp.src('./gruntfile.js')
283 .pipe(git.rm());
284});
285
286gulp.task('addSubmodule', function(){
287 git.addSubmodule('https://github.com/stevelacy/git-test', 'git-test', { args: '-b master'});
288});
289
290gulp.task('updateSubmodules', function(){
291 git.updateSubmodule({ args: '--init' });
292});
293
294// Working tree status
295gulp.task('status', function(){
296 git.status({args: '--porcelain'}, function (err, stdout) {
297 if (err) throw err;
298 });
299});
300
301// Other actions that do not require a Vinyl
302gulp.task('log', function(){
303 git.exec({args : 'log --follow index.js'}, function (err, stdout) {
304 if (err) throw err;
305 });
306});
307
308// Git clean files
309gulp.task('clean', function() {
310 git.clean({ args: '-f' }, function (err) {
311 if(err) throw err;
312 });
313});
314
315// Get the current branch name
316
317git.revParse({args:'--abbrev-ref HEAD'}, function (err, branch) {
318 console.log('current git branch: ' + branch);
319});
320
321// Run gulp's default task
322gulp.task('default',['add']);
323```
324
325##API
326
327### git.init(opt, cb)
328`git init`
329
330Creates an empty git repo
331
332`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
333
334`cb`: function, passed err if any
335
336```js
337git.init({args:'options'}, function (err) {
338 //if (err) ...
339});
340```
341
342### git.clone(remote, opt, cb)
343`git clone <remote> <options>`
344
345Clones a remote repo for the first time
346
347`remote`: String, remote url
348
349`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
350
351`cb`: function, passed err if any
352
353```js
354git.clone('https://remote.git', function (err) {
355 //if (err) ...
356});
357```
358A desination folder or subfolder can be set with `args: '<destination>'`
359```
360git.clone('https://remote.git', {args: './sub/folder'}, function (err) {
361 //if (err) ...
362});
363```
364
365### git.add(opt)
366`git add <files>`
367
368Adds files to repo
369
370`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024, maxFiles: Infinity}`
371
372```js
373gulp.src('./*')
374 .pipe(git.add());
375});
376```
377
378### git.commit(message, opt)
379`git commit -m <message> <files>`
380
381Commits changes to repo
382
383`message`: String or array of strings, commit message
384
385`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', maxBuffer: 200 * 1024, quiet: true, disableMessageRequirement: false, disableAppendPaths: false, multiline: false}`
386
387```js
388gulp.src('./*')
389 .pipe(git.commit('commit message'));
390});
391```
392
393### git.addRemote(remote, url, opt, cb)
394`git remote add <remote> <repo https url>`
395
396Adds remote repo url
397
398`remote`: String, name of remote, default: `origin`
399
400`url`: String, url of remote
401
402`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
403
404`cb`: function, passed err if any
405
406```js
407git.addRemote('origin', 'git-repo-url', function (err) {
408 //if (err) ...
409});
410```
411
412### git.removeRemote(remote, opt, cb)
413`git remote remove <remote>`
414
415Removes remote repo
416
417`remote`: String, name of remote
418
419`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
420
421`cb`: function, passed err if any
422
423```js
424git.removeRemote('origin', function (err) {
425 //if (err) ...
426});
427```
428
429### git.fetch(remote, branch, opt, cb)
430`git fetch <remote> <branch>`
431
432Fetches refs and objects from remote repo
433
434`remote`: String, name of remote, default: `origin`
435
436`branch`: String, branch, default: ``
437
438`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
439
440`cb`: function, passed err if any
441
442```js
443git.fetch('origin', '', function (err) {
444 //if (err) ...
445});
446```
447
448### git.pull(remote, branch, opt, cb)
449`git pull <remote> <branch>`
450
451Pulls changes from remote repo
452
453`remote`: String, name of remote, default: `undefined`
454
455`branch`: String || Array, branch, default: `undefined`
456
457`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
458
459`cb`: function, passed err if any
460
461```js
462git.pull('origin', 'master', function (err) {
463 //if (err) ...
464});
465
466// without any remote or branches
467git.pull(function(err){
468 //if (err) ...
469});
470
471// with only a remote
472git.pull('upstream', function(err){
473 //if (err) ...
474});
475
476// with remote and an array of branches
477git.pull('upstream' ['dev', 'master'], function(err){
478 //if (err) ...
479});
480```
481
482### git.push(remote, branch, opt, cb)
483`git push <remote> <branch>`
484
485Pushes changes to remote repo
486
487`remote`: String, name of remote, default: `origin`
488
489`branch`: String (may be `null`), branch, default: `master`
490
491`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true}`
492
493`cb`: function, passed err if any
494
495```js
496git.push('origin', 'master', function (err) {
497 //if (err) ...
498});
499```
500
501### git.tag(version, message, opt, cb)
502`git tag -a/s <version> -m <message>`
503
504Tags repo with release version, returns all tags when used without arguments
505
506`version`: String (optional), tag name
507
508`message`: String or array of strings (optional), tag message
509
510`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
511
512`cb`: function, passed err if any
513
514```js
515git.tag('v1.1.1', 'Version message', function (err) {
516 //if (err) ...
517});
518```
519
520if options.signed is set to true, the tag will use the git secure key:
521`git.tag('v1.1.1', 'Version message with signed key', {signed: true});`
522
523### git.branch(branch, opt, cb)
524`git branch <new branch name>`
525
526Creates a new branch but doesn't switch to it
527
528(Want to switch as you create? Use `git.checkout({args:'-b'})`.)
529
530`branch`: String, branch
531
532`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
533
534`cb`: function, passed err if any
535
536```js
537git.branch('development', function (err) {
538 //if (err) ...
539});
540```
541
542### git.showBranch(opt, cb)
543`git show-branch <opt>`
544
545Show branches and their commits
546
547`opt`: Object (optional) `{args: 'options'}`
548
549`cb`: function, passed err if any
550
551```js
552git.showBranch({'args': '--list -a'}, function (err) {
553 //if (err) ...
554});
555```
556
557### git.checkout(branch, opt, cb)
558`git checkout <new branch name>`
559
560Checkout a new branch with files
561
562`branch`: String, branch
563
564`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
565
566`cb`: function, passed err if any
567
568```js
569git.checkout('development', function (err) {
570 //if (err) ...
571});
572```
573
574If you want to create a branch and switch to it:
575
576```js
577git.checkout('development', {args:'-b'}, function (err) {
578 //if (err) ...
579});
580```
581
582If you want to checkout files (e.g. revert them) use git.checkoutFiles:
583
584```js
585gulp.src('./*')
586 .pipe(git.checkoutFiles());
587```
588
589### git.checkoutFiles(opt)
590`git checkout <list of files>`
591
592Checkout (e.g. reset) files
593
594`opt`: Object (optional) `{args: 'options', quiet: true, maxBuffer: 200 * 1024}`
595
596```js
597gulp.src('./*')
598 .pipe(git.checkoutFiles());
599```
600
601### git.merge(branch, opt, cb)
602`git merge <branch name> <options>`
603
604Merges a branch into the current branch
605
606`branch`: String, source branch
607
608`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
609
610`cb`: function, passed err if any
611
612```js
613git.merge('development', function (err) {
614 //if (err) ...
615});
616```
617
618### git.rm()
619`git rm <file> <options>`
620
621Removes a file from git and deletes it
622
623`opt`: Object (optional) `{args: 'options', quiet: true, maxBuffer: 200 * 1024}`
624
625```js
626gulp.src('./*')
627 .pipe(git.commit('commit message'));
628});
629```
630
631### git.reset(commit, opt, cb)
632`git reset <SHA> <options>`
633
634Resets working directory to specified commit hash
635
636`commit`: String, commit hash or reference
637
638`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
639
640`cb`: function, passed err if any
641
642```js
643git.reset('HEAD' {args:'--hard'}, function (err) {
644 //if (err) ...
645});
646```
647
648### git.revParse(opt, cb)
649`git rev-parse <options>`
650
651Get details about the repository
652
653`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
654
655`cb`: function, passed err if any and command stdout
656
657
658```js
659git.revParse({args:'--short HEAD'}, function (err, hash) {
660 //if (err) ...
661 console.log('current git hash: '+hash);
662});
663```
664
665### git.addSubmodule()
666`git submodule add <options> <repository> <path>`
667
668Options: Object
669
670`.addSubmodule('https://repository.git', 'path', {args: "options", quiet: true})`
671
672### git.updateSubmodule()
673`git submodule update <options>`
674
675Options: Object
676
677`.updateSubmodule({args: "options", quiet: true})`
678
679
680### git.status(opt, cb)
681`git status <options>`
682
683Show the working tree status
684
685`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', maxBuffer: 200 * 1024, quiet: true}`
686
687`cb`: function (optional), passed err and command stdout
688
689```js
690git.status({args : '--porcelain'}, function (err, stdout) {
691 // if (err) ...
692});
693```
694
695### git.exec(opt, cb)
696`git <options>`
697
698Run other git actions that do not require a Vinyl.
699
700`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', maxBuffer: 200 * 1024, quiet: true}`
701
702`cb`: function (optional), passed err and command stdout
703
704```js
705git.exec({args : 'log --follow index.js'}, function (err, stdout) {
706 //if (err) ...
707});
708```
709
710### git.clean(paths, opt, cb)
711`git clean <options>`
712
713Remove untracked files from the working tree
714
715`paths`: String (optional), paths to be affected by clean operation
716
717`opt`: Object (optional), `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
718
719`cb`: function (optional), passed err if any
720
721### git.diff(branch, opt)
722`git diff master <options>`
723
724Diffs between git objects
725
726`branch`: String, branch name, commit name, or git tag
727
728`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}`
729
730```js
731gulp.task('diff', function(){
732 gulp.src('./*')
733 .pipe(git.diff('develop', {log: true}))
734 .pipe(gulp.dest('./diff.out'));
735});
736```
737
738#### You can view more examples in the [example folder.](https://github.com/stevelacy/gulp-git/tree/master/examples)
739
740<br/>
741<br/>
742
743### Possible errors:
744
745#### stdout maxBuffer exceeded
746
747Reported [here](https://github.com/stevelacy/gulp-git/issues/68).
748
749If you get this error it means that the git process doesn't have enough memory.
750
751Every function has an additional option: `maxBuffer`.
752
753```js
754gulp.task('pull', function(){
755 git.pull('origin', 'master', {args: '--rebase', maxBuffer: Infinity}, function (err) {
756 if (err) throw err;
757 });
758});
759```
760
761#### The command line is too long.
762
763Reported [here](https://github.com/stevelacy/gulp-git/issues/201).
764
765If the `git add` command exceeds [8191 characters on Windows](https://docs.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/command-line-string-limitation#more-information) you will get this error.
766
767The `add` function has an additional option: `maxFiles`.
768
769```js
770gulp.task('add', function(){
771 return gulp.src('./git-test/*')
772 .pipe(git.add({maxFiles: 8}));
773});
774```
775
776
777
778## LICENSE
779
780(MIT License)
781
782Copyright (c) 2015 Steve Lacy <me@slacy.me> slacy.me
783
784Permission is hereby granted, free of charge, to any person obtaining
785a copy of this software and associated documentation files (the
786"Software"), to deal in the Software without restriction, including
787without limitation the rights to use, copy, modify, merge, publish,
788distribute, sublicense, and/or sell copies of the Software, and to
789permit persons to whom the Software is furnished to do so, subject to
790the following conditions:
791
792The above copyright notice and this permission notice shall be
793included in all copies or substantial portions of the Software.
794
795THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
796EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
797MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
798NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
799LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
800OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
801WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.