-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathpush.rb
69 lines (56 loc) · 1.73 KB
/
push.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
difficulty 3
description "Your local master branch has diverged from " +
"the remote origin/master branch. Rebase your branch onto " +
"origin/master and push it to remote."
setup do
# remember the working directory so we can come back to it later
cwd = Dir.pwd
# initialize another git repo to be used as a "remote"
tmpdir = Dir.mktmpdir
# local repo
repo.init
FileUtils.touch "file1"
repo.add "file1"
repo.commit_all "First commit"
FileUtils.touch "file2"
repo.add "file2"
repo.commit_all "Second commit"
# copy the repo to remote
FileUtils.cp "file1", tmpdir
FileUtils.cp "file2", tmpdir
# add another file
FileUtils.touch "file3"
repo.add "file3"
repo.commit_all "Third commit"
system "git branch -m master"
# remote repo
Dir.chdir tmpdir
repo.init
# make a 'non-bare' repo accept pushes
`git config receive.denyCurrentBranch ignore`
# add a different file and commit so remote and local would diverge
FileUtils.touch "file4"
repo.add "file4"
repo.commit_all "Fourth commit"
system "git branch -m master" # tentative addition
# change back to original repo to set up a remote
Dir.chdir cwd
`git remote add origin #{tmpdir}/.git`
`git fetch origin`
`git branch -u origin/master master 2> /dev/null`
end
solution do
repo.init
result = true
# Check the commits of the local branch and the branch are the same.
local_commits = repo.commits("master")
remote_commits = repo.commits("origin/master")
result = false unless local_commits.size == 4
local_commits.each_with_index do |commit, idx|
result &&= (commit.id == remote_commits[idx].id)
end
result
end
hint do
puts "Take a look at `git fetch`, `git pull`, and `git push`."
end