From e73d9217aeb29d683bf0b869cadc3d2c3b589b36 Mon Sep 17 00:00:00 2001 From: Dongwoon Son Date: Sun, 30 Dec 2018 22:59:24 +0900 Subject: [PATCH] cli: add `--no-history` flag not to preserve deploy history --- bin/gh-pages.js | 5 +++++ lib/git.js | 17 +++++++++++++++-- lib/index.js | 10 +++++++++- readme.md | 13 +++++++++++++ test/bin/gh-pages.spec.js | 5 +++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/bin/gh-pages.js b/bin/gh-pages.js index 8ff652cc..07dc6d16 100755 --- a/bin/gh-pages.js +++ b/bin/gh-pages.js @@ -66,6 +66,10 @@ function main(args) { ghpages.defaults.only ) .option('-n, --no-push', 'Commit only (with no push)') + .option( + '-f, --no-history', + 'Push force new commit without parent history' + ) .parse(args); let user; @@ -95,6 +99,7 @@ function main(args) { only: program.remove, remote: program.remote, push: !!program.push, + history: !!program.history, user: user }; diff --git a/lib/git.js b/lib/git.js index 663fadb6..1aeb35fc 100644 --- a/lib/git.js +++ b/lib/git.js @@ -176,10 +176,15 @@ Git.prototype.tag = function(name) { * Push a branch. * @param {string} remote Remote alias. * @param {string} branch Branch name. + * @param {boolean} force Force push. * @return {Promise} A promise. */ -Git.prototype.push = function(remote, branch) { - return this.exec('push', '--tags', remote, branch); +Git.prototype.push = function(remote, branch, force) { + const args = ['push', '--tags', remote, branch]; + if (force) { + args.push('--force'); + } + return this.exec.apply(this, args); }; /** @@ -212,6 +217,14 @@ Git.prototype.getRemoteUrl = function(remote) { }); }; +/** + * Delete ref to remove branch history + * @param {string} branch + */ +Git.prototype.deleteRef = function(branch) { + return this.exec('update-ref', '-d', 'refs/heads/' + branch); +}; + /** * Clone a repo into the given dir if it doesn't already exist. * @param {string} repo Repository URL. diff --git a/lib/index.js b/lib/index.js index 248aa05e..333e6903 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,6 +33,7 @@ exports.defaults = { src: '**/*', only: '.', push: true, + history: true, message: 'Updates', silent: false }; @@ -142,6 +143,13 @@ exports.publish = function publish(basePath, config, callback) { log('Checking out %s/%s ', options.remote, options.branch); return git.checkout(options.remote, options.branch); }) + .then(git => { + if (!options.history) { + return git.deleteRef(options.branch); + } else { + return git; + } + }) .then(git => { if (!options.add) { log('Removing files'); @@ -193,7 +201,7 @@ exports.publish = function publish(basePath, config, callback) { .then(git => { if (options.push) { log('Pushing'); - return git.push(options.remote, options.branch); + return git.push(options.remote, options.branch, !options.history); } else { return git; } diff --git a/readme.md b/readme.md index 45cdb94d..cf824958 100644 --- a/readme.md +++ b/readme.md @@ -241,6 +241,19 @@ ghpages.publish('dist', {push: false}, callback); ``` +#### options.history + * type: `boolean` + * default: `true` + +Push force new commit without parent history. + +Example use of the `history` option: + +```js +ghpages.publish('dist', {history: false}, callback); +``` + + #### options.silent * type: `boolean` * default: `false` diff --git a/test/bin/gh-pages.spec.js b/test/bin/gh-pages.spec.js index bd84ae56..fea42be1 100644 --- a/test/bin/gh-pages.spec.js +++ b/test/bin/gh-pages.spec.js @@ -26,6 +26,11 @@ describe('gh-pages', () => { dist: 'lib', config: {push: false} }, + { + args: ['--dist', 'lib', '-f'], + dist: 'lib', + config: {history: false} + }, { args: ['--dist', 'lib', '-x'], dist: 'lib',