Class: TmpRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/tmp-repo.rb,
lib/tmp-repo/version.rb

Defined Under Namespace

Classes: GitError

Constant Summary collapse

DEFAULT_GIT_EXECUTABLE =
'git'
VERSION =
'1.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = nil) ⇒ TmpRepo

Returns a new instance of TmpRepo.



19
20
21
22
23
24
25
26
# File 'lib/tmp-repo.rb', line 19

def initialize(dir = nil)
  @working_dir = Pathname(
    dir || self.class.random_dir
  )

  FileUtils.mkdir_p(working_dir)
  git('init')
end

Instance Attribute Details

#working_dirObject (readonly)

Returns the value of attribute working_dir.



13
14
15
# File 'lib/tmp-repo.rb', line 13

def working_dir
  @working_dir
end

Class Method Details

.random_dirObject



15
16
17
# File 'lib/tmp-repo.rb', line 15

def self.random_dir
  File.join(Dir.tmpdir, SecureRandom.hex(16))
end

Instance Method Details

#add_allObject



49
50
51
# File 'lib/tmp-repo.rb', line 49

def add_all
  git('add -A')
end

#checkout(ref) ⇒ Object



57
58
59
# File 'lib/tmp-repo.rb', line 57

def checkout(ref)
  git("checkout #{ref}")
end

#commit(message) ⇒ Object



53
54
55
# File 'lib/tmp-repo.rb', line 53

def commit(message)
  git("commit -m '#{message.gsub("'", "\\\\'")}'")
end

#create_branch(branch_name) ⇒ Object



61
62
63
# File 'lib/tmp-repo.rb', line 61

def create_branch(branch_name)
  git("checkout -b #{branch_name}")
end

#create_file(new_file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tmp-repo.rb', line 33

def create_file(new_file)
  new_path = working_dir.join(new_file).to_s
  handle = File.open(new_path, 'w+')

  if block_given?
    yield handle
    handle.close
  else
    handle
  end
end

#current_branchObject



65
66
67
# File 'lib/tmp-repo.rb', line 65

def current_branch
  git('rev-parse --abbrev-ref HEAD').strip
end

#each_commit_id(&block) ⇒ Object



45
46
47
# File 'lib/tmp-repo.rb', line 45

def each_commit_id(&block)
  git('log --pretty=format:%H').strip.split("\n").each(&block)
end

#git(command) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tmp-repo.rb', line 73

def git(command)
  in_repo do
    output = `#{git_executable} #{command} 2>&1`

    if $?.exitstatus != 0
      raise GitError, output.strip
    end

    output
  end
end

#in_repoObject



85
86
87
88
89
# File 'lib/tmp-repo.rb', line 85

def in_repo
  Dir.chdir(working_dir.to_s) do
    yield
  end
end

#statusObject



69
70
71
# File 'lib/tmp-repo.rb', line 69

def status
  parse_status(git('status'))
end


28
29
30
31
# File 'lib/tmp-repo.rb', line 28

def unlink
  FileUtils.rm_rf(working_dir.to_s)
  nil
end