Skip to content

Commit

Permalink
Merge pull request #277 from dplusic/feature/no-history
Browse files Browse the repository at this point in the history
cli: add `--no-history` flag not to preserve deploy history
  • Loading branch information
tschaub authored Jan 7, 2020
2 parents 0249ac9 + e73d921 commit 6b87c84
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions bin/gh-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +99,7 @@ function main(args) {
only: program.remove,
remote: program.remote,
push: !!program.push,
history: !!program.history,
user: user
};

Expand Down
17 changes: 15 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exports.defaults = {
src: '**/*',
only: '.',
push: true,
history: true,
message: 'Updates',
silent: false
};
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ ghpages.publish('dist', {push: false}, callback);
```


#### <a id="optionshistory">options.history</a>
* type: `boolean`
* default: `true`

Push force new commit without parent history.

Example use of the `history` option:

```js
ghpages.publish('dist', {history: false}, callback);
```


#### <a id="optionssilent">options.silent</a>
* type: `boolean`
* default: `false`
Expand Down
5 changes: 5 additions & 0 deletions test/bin/gh-pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 6b87c84

Please sign in to comment.