Module: RubyGitHooks::GitOps
Constant Summary collapse
- Hook =
RubyGitHooks::Hook
Instance Method Summary collapse
- #add_hook(repo_name, hook_name, contents) ⇒ Object
- #clone_repo(parent_name = "parent_repo.git", child_name = "child_repo") ⇒ Object
- #git_commit(repo_name = "child_repo", commit_message = "Generic commit message") ⇒ Object
- #git_delete(repo_name = "child_repo", filename = "file_to_delete") ⇒ Object
- #git_push(repo_name = "child_repo", remote = "origin", branch = "master") ⇒ Object
- #git_rename(repo_name = "child_repo", filename = "file_to_rename", new_filename) ⇒ Object
- #last_commit_message(repo_name = "child_repo") ⇒ Object
- #last_commit_sha(repo_name = "child_repo") ⇒ Object
- #new_bare_repo(name = "parent_repo.git") ⇒ Object
- #new_commit(repo_name, filename, contents = "Contents", commit_message = "Single-file commit of #{filename}") ⇒ Object
- #new_single_file_commit(repo_name = "child_repo", contents = "Single-file commit") ⇒ Object
- #rewind_one_commit(repo_name = "child_repo") ⇒ Object
Instance Method Details
#add_hook(repo_name, hook_name, contents) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 23 def add_hook(repo_name, hook_name, contents) # We're adding to either a normal or bare directory. # Check for hooks directory. if File.exist? "#{repo_name}/.git/hooks" hooks_dir = "#{repo_name}/.git/hooks" elsif File.exist? "#{repo_name}/hooks" hooks_dir = "#{repo_name}/hooks" else raise "Can't locate hooks directory under #{repo_name.inspect}!" end filename = File.join(hooks_dir, hook_name) File.open(filename, "w") do |f| f.write(contents) end Hook.shell!("chmod +x #{filename}") end |
#clone_repo(parent_name = "parent_repo.git", child_name = "child_repo") ⇒ Object
19 20 21 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 19 def clone_repo(parent_name = "parent_repo.git", child_name = "child_repo") Hook.shell! "git clone #{parent_name} #{child_name}" end |
#git_commit(repo_name = "child_repo", commit_message = "Generic commit message") ⇒ Object
77 78 79 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 77 def git_commit(repo_name = "child_repo", = "Generic commit message") Hook.shell! "cd #{repo_name} && git commit -m \"#{commit_message}\"" end |
#git_delete(repo_name = "child_repo", filename = "file_to_delete") ⇒ Object
62 63 64 65 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 62 def git_delete(repo_name="child_repo", filename="file_to_delete") # filename is a file that has already been added and commited to the repo Hook.shell! "cd #{repo_name} && git rm #{filename}" end |
#git_push(repo_name = "child_repo", remote = "origin", branch = "master") ⇒ Object
81 82 83 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 81 def git_push(repo_name = "child_repo", remote = "origin", branch = "master") Hook.shell! "cd #{repo_name} && git push #{remote} #{branch}" end |
#git_rename(repo_name = "child_repo", filename = "file_to_rename", new_filename) ⇒ Object
67 68 69 70 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 67 def git_rename(repo_name="child_repo", filename="file_to_rename", new_filename) # filename is a file that has already been added and commited to the repo Hook.shell! "cd #{repo_name} && git mv #{filename} #{new_filename}" end |
#last_commit_message(repo_name = "child_repo") ⇒ Object
89 90 91 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 89 def (repo_name = "child_repo") Hook.shell!("cd #{repo_name} && git log -1").chomp end |
#last_commit_sha(repo_name = "child_repo") ⇒ Object
73 74 75 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 73 def last_commit_sha(repo_name = "child_repo") Hook.shell!("cd #{repo_name} && git log -n 1 --format=%H").chomp end |
#new_bare_repo(name = "parent_repo.git") ⇒ Object
15 16 17 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 15 def (name = "parent_repo.git") Hook.shell! "mkdir #{name} && cd #{name} && git init --bare" end |
#new_commit(repo_name, filename, contents = "Contents", commit_message = "Single-file commit of #{filename}") ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 41 def new_commit(repo_name, filename, contents = "Contents", = "Single-file commit of #{filename}") if !contents.nil? File.open(File.join(repo_name, filename), "w") do |f| f.write(contents) end end Hook.shell! "cd #{repo_name} && git add #{filename} && git commit -m \"#{commit_message}\"" end |
#new_single_file_commit(repo_name = "child_repo", contents = "Single-file commit") ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 51 def new_single_file_commit(repo_name = "child_repo", contents = "Single-file commit") @single_file_counter ||= 1 filename = "test_file_#{@single_file_counter}" new_commit(repo_name, filename, contents) @single_file_counter += 1 end |
#rewind_one_commit(repo_name = "child_repo") ⇒ Object
85 86 87 |
# File 'lib/ruby_git_hooks/git_ops.rb', line 85 def rewind_one_commit(repo_name = "child_repo") Hook.shell! "cd #{repo_name} && git reset --hard HEAD~" end |