Module: Vimpack::Utils::Git

Includes:
Process
Included in:
Commands::Command, Models::Base, Models::Repo
Defined in:
lib/vimpack/utils/git.rb

Instance Method Summary collapse

Methods included from Process

#run_process!, #run_process_and_wait!, #run_process_or_die!, #wait_for_child, #within_dir

Methods included from Io

#die!, #exit_with_error!, #say, #scream

Instance Method Details

#add_submodule(repo_uri, *paths) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/vimpack/utils/git.rb', line 11

def add_submodule(repo_uri, *paths)
  parent, name = paths[0..-2], paths[-1]
  path = ::File.join('scripts', *paths)
  run_process_or_die!("git submodule add #{repo_uri} #{path}",
                      self.pack_path)
  init_submodule(path)
  update_submodule(path)
end

#init_repo(path, bare = false) ⇒ Object



7
8
9
# File 'lib/vimpack/utils/git.rb', line 7

def init_repo(path, bare=false)
  run_process_or_die!("git init --quiet#{bare == true ? ' --bare' : ''}", path)
end

#init_submodule(path = '') ⇒ Object



39
40
41
# File 'lib/vimpack/utils/git.rb', line 39

def init_submodule(path='')
  run_process_or_die!("git submodule init #{path}", self.pack_path)
end

#remove_submodule(*paths) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/vimpack/utils/git.rb', line 28

def remove_submodule(*paths)
  path = ::File.join('scripts', *paths)
  submod_match = ::Regexp.escape("[submodule \"#{path}\"]") + '\n.*\n.*'
  config_match = ::Regexp.escape("[submodule \"#{path}\"]") + '\n.*'
  replace_contents(self.pack_path.join('.gitmodules'), submod_match)
  replace_contents(self.pack_path.join('.git', 'config'), config_match)
  remove_link(self.bundle_path.join(paths[-1]))
  remove_directory(self.pack_path.join(path))
  run_process_or_die!("git rm -r #{path}", self.pack_path)
end

#replace_contents(path, match, new = '') ⇒ Object



20
21
22
23
24
25
26
# File 'lib/vimpack/utils/git.rb', line 20

def replace_contents(path, match, new='')
  contents = ::File.read(path)
  contents = contents.sub(match, new)
  ::File.open(path, 'w') do |file|
    file.write(contents)
  end
end

#repo_add_dotObject



83
84
85
86
# File 'lib/vimpack/utils/git.rb', line 83

def repo_add_dot
  cmd = "git add ."
  run_process_or_die!(cmd, self.pack_path.to_s)
end

#repo_add_remote(name, path) ⇒ Object



47
48
49
50
# File 'lib/vimpack/utils/git.rb', line 47

def repo_add_remote(name, path)
  cmd = "git remote add #{name} #{path}"
  run_process_or_die!(cmd, self.pack_path.to_s)
end

#repo_clone(repo_url, path) ⇒ Object



78
79
80
81
# File 'lib/vimpack/utils/git.rb', line 78

def repo_clone(repo_url, path)
  cmd = "git clone #{repo_url} #{path}"
  run_process_or_die!(cmd)
end

#repo_commit(msg = '') ⇒ Object



58
59
60
61
62
# File 'lib/vimpack/utils/git.rb', line 58

def repo_commit(msg='')
  msg = '[VIMPACK] vimpack updated' if msg == ''
  cmd = "git add . && git commit -m '#{msg}'"
  run_process_or_die!(cmd, self.pack_path.to_s)
end

#repo_exec(subcommand, commands) ⇒ Object



88
89
90
91
92
# File 'lib/vimpack/utils/git.rb', line 88

def repo_exec(subcommand, commands)
  commands = sanitize_commands(commands)
  cmd = "git #{subcommand} #{commands}"
  run_process_or_die!(cmd, self.pack_path.to_s)
end

#repo_pull(remote_path = 'origin master') ⇒ Object



64
65
66
# File 'lib/vimpack/utils/git.rb', line 64

def repo_pull(remote_path='origin master')
  run_process_or_die!("git pull #{remote_path}", self.pack_path.to_s)
end

#repo_pushObject



68
69
70
71
72
73
74
75
76
# File 'lib/vimpack/utils/git.rb', line 68

def repo_push
  cmd = "git push origin master"
  error_message = <<-EOE
error: local repo out of sync with remote
  use git to sync with something like this:
   vimpack git fetch && vimpack git merge origin/master
  EOE
  run_process_or_die!(cmd, self.pack_path.to_s, error_message)
end

#repo_remote?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/vimpack/utils/git.rb', line 52

def repo_remote?(name)
  remotes = run_process_and_wait!("git remote show #{name}", pack_path.to_s)
  !remotes.message.include?('does not appear to be a git') && remotes.message.include?(name)
end

#sanitize_commands(commands) ⇒ Object



94
95
96
97
98
99
# File 'lib/vimpack/utils/git.rb', line 94

def sanitize_commands(commands)
  commands.each.inject('') do |full_command, command|
    full_command << ' ' unless full_command == ''
    full_command << (command.include?(' ') ? "'#{command}'" : command)
  end
end

#update_submodule(path = '') ⇒ Object



43
44
45
# File 'lib/vimpack/utils/git.rb', line 43

def update_submodule(path='')
  run_process_or_die!("git submodule update #{path}", self.pack_path)
end