Module: Git

Defined in:
lib/git.rb

Overview

module for help with git

Constant Summary collapse

BASE_DIR =
'~/'.freeze
LOOKFILE_DIR =
'.lookfile'.freeze

Class Method Summary collapse

Class Method Details

.commit(base_dir = BASE_DIR) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/git.rb', line 52

def commit(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  message = status(base_dir)
  `#{git} add --all`
  return nil if !make_commit?(message, base_dir) || message.empty?
  message
end

.init(base_dir = BASE_DIR) ⇒ Object



16
17
18
19
# File 'lib/git.rb', line 16

def init(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  `#{git} init`
end

.load_git_command(base_dir = BASE_DIR) ⇒ Object



11
12
13
14
# File 'lib/git.rb', line 11

def load_git_command(base_dir = BASE_DIR)
  directory = Lookfile.load_lookfile_dir(base_dir)
  "git -C '#{directory}'"
end

.make_commit?(message, base_dir = BASE_DIR) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/git.rb', line 65

def make_commit?(message, base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  commit = `#{git} commit -m "#{message}"`
  return false if commit.include?('nothing to commit')
  true
end

.push(base_dir = BASE_DIR) ⇒ Object



60
61
62
63
# File 'lib/git.rb', line 60

def push(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  `#{git} push origin master`
end

.rebase(base_dir = BASE_DIR) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/git.rb', line 33

def rebase(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  branchs = `#{git} branch -a`
  return nil unless branchs.include?('remotes/origin/master')
  `#{git} fetch origin -p`
  `#{git} pull origin master`
end

.remote?(base_dir = BASE_DIR) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/git.rb', line 21

def remote?(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  remote = `#{git} remote`
  remote.include?('origin')
end

.set_remote(repository_ssh_name, base_dir = BASE_DIR) ⇒ Object



27
28
29
30
31
# File 'lib/git.rb', line 27

def set_remote(repository_ssh_name, base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  `#{git} remote remove origin` if remote?(base_dir)
  `#{git} remote add origin #{repository_ssh_name}`
end

.status(base_dir = BASE_DIR) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/git.rb', line 41

def status(base_dir = BASE_DIR)
  git = load_git_command(base_dir)
  untracked_files = `#{git} ls-files --others --exclude-standard`.split
  deleted_files = `#{git} ls-files --deleted`.split
  modified_files = `#{git} ls-files --modified`.split - deleted_files
  message = Lookfile.show_files("\nAdded files:", untracked_files)
  message += Lookfile.show_files("\nModified files:", modified_files)
  message += Lookfile.show_files("\nDeleted files:", deleted_files)
  message.strip
end