Class: Gitmine

Inherits:
Object
  • Object
show all
Defined in:
lib/gitmine.rb,
lib/gitmine/issue.rb,
lib/gitmine/branch.rb,
lib/gitmine/config.rb

Defined Under Namespace

Classes: Branch, CLI, Commit, Config, Git, Issue, LocalBranch, RemoteBranch

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitmine

Returns a new instance of Gitmine.



40
41
42
43
# File 'lib/gitmine.rb', line 40

def initialize
  @repo = Grit::Repo.new(ENV['PWD'])
  @branch = File.read('./.git/HEAD').match(/^ref: refs\/heads\/(.+)/)[1]
end

Class Method Details

.branch(branch_name) ⇒ Object

TODO specs



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
# File 'lib/gitmine.rb', line 66

def self.branch(branch_name)
  issue_id = branch_name[/^\d+/]
  original_branch = File.read('./.git/HEAD').match(/^ref: refs\/heads\/(.+)/)[1]

  raise "Invalid branch name. It should look like 123-my-branch" unless branch_name[/^\d+-/]

  issue = Issue.find(issue_id)

  raise "Issue ##{issue_id} does not exists" if issue.nil?

  puts yellow("Create the branch #{branch_name}")
  run_cmd("git checkout -b #{branch_name}")

  puts yellow("Push it to origin")
  run_cmd("git push origin #{branch_name}")

  puts yellow("Make the local branch tracking the remote")
  run_cmd("git branch --set-upstream #{branch_name} origin/#{branch_name}")

  puts yellow("Adding a note to the Issue ##{issue_id}")
  note = "Branch *#{branch_name}* created from #{original_branch}"
  if Config.github
    note << %{ - "See on Github":https://github.com/#{Config.github}/tree/#{branch_name}}
    note << %{ - "Compare on Github":https://github.com/#{Config.github}/compare/#{original_branch}...#{branch_name}}
  end

  issue.add_note(note)
end

.checkout(issue_id) ⇒ Object

TODO specs



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gitmine.rb', line 96

def self.checkout(issue_id)
  local_branch = LocalBranch.find(issue_id).name
  if local_branch
    run_cmd("git checkout #{local_branch}")
    return
  end

  remote_branch = RemoteBranch.find(issue_id).name
  if remote_branch
    run_cmd("git checkout -b #{remote_branch} origin/#{remote_branch}")
    return
  end

  raise "Can't find branch starting with #{issue_id}"
end

.delete(issue_id) ⇒ Object

TODO specs



113
114
115
# File 'lib/gitmine.rb', line 113

def self.delete(issue_id)
  RemoteBranch.find(issue_id).delete
end

.listObject



10
11
12
13
14
15
16
17
# File 'lib/gitmine.rb', line 10

def self.list
  gm = Gitmine.new
  gm.commits.each do |commit|
    status = commit.issue.status if commit.issue
    status ||= 'N/A'
    puts "#{commit.id[0..6]} #{status.ljust(12)} #{(commit.committer.name || "").ljust(15)} #{commit.message[0..50].gsub("\n", '')}"
  end
end

.openObject



29
30
31
# File 'lib/gitmine.rb', line 29

def self.open
  Gitmine.new.open
end

.reviewed(issue_id) ⇒ Object

TODO specs



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gitmine.rb', line 118

def self.reviewed(issue_id)
  checkout(issue_id)
  # Make sure we get the latest commits
  Git.pull

  issue = Issue.find(issue_id)

  puts yellow("Merge #{issue_id} to master and push")
  issue.local_branch.merge_to_master

  puts yellow("Delete remote branch")
  issue.remote_branch.delete

  puts yellow("Set Ticket status to 'reviewed'")
  issue.update_status("reviewed")
end

.statusObject



19
20
21
# File 'lib/gitmine.rb', line 19

def self.status
  Gitmine.new.status
end

Instance Method Details

#commitsObject



45
46
47
48
49
# File 'lib/gitmine.rb', line 45

def commits
  @repo.commits(@branch).map do |c|
    Commit.new(c)
  end
end

#issueObject

Return issue for current branch or nil



56
57
58
59
60
61
62
63
# File 'lib/gitmine.rb', line 56

def issue
  if issue_id 
    Issue.find(issue_id)
  else
    puts "No issue found for branch #{@branch}"
    nil
  end
end

#issue_idObject



51
52
53
# File 'lib/gitmine.rb', line 51

def issue_id
  @branch[/^\d+/]
end

#openObject



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

def open
  issue_url = "#{Config.redmine_host}/issues/#{issue_id}"
  if issue
    Launchy.open issue_url
  end
end

#statusObject



23
24
25
26
27
# File 'lib/gitmine.rb', line 23

def status
  if issue
    puts "#{bold(issue.status)} - #{issue.subject} #{blue("(#{issue.assigned_to})")}"
  end
end