Skip to content

Commit 20ec83a

Browse files
authored
Merge pull request git-school#80 from git-school/ku-changes
Implement force-push, add scenario for rewritten history on remote
2 parents 940b915 + dc2d657 commit 20ec83a

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
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();

js/demos.js

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,112 @@ define([], function () {
6969
]
7070
}
7171

72+
var rewrittenHistory = {
73+
title: 'Rewritten Remote History',
74+
key: 'rewritten-history',
75+
message: 'Someone force-pushed and re-wrote history on the remote!',
76+
currentBranch: "feature",
77+
commitData: [
78+
{
79+
"id": "e137e9b",
80+
"tags": [],
81+
"message": "first commit",
82+
"parent": "initial",
83+
"cx": 50,
84+
"cy": 330,
85+
"branchless": false
86+
},
87+
{
88+
"id": "84c98fe",
89+
"parent": "e137e9b",
90+
"tags": [
91+
"master",
92+
"origin/master"
93+
],
94+
"cx": 140,
95+
"cy": 330,
96+
"branchless": false
97+
},
98+
{
99+
"id": "1c016b6",
100+
"parent": "e137e9b",
101+
"tags": [],
102+
"cx": 140,
103+
"cy": 240,
104+
"branchless": false
105+
},
106+
{
107+
"id": "fd0af32",
108+
"parent": "1c016b6",
109+
"tags": [],
110+
"cx": 230,
111+
"cy": 240,
112+
"branchless": false
113+
},
114+
{
115+
"id": "5041e4c",
116+
"tags": [
117+
"feature",
118+
"origin/feature",
119+
"HEAD"
120+
],
121+
"parent": "fd0af32",
122+
"cx": 320,
123+
"cy": 240,
124+
"branchless": false
125+
}
126+
],
127+
originData: [
128+
{
129+
"id": "e137e9b",
130+
"tags": [],
131+
"message": "first commit",
132+
"parent": "initial",
133+
"cx": 50,
134+
"cy": 360,
135+
"branchless": false
136+
},
137+
{
138+
"id": "84c98fe",
139+
"parent": "e137e9b",
140+
"tags": [
141+
"master"
142+
],
143+
"cx": 140,
144+
"cy": 360,
145+
"branchless": false
146+
},
147+
{
148+
"id": "1c016b6",
149+
"parent": "e137e9b",
150+
"tags": [],
151+
"cx": 140,
152+
"cy": 270,
153+
"branchless": false
154+
},
155+
{
156+
"id": "fd0af32",
157+
"tags": [
158+
"feature",
159+
"HEAD"
160+
],
161+
"parent": "1c016b6",
162+
"cx": 230,
163+
"cy": 270,
164+
"branchless": false
165+
},
166+
{
167+
"id": "5041e4c",
168+
"tags": [],
169+
"parent": "fd0af32",
170+
"cx": 320,
171+
"cy": 270,
172+
"branchless": true
173+
}
174+
]
175+
176+
}
177+
72178
var cherryPick = {
73179
title: 'Cherry Picking',
74180
key: 'cherry',
@@ -145,6 +251,6 @@ define([], function () {
145251
}
146252

147253
return [
148-
free, freeWithRemote, upstreamChanges, cherryPick
254+
free, freeWithRemote, upstreamChanges, rewrittenHistory, cherryPick
149255
]
150256
})

js/explaingit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ define(['historyview', 'controlbox', 'd3'], function(HistoryView, ControlBox, d3
2626
name: name + '-Origin',
2727
width: 300,
2828
height: 400,
29-
commitRadius: 15,
29+
commitRadius: args.commitRadius,
3030
remoteName: 'origin',
3131
commitData: args.originData,
3232
savedState: args.ovSavedState

0 commit comments

Comments
 (0)