Class: TempGit::GitDir

Inherits:
Object
  • Object
show all
Defined in:
lib/tempgit/tempgit.rb

Overview

Create a temporary Git directory for testing git-related tools.

Usage Example

require 'tempgit'

repo = TempGit::GitDir.new
puts repo.git('log')

repo.add_new_file
puts repo.git('status')

repo.commit_with_sequence_number
puts repo.git('log')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nosweep = false) ⇒ GitDir

Returns a new instance of GitDir.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tempgit/tempgit.rb', line 32

def initialize nosweep=false
  @seq_num = 0
  @work_tree = Dir.mktmpdir
  @git_dir = File.join(@work_tree, '.git')
  @nosweep = ENV['NOSWEEP'] || nosweep

  cleanup_at_exit @work_tree
  init_temp_repo
  add_new_file
  commit_with_sequence_number
end

Instance Attribute Details

#git_dirObject (readonly)

Path to .git repository.



30
31
32
# File 'lib/tempgit/tempgit.rb', line 30

def git_dir
  @git_dir
end

#nosweepObject

Do not delete the temporary directory at exit.



24
25
26
# File 'lib/tempgit/tempgit.rb', line 24

def nosweep
  @nosweep
end

#work_treeObject (readonly)

Path to repository’s working tree.



27
28
29
# File 'lib/tempgit/tempgit.rb', line 27

def work_tree
  @work_tree
end

Instance Method Details

#add_new_fileObject

Create unique, empty file in the temporary git repository and add it to the index.



50
51
52
53
# File 'lib/tempgit/tempgit.rb', line 50

def add_new_file
  file = File.basename(Tempfile.new 'empty-', @work_tree)
  git 'add', file
end

#cleanup_at_exit(dir) ⇒ Object

Sweep temporary git repositories at interpreter exit unless nosweep is true.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tempgit/tempgit.rb', line 81

def cleanup_at_exit dir
  at_exit do
    if @nosweep
      $stderr.puts "\nAbandoning directory: #{dir}"
      Kernel::exit false
    else
      $stderr.puts "Directory missing: #{dir}" unless Dir.exists? dir
      fail "Refusing to purge directory outside /tmp: #{dir}" if
        File.dirname(dir) !~ %r{^/tmp}
      FileUtils.rm_rf dir, :secure => true
    end
  end
end

#commit_with_sequence_numberObject

Commit current index to repository with an incrementing counter in the commit message. Useful for ensuring the number of commits is the repository meets expectations.



58
59
60
# File 'lib/tempgit/tempgit.rb', line 58

def commit_with_sequence_number
  git %q/commit -m 'Commit number %d'/ % @seq_num+=1
end

#git(*commands) ⇒ Object

Run arbitrary git commands in the correct directory context.

commands

String or array of arguments to git.



65
66
67
68
69
# File 'lib/tempgit/tempgit.rb', line 65

def git *commands
  context = %q/--git-dir="%s" --work-tree="%s"/ %
            [@git_dir, @work_tree]
  `git #{context} #{[*commands].join(' ')}`
end

#init_temp_repoObject



44
45
46
# File 'lib/tempgit/tempgit.rb', line 44

def init_temp_repo
  git 'init'
end

#timestampObject

Returns timestamp with nanosecond resolution. Useful for creating sequential files or non-colliding file content.

return

seconds_since_epoch.nanoseconds



75
76
77
# File 'lib/tempgit/tempgit.rb', line 75

def timestamp
  Time.now.strftime '%s.%N'
end