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



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

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



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

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



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

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



61
62
63
64
# File 'lib/ruby_git_hooks/git_ops.rb', line 61

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



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

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



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

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



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

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



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

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



66
67
68
69
# File 'lib/ruby_git_hooks/git_ops.rb', line 66

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



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

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



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

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



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

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



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

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_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



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

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