Class: GitCommitAutouser::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/git_commit_autouser/cli.rb

Class Method Summary collapse

Class Method Details

.abort_no_origin!Object



51
52
53
54
# File 'lib/git_commit_autouser/cli.rb', line 51

def self.abort_no_origin!
  $stderr.puts "remote `origin` is not configured"
  exit 1
end

.abort_no_user_config!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/git_commit_autouser/cli.rb', line 34

def self.abort_no_user_config!
  $stderr.puts <<-EOS
No user setting found. You should add to ~/.gitconfig like the following.
------
[#{Config::USER_CONFIG_PREFIX}github]
  url-regexp = github.com
  name = "Foo Bar"
  email = [email protected]
[#{Config::USER_CONFIG_PREFIX}ghe]
  url-regexp = git.company.com
  name = "Foo Bar"
  email = [email protected]
------
  EOS
  exit 1
end

.abort_no_user_matched!Object



56
57
58
59
60
# File 'lib/git_commit_autouser/cli.rb', line 56

def self.abort_no_user_matched!
  $stderr.puts "No user setting matched. Abort."
  $stderr.puts "Remote Url: #{remote_url}"
  exit 1
end

.start(argv = ARGV) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git_commit_autouser/cli.rb', line 3

def self.start(argv=ARGV)
  abort_no_origin! unless Git.remote?("origin")
  remote_url = Git.remote_push_url("origin")

  users = Config.users
  abort_no_user_config! if users.empty?

  matched = nil
  users.each do |user|
    matched = user.url_regexp.match(remote_url)
    unless matched.nil?
      matched = user
      break
    end
  end

  abort_no_user_matched! if matched.nil?

  env = {
    "GIT_COMMITTER_NAME" => matched.name,
    "GIT_COMMITTER_EMAIL" => matched.email,
    "GIT_AUTHOR_NAME" => matched.name,
    "GIT_AUTHOR_EMAIL" => matched.email,
  }

  env["HUB_CONFIG"] = matched.hub_config if matched.hub_config

  system env, "git", "commit", *argv
  exit $?.exitstatus
end