Skip to content

Commit 4c03b8d

Browse files
committed
Implement force push
1 parent 582cfcd commit 4c03b8d

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

js/controlbox.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,20 @@ function(_yargs, d3, demos) {
733733
}.bind(this), 750);
734734
},
735735

736-
push: function(args) {
736+
push: function(args, opts, cmdStr) {
737+
var opt = yargs(cmdStr, {
738+
alias: { force: ['f'] },
739+
boolean: ['f']
740+
})
741+
737742
if (this.mode !== 'local') {
738743
throw new Error('can only push from local')
739744
}
740745
var control = this,
741746
local = this.historyView,
742-
remoteName = args.shift() || 'origin',
747+
remoteName = opt._[0] || 'origin',
743748
remote = this[remoteName + 'View'],
744-
branchArgs = args.pop(),
749+
branchArgs = opt._[1],
745750
localRef = local.currentBranch,
746751
remoteRef = local.currentBranch,
747752
localCommit, remoteCommit,
@@ -794,7 +799,7 @@ function(_yargs, d3, demos) {
794799

795800
// push to an existing branch on the remote
796801
if (remoteCommit && remote.branches.indexOf(remoteRef) > -1) {
797-
if (!local.isAncestorOf(remoteCommit.id, localCommit.id)) {
802+
if (!local.isAncestorOf(remoteCommit.id, localCommit.id) && !opt.f) {
798803
throw new Error('Push rejected. Non fast-forward.');
799804
}
800805

@@ -804,9 +809,21 @@ function(_yargs, d3, demos) {
804809
return this.info('Everything up-to-date.');
805810
}
806811

807-
findCommitsToPush(localCommit);
812+
if (!opt.f) {
813+
findCommitsToPush(localCommit);
814+
remote.commitData = remote.commitData.concat(toPush);
815+
} else {
816+
var localData = JSON.parse(JSON.stringify(local.commitData))
817+
localData.forEach(function(commit) {
818+
var originTagIndex = commit.tags.indexOf('origin/' + localRef)
819+
if (originTagIndex > -1) {
820+
commit.tags.splice(originTagIndex, 1)
821+
}
822+
})
823+
remote.commitData = localData
824+
this.info('forced update')
825+
}
808826

809-
remote.commitData = remote.commitData.concat(toPush);
810827
remote.moveTag(remoteRef, localCommit.id);
811828
local.moveTag('origin/' + localRef, localRef)
812829
remote.renderCommits();

0 commit comments

Comments
 (0)