Module: RubyGitHooks::GitOps

Extended by:
GitOps
Included in:
GitOps
Defined in:
lib/ruby_git_hooks/git_ops.rb

Constant Summary collapse

Hook =
RubyGitHooks::Hook

Instance Method Summary collapse

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_checkout(repo_name = "child_repo", branch_name = "master") ⇒ Object



105
106
107
# File 'lib/ruby_git_hooks/git_ops.rb', line 105

def git_checkout(repo_name="child_repo", branch_name="master")
  Hook.shell! "cd #{repo_name} && git checkout #{branch_name}"
end

#git_commit(repo_name = "child_repo", commit_message = "Generic commit message") ⇒ Object



85
86
87
# File 'lib/ruby_git_hooks/git_ops.rb', line 85

def git_commit(repo_name = "child_repo", commit_message = "Generic commit message")
  Hook.shell! "cd #{repo_name} && git commit -m \"#{commit_message}\""
end

#git_create_and_checkout_branch(repo_name = "child_repo", branch_name = "B1") ⇒ Object



109
110
111
# File 'lib/ruby_git_hooks/git_ops.rb', line 109

def git_create_and_checkout_branch(repo_name="child_repo", branch_name="B1")
  Hook.shell! "cd #{repo_name} && git checkout -b #{branch_name}"
end

#git_delete(repo_name = "child_repo", filename = "file_to_delete") ⇒ Object



71
72
73
74
# File 'lib/ruby_git_hooks/git_ops.rb', line 71

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_ff_merge(repo_name = "child_repo", branch = "B1", msg = "Merge branch #{branch}") ⇒ Object



126
127
128
129
# File 'lib/ruby_git_hooks/git_ops.rb', line 126

def git_ff_merge(repo_name = "child_repo", branch = "B1", msg = "Merge branch #{branch}")
  # better be sure there's not going to be a conflict
  Hook.shell!("cd #{repo_name} && git merge #{branch} --ff")
end

#git_merge(repo_name = "child_repo", branch = "B1", msg = "Merge branch #{branch}") ⇒ Object



121
122
123
124
# File 'lib/ruby_git_hooks/git_ops.rb', line 121

def git_merge(repo_name = "child_repo", branch = "B1", msg = "Merge branch #{branch}")
  # better be sure there's not going to be a conflict
  Hook.shell!("cd #{repo_name} && git merge #{branch} --no-ff -m '#{msg}'")
end

#git_push(repo_name = "child_repo", remote = "origin", branch = "master") ⇒ Object



89
90
91
# File 'lib/ruby_git_hooks/git_ops.rb', line 89

def git_push(repo_name = "child_repo", remote = "origin", branch = "master")
  Hook.shell! "cd #{repo_name} && git push #{remote} #{branch}"
end

#git_push_all(repo_name = "child_repo") ⇒ Object



113
114
115
# File 'lib/ruby_git_hooks/git_ops.rb', line 113

def git_push_all(repo_name = "child_repo")
  Hook.shell! "cd #{repo_name} && git push --all"
end

#git_rename(repo_name = "child_repo", filename = "file_to_rename", new_filename) ⇒ Object



76
77
78
79
# File 'lib/ruby_git_hooks/git_ops.rb', line 76

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

#git_revlist_all(repo_name = "child_repo") ⇒ Object



117
118
119
# File 'lib/ruby_git_hooks/git_ops.rb', line 117

def git_revlist_all(repo_name = "child_repo")
  Hook.shell!("cd #{repo_name} && git rev-list --all").split("\n")
end

#git_tag(repo_name = "child_repo", tagname = "0.1") ⇒ Object



101
102
103
# File 'lib/ruby_git_hooks/git_ops.rb', line 101

def git_tag(repo_name="child_repo", tagname="0.1")
  Hook.shell! "cd #{repo_name} && git tag -a #{tagname} -m 'test'"
end

#last_commit_message(repo_name = "child_repo") ⇒ Object



97
98
99
# File 'lib/ruby_git_hooks/git_ops.rb', line 97

def last_commit_message(repo_name = "child_repo")
  Hook.shell!("cd #{repo_name} && git log -1").chomp
end

#last_commit_sha(repo_name = "child_repo") ⇒ Object



81
82
83
# File 'lib/ruby_git_hooks/git_ops.rb', line 81

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 new_bare_repo(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", commit_message = "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_multi_file_commit(num_files = 3, repo_name = "child_repo", commit_message = "multi-file commit", base_filename = "test", contents = "Contents") ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ruby_git_hooks/git_ops.rb', line 61

def new_multi_file_commit(num_files = 3, repo_name = "child_repo", commit_message = "multi-file commit", base_filename = "test", contents="Contents")
  (1..num_files).each do |num|
    File.open(File.join(repo_name, base_filename + num.to_s), "w") do |f|
      f.write(contents)
    end
  end
  Hook.shell! "cd #{repo_name} && git add . && 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



93
94
95
# File 'lib/ruby_git_hooks/git_ops.rb', line 93

def rewind_one_commit(repo_name = "child_repo")
  Hook.shell! "cd #{repo_name} && git reset --hard HEAD~"
end