Skip to content

Commit 5af4021

Browse files
taufeksds
authored andcommitted
Update PrePush Modified Files and Lines with Multiple Branches Push
In `pre-push` hook, it receives multiple ref ranges when we pushed multiple branches to remote. Below is pushing multiple branches plus setting up the upstream branch on remote. ``` command: git push origin -u tj-test tj-test-2 stdin: refs/heads/tj-test 62738e2 refs/heads/tj-test 0000000000000000000000000000000000000000 refs/heads/tj-test-2 feb40b5 refs/heads/tj-test-2 0000000000000000000000000000000000000000 ``` Below is pushing multiple branches which already has upstream branch on remote. ``` command: git push origin tj-test tj-test-2 stdin: refs/heads/tj-test 61bc13ab2e717633441855d534904c5036f342dd refs/heads/tj-test 62738e2 refs/heads/tj-test-2 1e371d2c345e00c34410372dad2d7602aca99908 refs/heads/tj-test-2 feb40b5 ``` With these multiple ref ranges, we should read all of them during pre-push `modified_files` and `modified_lines_in_file`.
1 parent 2594697 commit 5af4021

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

lib/overcommit/hook_context/pre_push.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ def pushed_refs
1818
end
1919

2020
def modified_files
21-
@modified_files ||= Overcommit::GitRepo.modified_files(refs: ref_range)
21+
@modified_files ||= pushed_refs.map(&:modified_files).flatten.uniq
2222
end
2323

2424
def modified_lines_in_file(file)
2525
@modified_lines ||= {}
26-
@modified_lines[file] =
27-
Overcommit::GitRepo.extract_modified_lines(file, refs: ref_range)
28-
end
29-
30-
def ref_range
31-
"#{pushed_refs[0].remote_sha1}..#{pushed_refs[0].local_sha1}"
26+
@modified_lines[file] = pushed_refs.each_with_object(Set.new) do |pushed_ref, set|
27+
set.merge(pushed_ref.modified_lines_in_file(file))
28+
end
3229
end
3330

3431
PushedRef = Struct.new(:local_ref, :local_sha1, :remote_ref, :remote_sha1) do
@@ -48,12 +45,24 @@ def destructive?
4845
deleted? || forced?
4946
end
5047

48+
def modified_files
49+
Overcommit::GitRepo.modified_files(refs: ref_range)
50+
end
51+
52+
def modified_lines_in_file(file)
53+
Overcommit::GitRepo.extract_modified_lines(file, refs: ref_range)
54+
end
55+
5156
def to_s
5257
"#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}"
5358
end
5459

5560
private
5661

