Skip to content

Commit 867cafe

Browse files
author
Bertrand Roussel
committed
Provide a simple fsck command
1 parent bc4e9e3 commit 867cafe

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/git/lib.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,16 @@ def gc
793793
command('gc', ['--prune', '--aggressive', '--auto'])
794794
end
795795

796+
def fsck
797+
begin
798+
command('fsck')
799+
rescue
800+
return false
801+
end
802+
803+
true
804+
end
805+
796806
# reads a tree into the current index file
797807
def read_tree(treeish, opts = {})
798808
arr_opts = []

tests/units/test_lib.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,25 @@ def test_show
228228
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
229229
end
230230

231+
def test_fsck
232+
ret = @lib.fsck
233+
assert_equal(ret, true)
234+
235+
# Corrupted
236+
@wdir_corrupted = File.expand_path(File.join(@test_dir, 'corrupted'))
237+
FileUtils.cp_r(@wdir, @wdir_corrupted)
238+
# Remove first objects dir to corrupt the repo
239+
obj_dir = File.join(@wdir_corrupted, '.git', 'objects')
240+
Dir.foreach(obj_dir) do |filename|
241+
next if filename == '.'
242+
next if filename == '..'
243+
FileUtils.rm_r(File.join(obj_dir, filename))
244+
break
245+
end
246+
247+
lib_corrupted = Git.open(@wdir_corrupted).lib
248+
ret = lib_corrupted.fsck
249+
assert_equal(ret, false)
250+
end
251+
231252
end

0 commit comments

Comments
 (0)