Skip to content

Commit 5d19f58

Browse files
jawshooahsds
authored andcommittedApr 9, 2015
Restore modified times for both staged and unstaged files
1 parent 8ad39fe commit 5d19f58

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed
 

‎lib/overcommit/hook_context/pre_commit.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ def initial_commit?
170170
def store_modified_times
171171
@modified_times = {}
172172

173-
modified_files.each do |file|
173+
staged_files = modified_files
174+
unstaged_files = Overcommit::GitRepo.modified_files(staged: false)
175+
176+
(staged_files + unstaged_files).each do |file|
174177
next if Overcommit::Utils.broken_symlink?(file)
175178
@modified_times[file] = File.mtime(file)
176179
end
@@ -179,10 +182,9 @@ def store_modified_times
179182
# Restores the file modification times for all modified files to make it
180183
# appear like they never changed.
181184
def restore_modified_times
182-
modified_files.each do |file|
185+
@modified_times.each do |file, time|
183186
next if Overcommit::Utils.broken_symlink?(file)
184187
next unless File.exist?(file)
185-
time = @modified_times[file]
186188
File.utime(time, time, file)
187189
end
188190
end

‎spec/overcommit/hook_context/pre_commit_spec.rb

+55-15
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@
7171
around do |example|
7272
repo do
7373
`echo "Hello World" > tracked-file`
74-
`git add tracked-file`
75-
`git commit -m "Add tracked-file"`
74+
`echo "Hello Other World" > other-tracked-file`
75+
`git add tracked-file other-tracked-file`
76+
`git commit -m "Add tracked-file and other-tracked-file"`
7677
`echo "Hello Again" > untracked-file`
78+
`echo "Some more text" >> other-tracked-file`
7779
example.run
7880
end
7981
end
@@ -83,25 +85,38 @@
8385
File.open('tracked-file', 'r').read.should == "Hello World\n"
8486
end
8587

88+
it 'does not keep unstaged changes' do
89+
subject
90+
File.open('other-tracked-file', 'r').read.should == "Hello Other World\n"
91+
end
92+
8693
it 'keeps untracked files' do
8794
subject
8895
File.open('untracked-file', 'r').read.should == "Hello Again\n"
8996
end
9097

9198
it 'keeps modification times the same' do
92-
expect { subject }.
93-
to_not change { [File.mtime('tracked-file'), File.mtime('untracked-file')] }
99+
sleep 1
100+
expect { subject }.to_not change {
101+
[
102+
File.mtime('tracked-file'),
103+
File.mtime('other-tracked-file'),
104+
File.mtime('untracked-file')
105+
]
106+
}
94107
end
95108
end
96109

97110
context 'when there are staged changes' do
98111
around do |example|
99112
repo do
100113
`echo "Hello World" > tracked-file`
101-
`git add tracked-file`
102-
`git commit -m "Add tracked-file"`
114+
`echo "Hello Other World" > other-tracked-file`
115+
`git add tracked-file other-tracked-file`
116+
`git commit -m "Add tracked-file and other-tracked-file"`
103117
`echo "Hello Again" > untracked-file`
104118
`echo "Some more text" >> tracked-file`
119+
`echo "Some more text" >> other-tracked-file`
105120
`git add tracked-file`
106121
`echo "Yet some more text" >> tracked-file`
107122
example.run
@@ -110,7 +125,12 @@
110125

111126
it 'keeps staged changes' do
112127
subject
113-
`git show :tracked-file`.should == "Hello World\nSome more text\n"
128+
File.open('tracked-file', 'r').read.should == "Hello World\nSome more text\n"
129+
end
130+
131+
it 'does not keep unstaged changes' do
132+
subject
133+
File.open('other-tracked-file', 'r').read.should == "Hello Other World\n"
114134
end
115135

116136
it 'keeps untracked files' do
@@ -119,8 +139,14 @@
119139
end
120140

121141
it 'keeps modification times the same' do
122-
expect { subject }.
123-
to_not change { [File.mtime('tracked-file'), File.mtime('untracked-file')] }
142+
sleep 1
143+
expect { subject }.to_not change {
144+
[
145+
File.mtime('tracked-file'),
146+
File.mtime('other-tracked-file'),
147+
File.mtime('untracked-file')
148+
]
149+
}
124150
end
125151
end
126152

@@ -202,19 +228,27 @@
202228
end
203229

204230
it 'keeps modification times the same' do
205-
expect { subject }.
206-
to_not change { [File.mtime('tracked-file'), File.mtime('untracked-file')] }
231+
sleep 1
232+
expect { subject }.to_not change {
233+
[
234+
File.mtime('tracked-file'),
235+
File.mtime('other-tracked-file'),
236+
File.mtime('untracked-file')
237+
]
238+
}
207239
end
208240
end
209241

210242
context 'when there were staged changes' do
211243
around do |example|
212244
repo do
213245
`echo "Hello World" > tracked-file`
214-
`git add tracked-file`
215-
`git commit -m "Add tracked-file"`
246+
`echo "Hello Other World" > other-tracked-file`
247+
`git add tracked-file other-tracked-file`
248+
`git commit -m "Add tracked-file and other-tracked-file"`
216249
`echo "Hello Again" > untracked-file`
217250
`echo "Some more text" >> tracked-file`
251+
`echo "Some more text" >> other-tracked-file`
218252
`git add tracked-file`
219253
`echo "Yet some more text" >> tracked-file`
220254
example.run
@@ -238,8 +272,14 @@
238272
end
239273

240274
it 'keeps modification times the same' do
241-
expect { subject }.
242-
to_not change { [File.mtime('tracked-file'), File.mtime('untracked-file')] }
275+
sleep 1
276+
expect { subject }.to_not change {
277+
[
278+
File.mtime('tracked-file'),
279+
File.mtime('other-tracked-file'),
280+
File.mtime('untracked-file')
281+
]
282+
}
243283
end
244284
end
245285

0 commit comments

Comments
 (0)