Module: BarkeepClient::Commands

Defined in:
lib/barkeep/view.rb,
lib/barkeep/commit.rb,
lib/barkeep/unapproved.rb

Class Method Summary collapse

Class Method Details

.commit(configuration) ⇒ 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
# File 'lib/barkeep/commit.rb', line 11

def self.commit(configuration)
  options = Trollop.options do
    banner <<-EOS.dedent
      Barkeep's 'commit' command shows information about a particular commit given its SHA.

      Usage:
          $ barkeep commit [options] <commit>
      where <commit> is specified as a (partial) SHA-1 hash (for the current repo) or as, for example,
          myrepo/d29a4a0fa
      to specify a particular repository, and [options] can include:
    EOS
  end
  Trollop.die "must provide a commit sha" unless ARGV.size == 1

  begin
    repo, sha = Commands.parse_commit(ARGV[0])
  rescue RuntimeError => e
    Trollop.die e.message
  end

  begin
    result = BarkeepClient.commits(configuration, repo, [sha]).values[0]
  rescue RuntimeError => e
    puts e.message
    exit 1
  end

  result.each { |key, value| puts "  #{key.rjust(result.keys.map(&:size).max)}: #{value}" }
end

.parse_commit(commit_specification) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/barkeep/commit.rb', line 41

def self.parse_commit(commit_specification)
  case commit_specification
  when %r{^[^/]+/#{SHA_REGEX}$} # foo/abc123
    commit_specification.split "/"
  when /^#{SHA_REGEX}$/ # abc123
    repo = File.basename(`git rev-parse --show-toplevel`).strip
    if repo.empty?
      raise "need to be in a git repo or specify a repository (e.g. myrepo/abc123)"
    end
    [repo, commit_specification]
  else
    raise "#{commit_specification} is an invalid commit specification"
  end
end

.unapproved(configuration) ⇒ Object



10
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
# File 'lib/barkeep/unapproved.rb', line 10

def self.unapproved(configuration)
  options = Trollop.options do
    banner <<-EOS.dedent
      Barkeep's 'unapproved' command shows information about a particular commit. It MUST be run from a git
      repository of the same name as the repository on the server.

      Usage:
          $ barkeep unapproved [options] <commit-range>
      where <commit-range> is a commit range specified using git's range syntax (see `man gitrevisions`).
      For example:

          $ barkeep unapproved abc123
          $ barkeep unapproved ^abc123 def456
          $ barkeep unapproved abc123..def456

      [options] can include:
    EOS
  end
  Trollop.die "must provide a commit range" if ARGV.empty?

  repo = File.basename(`git rev-parse --show-toplevel`).strip
  if repo.empty?
    Trollop.die "need to be in a git repo"
  end

  commit_range = ARGV.map { |arg| "'#{arg}'" }.join(" ")
  commits_string = `git log --format='%H' #{commit_range}`
  exit(1) unless $?.to_i.zero?
  commits = commits_string.split("\n").map(&:strip)

  if commits.empty?
    puts "No commits in range."
    exit 0
  elsif commits.size > 1000
    puts "Warning: #{commits.size} commits in range. Lookup could be very slow. Proceed? [yN]"
    unless STDIN.gets.downcase.strip =~ /^y(es)?/
      puts "Aborting."
      exit 0
    end
  end

  begin
    commit_data = BarkeepClient.commits(configuration, repo, commits, ["approved"])
  rescue RuntimeError => e
    puts e.message
    exit 1
  end

  unapproved_commits = {}

  commit_data.each do |sha, commit|
    next if commit["approved"]
    author_name = `git log --format='%an' #{sha} -n 1`
    unapproved_commits[sha] = author_name
  end

  if unapproved_commits.empty?
    puts "#{commits.size} approved commit(s) and no unapproved commits in the given range."
  else
    puts "#{commits.size - unapproved_commits.size} approved commit(s) and " <<
         "#{unapproved_commits.size} unapproved commit(s) in the given range."
    puts "Unapproved:"
    unapproved_commits.each { |sha, author| puts "#{sha} #{author}" }
    exit 1
  end
end

.view(configuration) ⇒ Object



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
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/barkeep/view.rb', line 8

def self.view(configuration)
  options = Trollop.options do
    banner <<-EOS.dedent
      Barkeep's 'view' command opens the Barkeep commit page for a commit.

      Usage:
          $ barkeep view [options] <commit>
      where <commit> is specified as a (partial) SHA-1 hash (for the current repo) or as, for example,
          myrepo/d29a4a0fa
      to specify a particular repository, and [options] can include:
    EOS
  end
  Trollop.die "must provide a commit sha" unless ARGV.size == 1

  begin
    repo, sha = Commands.parse_commit(ARGV[0])
  rescue RuntimeError => e
    Trollop.die e.message
  end

  begin
    result = BarkeepClient.commits(configuration, repo, [sha]).values[0]
  rescue RuntimeError => e
    puts e.message
    exit 1
  end

  # Try xdg-open (linux) open (mac os). Otherwise throw an error.
  open_command = ["xdg-open", "open"].reject { |c| `which #{c}`.empty? }.first
  unless open_command
    puts "No application available to open a url (tried 'xdg-open' and 'open')."
    exit 1
  end

  puts `#{open_command} #{result["link"]}`
end