Class: TmpRepo
- Inherits:
-
Object
- Object
- TmpRepo
- 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
-
#working_dir ⇒ Object
readonly
Returns the value of attribute working_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #add_all ⇒ Object
- #checkout(ref) ⇒ Object
- #commit(message) ⇒ Object
- #create_branch(branch_name) ⇒ Object
- #create_file(new_file) ⇒ Object
- #current_branch ⇒ Object
- #each_commit_id(&block) ⇒ Object
- #git(command) ⇒ Object
- #in_repo ⇒ Object
-
#initialize(dir = nil) ⇒ TmpRepo
constructor
A new instance of TmpRepo.
- #status ⇒ Object
- #unlink ⇒ Object
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_dir ⇒ Object (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_dir ⇒ Object
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_all ⇒ Object
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() git("commit -m '#{.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_branch ⇒ Object
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_repo ⇒ Object
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 |
#status ⇒ Object
69 70 71 |
# File 'lib/tmp-repo.rb', line 69 def status parse_status(git('status')) end |
#unlink ⇒ Object
28 29 30 31 |
# File 'lib/tmp-repo.rb', line 28 def unlink FileUtils.rm_rf(working_dir.to_s) nil end |