Class: TempGit::GitDir
- Inherits:
-
Object
- Object
- TempGit::GitDir
- Defined in:
- lib/tempgit/tempgit.rb
Overview
Instance Attribute Summary collapse
-
#git_dir ⇒ Object
readonly
Path to .git repository.
-
#nosweep ⇒ Object
Do not delete the temporary directory at exit.
-
#work_tree ⇒ Object
readonly
Path to repository’s working tree.
Instance Method Summary collapse
-
#add_new_file ⇒ Object
Create unique, empty file in the temporary git repository and add it to the index.
-
#cleanup_at_exit(dir) ⇒ Object
Sweep temporary git repositories at interpreter exit unless nosweep is true.
-
#commit_with_sequence_number ⇒ Object
Commit current index to repository with an incrementing counter in the commit message.
-
#git(*commands) ⇒ Object
Run arbitrary git commands in the correct directory context.
- #init_temp_repo ⇒ Object
-
#initialize(nosweep = false) ⇒ GitDir
constructor
A new instance of GitDir.
-
#timestamp ⇒ Object
Returns timestamp with nanosecond resolution.
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_dir ⇒ Object (readonly)
Path to .git repository.
30 31 32 |
# File 'lib/tempgit/tempgit.rb', line 30 def git_dir @git_dir end |
#nosweep ⇒ Object
Do not delete the temporary directory at exit.
24 25 26 |
# File 'lib/tempgit/tempgit.rb', line 24 def nosweep @nosweep end |
#work_tree ⇒ Object (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_file ⇒ Object
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_number ⇒ Object
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_repo ⇒ Object
44 45 46 |
# File 'lib/tempgit/tempgit.rb', line 44 def init_temp_repo git 'init' end |
#timestamp ⇒ Object
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 Time.now.strftime '%s.%N' end |