62+
def ref_range
63+
"#{remote_sha1}..#{local_sha1}"
64+
end
65+
5766
def overwritten_commits
5867
return @overwritten_commits if defined? @overwritten_commits
5968
result = Overcommit::Subprocess.spawn(%W[git rev-list #{remote_sha1} ^#{local_sha1}])

spec/overcommit/hook_context/pre_push_spec.rb

+92-17
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,55 @@
8787
`git add . 2>&1 > #{File::NULL}`
8888
`git commit -m "Update Branch 2" 2>&1 > #{File::NULL}`
8989

90-
should include(*%w[added-1 update-me added-2].map { |file| File.expand_path(file) })
90+
should == %w[added-1 added-2 update-me].map { |file| File.expand_path(file) }
91+
should_not include(*%w[delete-me].map { |file| File.expand_path(file) })
92+
end
93+
end
94+
end
95+
96+
context 'when pushing multiple branches at once' do
97+
let(:local_ref_1) { 'refs/heads/project-branch-1' }
98+
let(:local_sha1_1) { get_sha1(local_ref_1) }
99+
let(:local_ref_2) { 'refs/heads/project-branch-2' }
100+
let(:local_sha1_2) { get_sha1(local_ref_2) }
101+
let(:remote_ref) { 'refs/remotes/origin/master' }
102+
let(:remote_sha1) { get_sha1(remote_ref) }
103+
let(:input) do
104+
double('input', read: ref_ranges)
105+
end
106+
let(:ref_ranges) do
107+
[
108+
"#{local_ref_1} #{local_sha1_1} #{remote_ref} #{remote_sha1}\n",
109+
"#{local_ref_2} #{local_sha1_2} #{remote_ref} #{remote_sha1}\n"
110+
].join
111+
end
112+
113+
it 'has modified files based on multiple tracking branches' do
114+
repo do
115+
`git remote add origin file://#{remote_repo}`
116+
`git fetch origin 2>&1 > #{File::NULL} && git reset --hard origin/master`
117+
118+
`git checkout -b project-branch-1 2>&1 > #{File::NULL}`
119+
`git push -u origin project-branch-1 2>&1 > #{File::NULL}`
120+
121+
touch 'added-1'
122+
echo 'add', 'added-1'
123+
echo 'append', 'update-me'
124+
FileUtils.rm 'delete-me'
125+
`git add . 2>&1 > #{File::NULL}`
126+
`git commit -m "Update Branch 1" 2>&1 > #{File::NULL}`
127+
128+
`git checkout master 2>&1 > #{File::NULL}`
129+
`git checkout -b project-branch-2 2>&1 > #{File::NULL}`
130+
`git push -u origin project-branch-2 2>&1 > #{File::NULL}`
131+
132+
echo 'append', 'update-me'
133+
touch 'added-2'
134+
echo 'add', 'added-2'
135+
`git add . 2>&1 > #{File::NULL}`
136+
`git commit -m "Update Branch 2" 2>&1 > #{File::NULL}`
137+
138+
should == %w[added-1 update-me added-2].map { |file| File.expand_path(file) }
91139
should_not include(*%w[delete-me].map { |file| File.expand_path(file) })
92140
end
93141
end
@@ -121,7 +169,7 @@
121169
`git add . 2>&1 > #{File::NULL}`
122170
`git commit -m "Update Branch 2" 2>&1 > #{File::NULL}`
123171

124-
should include(*%w[added-1 update-me added-2].map { |file| File.expand_path(file) })
172+
should == %w[added-1 added-2 update-me].map { |file| File.expand_path(file) }
125173
should_not include(*%w[delete-me].map { |file| File.expand_path(file) })
126174
end
127175
end
@@ -130,12 +178,20 @@
130178

131179
describe '#modified_lines_in_file' do
132180
subject { context.modified_lines_in_file(file) }
133-
let(:local_ref) { 'refs/heads/project-branch' }
134-
let(:local_sha1) { get_sha1(local_ref) }
181+
let(:local_ref_1) { 'refs/heads/project-branch-1' }
182+
let(:local_sha1_1) { get_sha1(local_ref_1) }
183+
let(:local_ref_2) { 'refs/heads/project-branch-2' }
184+
let(:local_sha1_2) { get_sha1(local_ref_2) }
135185
let(:remote_ref) { 'refs/remotes/origin/master' }
136186
let(:remote_sha1) { get_sha1(remote_ref) }
137187
let(:input) do
138-
double('input', read: "#{local_ref} #{local_sha1} #{remote_ref} #{remote_sha1}\n")
188+
double('input', read: ref_ranges)
189+
end
190+
let(:ref_ranges) do
191+
[
192+
"#{local_ref_1} #{local_sha1_1} #{remote_ref} #{remote_sha1}\n",
193+
"#{local_ref_2} #{local_sha1_2} #{remote_ref} #{remote_sha1}\n"
194+
].join
139195
end
140196
let(:remote_repo) do
141197
repo do
@@ -154,20 +210,28 @@
154210
`git remote add origin file://#{remote_repo}`
155211
`git fetch origin 2>&1 > #{File::NULL} && git reset --hard origin/master`
156212

157-
`git checkout -b project-branch 2>&1 > #{File::NULL}`
158-
`git push -u origin project-branch 2>&1 > #{File::NULL}`
213+
`git checkout -b project-branch-1 2>&1 > #{File::NULL}`
214+
`git push -u origin project-branch-1 2>&1 > #{File::NULL}`
159215

160216
echo 'append-1', 'initial_file', append: true
161217

162218
`git add . 2>&1 > #{File::NULL}`
163-
`git commit -m "Update Branch 1" 2>&1 > #{File::NULL}`
219+
`git commit -m "Update Branch 1 Commit 1" 2>&1 > #{File::NULL}`
164220

165221
echo 'append-2', 'initial_file', append: true
166222

167223
`git add . 2>&1 > #{File::NULL}`
168-
`git commit -m "Update Branch 2" 2>&1 > #{File::NULL}`
224+
`git commit -m "Update Branch 1 Commit 2" 2>&1 > #{File::NULL}`
169225

170-
should == [2, 3].to_set
226+
`git checkout -b project-branch-2 2>&1 > #{File::NULL}`
227+
`git push -u origin project-branch-2 2>&1 > #{File::NULL}`
228+
229+
echo 'append-3', 'initial_file', append: true
230+
231+
`git add . 2>&1 > #{File::NULL}`
232+
`git commit -m "Update Branch 2 Commit 1" 2>&1 > #{File::NULL}`
233+
234+
should == [2, 3, 4].to_set
171235
end
172236
end
173237
end
@@ -180,36 +244,47 @@
180244
`git remote add origin file://#{remote_repo}`
181245
`git fetch origin 2>&1 > #{File::NULL} && git reset --hard origin/master`
182246

183-
`git checkout -b project-branch 2>&1 > #{File::NULL}`
184-
`git push -u origin project-branch 2>&1 > #{File::NULL}`
247+
`git checkout -b project-branch-1 2>&1 > #{File::NULL}`
248+
`git push -u origin project-branch-1 2>&1 > #{File::NULL}`
185249

186250
touch 'new_file'
187251

188252
echo 'append-1', 'new_file', append: true
189253

190254
`git add . 2>&1 > #{File::NULL}`
191-
`git commit -m "Update Branch 1" 2>&1 > #{File::NULL}`
255+
`git commit -m "Update Branch 1 Commit 1" 2>&1 > #{File::NULL}`
192256

193257
echo 'append-2', 'new_file', append: true
194258

195259
`git add . 2>&1 > #{File::NULL}`
196-
`git commit -m "Update Branch 2" 2>&1 > #{File::NULL}`
260+
`git commit -m "Update Branch 1 Commit 2" 2>&1 > #{File::NULL}`
261+
262+
`git checkout -b project-branch-2 2>&1 > #{File::NULL}`
263+
`git push -u origin project-branch-2 2>&1 > #{File::NULL}`
197264

198-
should == [1, 2].to_set
265+
echo 'append-3', 'new_file', append: true
266+
267+
`git add . 2>&1 > #{File::NULL}`
268+
`git commit -m "Update Branch 2 Commit 1" 2>&1 > #{File::NULL}`
269+
270+
should == [1, 2, 3].to_set
199271
end
200272
end
201273
end
202274

203275
context 'when deleting a file' do
204276
let(:file) { File.expand_path('initial_file') }
277+
let(:ref_ranges) do
278+
"#{local_ref_1} #{local_sha1_1} #{remote_ref} #{remote_sha1}\n"
279+
end
205280

206281
it 'has modified lines in file' do
207282
repo do
208283
`git remote add origin file://#{remote_repo}`
209284
`git fetch origin 2>&1 > #{File::NULL} && git reset --hard origin/master`
210285

211-
`git checkout -b project-branch 2>&1 > #{File::NULL}`
212-
`git push -u origin project-branch 2>&1 > #{File::NULL}`
286+
`git checkout -b project-branch-1 2>&1 > #{File::NULL}`
287+
`git push -u origin project-branch-1 2>&1 > #{File::NULL}`
213288

214289
FileUtils.rm 'initial_file'
215290

0 commit comments

Comments
 (0)