Module: GitPair::Command

Extended by:
Command
Included in:
Command
Defined in:
lib/git-pair/command.rb

Instance Method Summary collapse

Instance Method Details

#abort(error_message, extra = "") ⇒ Object



114
115
116
# File 'lib/git-pair/command.rb', line 114

def abort(error_message, extra = "")
  super red(" Error: #{error_message}\n") + extra
end

#author_listObject



101
102
103
# File 'lib/git-pair/command.rb', line 101

def author_list
  "     #{bold 'Author list:'} #{Author.all.sort.map { |a| a.name }.join "\n                  "}"
end

#bold(string) ⇒ Object



122
123
124
# File 'lib/git-pair/command.rb', line 122

def bold(string)
  "#{C_BOLD}#{string}#{C_RESET}"
end

#current_author_infoObject



109
110
111
112
# File 'lib/git-pair/command.rb', line 109

def current_author_info
  "  #{bold 'Current author:'} #{Config.current_author}\n" +
  "   #{bold 'Current email:'} #{Config.current_email}\n "
end

#highlight(string) ⇒ Object



118
119
120
# File 'lib/git-pair/command.rb', line 118

def highlight(string)
  "#{C_REVERSE}#{string}#{C_RESET}"
end

#pair_emailObject



105
106
107
# File 'lib/git-pair/command.rb', line 105

def pair_email
  "      #{bold 'Pair email:'} #{Config.pair_email} \n"
end

#red(string) ⇒ Object



126
127
128
# File 'lib/git-pair/command.rb', line 126

def red(string)
  "#{C_RED}#{C_REVERSE}#{string}#{C_RESET}"
end

#run!(args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/git-pair/command.rb', line 11

def run!(args)
  options = OpenStruct.new(:update  => true,
                           :authors => [])

  parser = OptionParser.new do |opts|
    opts.banner = highlight('General Syntax:')
    opts.separator '  git pair [reset | authors | options]'

    opts.separator ' '
    opts.separator highlight('Options:')

    opts.on '-a', '--add AUTHOR',    'Add an author. Format: "Author Name <[email protected]>"' do |author|
      options.add_author = Author.new(author)
      Config.add_author(options.add_author)
    end

    opts.on '-r', '--remove NAME', 'Remove an author. Use the full name.' do |name|
      options.remove_author = name
      Config.remove_author options.remove_author
    end

    opts.on '-d', '--reset', 'Reset current author to default (global) config' do
      options.reset = true
      Config.reset
    end

    opts.on '--install-hook', 'Install a post-commit hook for the current repo. See git-pair/hooks/post-commit for more information.' do
      options.symlink = true
    end

    opts.on '--email EMAIL', 'Add a default email address to be used for pairs' do |email|
      puts "Setting email to #{email}"
      options.pair_email = email
      Config.set_pair_email(options.pair_email)
    end

    opts.on '-s', "--show 'aa [bb]'", 'Show the string to be used for the commit author field' do |initials|
      options.update = false
      options.authors = Author.find_all(initials.split(' '))
    end

    opts.separator ' '
    opts.separator highlight('Switching authors:')
    opts.separator '  git pair aa [bb]                   Where AA and BB are any abbreviation of an'
    opts.separator ' '*37 + 'author\'s name. You can specify one or more authors.'

    opts.separator ' '
    opts.separator highlight('Current config:')
    opts.separator author_list.split("\n")
    opts.separator ' '
    opts.separator pair_email.split("\n")
    opts.separator ' '
    opts.separator current_author_info.split("\n")
  end

  initials = parser.parse!(args.dup)

  initials = initials.map { |e| e.split(' ') }.flatten # in case initials are enclosed in quotes

  if options.authors.empty? && !initials.empty?
    options.authors = Author.find_all(initials)
  end

  if args.empty?
    puts parser.help
  elsif options.authors && !options.update
    puts Display.git_author options.authors
  elsif options.symlink
    symlink_post_commit_hook
  elsif options.authors.empty?
    puts author_list
    puts
    puts current_author_info
  else
    Config.switch(options.authors)
    puts current_author_info
  end

rescue OptionParser::MissingArgument
  abort "missing required argument", parser.help
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
  abort e.message.sub(':', ''), parser.help
rescue NoMatchingAuthorsError => e
  abort e.message, "\n" + author_list
rescue MissingConfigurationError => e
  abort e.message, parser.help
rescue Author::InvalidAuthorString => e
  abort e.message, parser.help
end

#symlink_post_commit_hookObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/git-pair/command.rb', line 130

def symlink_post_commit_hook
  this_directory    = Pathname.new File.expand_path(File.dirname(__FILE__))
  post_commit_hook  = Pathname.new(File.join(this_directory, "..", "..", "hooks", "post-commit")).realpath
  project_git_hooks = File.join(Dir.pwd, ".git", "hooks")

  if File.exists?(post_commit_hook) && File.exists?(project_git_hooks)
    symlink_target = Pathname.new(File.join(project_git_hooks, "post-commit"))

    puts "Symlinking: #{symlink_target}\nto #{post_commit_hook}.\n\n"

    if File.exists?(symlink_target)
      puts "Can't create symlink! #{symlink_target} already exists."
    else
      symlink_target.make_symlink(post_commit_hook)
      puts "Succceded!"
    end
  elsif !File.exists?(project_git_hooks)
    puts "The current directory doesn't appear to be a valid git repository."
  end
end