Module: Popo::GitUtils

Includes:
Constants
Defined in:
lib/popo/git_utils.rb

Constant Summary

Constants included from Constants

Constants::COLOR_GREEN, Constants::COLOR_NONE, Constants::COLOR_RED, Constants::COLOR_YELLOW, Constants::DEFAULT_CONFIG_FILE, Constants::DEFAULT_POPO_TARGET, Constants::POPORC, Constants::POPO_COMMANDS, Constants::POPO_CONFIG, Constants::POPO_DIR_KEY, Constants::POPO_KEY_VALUES, Constants::POPO_WORK_PATH, Constants::POPO_YML_FILE, Constants::POST_INSTALL_NOTE, Constants::REQUIRED_COMMANDS

Class Method Summary collapse

Class Method Details

.branch_diff(cwd, branches = ['master', 'development']) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/popo/git_utils.rb', line 60

def self.branch_diff(cwd, branches = ['master', 'development'])
  if is_git?(cwd)
    diff_msg = `#{GIT_CMD} log --abbrev-commit --format=short \
               origin/#{branches[0]}..origin/#{branches[1]}`

    parsed = diff_msg.scan(/(commit [0-9a-f]+)\n+(.*?)\n+(.*?)(?:\n|$)/)

    Utils.git_say "#{File.basename(cwd).capitalize}"

    parsed.each do |p|
      commit_id = p[0].gsub(/commit/,'').strip
      author = p[1].scan(/Author: (.*) <.*>/)
      commit_msg = p[2].strip

      Utils.git_say "#{commit_id} \<#{author}\> #{commit_msg}", true
    end
  else
    repos = Dir.entries(cwd) - [ '.', '..' ]

    repos.each do |r|
      if File.directory?(r)
        FileUtils.cd(r) { branch_diff(File.join(cwd, r)) }
      end
    end
  end

end

.git_checkout(repo, branch) ⇒ Object



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

def self.git_checkout(repo, branch)
  Utils.git_say_with_time "checkout".yellow, "#{branch} branch" do
    Dir.chdir(repo) do
      out = `#{GIT_CMD} checkout #{branch} 2>&1`
      Utils.say(out, true)
    end
  end
end

.git_clone(repo, clone_path, branch) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/popo/git_utils.rb', line 5

def self.git_clone(repo, clone_path, branch)
  clone_path ||= nil
  branch = 'master' if branch.nil?

  if clone_path.nil?
    cmd = "#{GIT_CMD} clone -b #{branch} #{repo}"
  else
    cmd = "#{GIT_CMD} clone -b #{branch} #{repo} #{clone_path}"
  end

  Utils.git_say_with_time "clone".green, nil do
    Utils.say "#{'source'.yellow} #{repo}", true
    Utils.say "#{'target'.yellow} #{clone_path}", true
    Utils.say "#{'branch'.yellow} #{branch}", true
   `#{cmd}`
  end
end

.git_reset(repo, branch) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/popo/git_utils.rb', line 42

def self.git_reset(repo, branch)
  Utils.git_say_with_time "reset".red, "#{repo}" do
    Dir.chdir(repo) do
      out = `#{GIT_CMD} reset --hard origin/#{branch} 2>&1`
      Utils.say(out, true)
    end
  end
end

.git_stash(repo) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/popo/git_utils.rb', line 34

def self.git_stash(repo)
  Utils.git_say_with_time "stash".yellow, "#{repo}" do
    Dir.chdir(repo) do
      `#{GIT_CMD} stash`
    end
  end
end

.git_update(repo, branch) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/popo/git_utils.rb', line 23

def self.git_update(repo, branch)
  Utils.git_say_with_time "fetch".green, "#{repo}" do
    Dir.chdir(repo) do
      `#{GIT_CMD} fetch 2>&1`
    end
  end

  git_checkout(repo, branch)
  git_reset(repo, branch)
end

.is_git?(cwd) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/popo/git_utils.rb', line 88

def self.is_git?(cwd)
  File.exists?(File.join(cwd, '.git'))
end