Module: GitReview::Commands

Extended by:
Commands
Includes:
Helpers
Included in:
Commands
Defined in:
lib/git-review/commands.rb

Instance Method Summary collapse

Instance Method Details

#approve(number) ⇒ Object

Add an approving comment to the request.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/git-review/commands.rb', line 74

def approve(number)
  request = server.get_request_by_number(number)
  repo = server.source_repo
  # TODO: Make this configurable.
  comment = 'Reviewed and approved.'
  response = server.add_comment(repo, request.number, comment)
  if response[:body] == comment
    puts 'Successfully approved request.'
  else
    puts response[:message]
  end
end

#browse(number) ⇒ Object

Open a browser window and review a specified request.



39
40
41
42
43
# File 'lib/git-review/commands.rb', line 39

def browse(number)
  request = server.get_request_by_number(number)
  # FIXME: Use request.html_url as soon as we are using our Request model.
  Launchy.open request._links.html.href
end

#checkout(number, branch = true) ⇒ Object

Checkout a specified request’s changes to your local repository.



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
# File 'lib/git-review/commands.rb', line 46

def checkout(number, branch = true)
  request = server.get_request_by_number(number)
  puts 'Checking out changes to your local repository.'
  puts 'To get back to your original state, just run:'
  puts
  puts '  git checkout master'.pink
  puts
  # Ensure we are looking at the right remote.
  remote = local.remote_for_request(request)
  git_call "fetch #{remote}"
  # Checkout the right branch.
  branch_name = request.head.ref
  if branch
    if local.branch_exists?(:local, branch_name)
      if local.source_branch == branch_name
        puts "On branch #{branch_name}."
      else
        git_call "checkout #{branch_name}"
      end
    else
      git_call "checkout --track -b #{branch_name} #{remote}/#{branch_name}"
    end
  else
    git_call "checkout #{remote}/#{branch_name}"
  end
end

#clean(number = nil, force = false, all = false) ⇒ Object

Remove remotes with ‘review’ prefix (left over from previous reviews). Prune all existing remotes and delete obsolete branches (left over from already closed requests).



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/git-review/commands.rb', line 171

def clean(number = nil, force = false, all = false)
  git_call "checkout #{local.target_branch}"
  local.prune_remotes
  # Determine strategy to clean.
  if all
    local.clean_all
  else
    local.clean_single(number, force)
  end
  # Remove al review remotes without existing local branches.
  local.clean_remotes
end

#close(number) ⇒ Object

Close a specified request.



108
109
110
111
112
113
114
115
# File 'lib/git-review/commands.rb', line 108

def close(number)
  request = server.get_request_by_number(number)
  repo = server.source_repo
  server.close_issue(repo, request.number)
  unless server.request_exists?('open', request.number)
    puts 'Successfully closed request.'
  end
end

#console(number = nil) ⇒ Object

Start a console session (used for debugging)



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/git-review/commands.rb', line 185

def console(number = nil)
  puts 'Entering debug console.'
  request = server.get_request_by_number(number) if number

  if RUBY_VERSION.to_f >= 2
    begin
      require 'byebug'
      byebug
    rescue LoadError => e
      puts
      puts 'Missing debugger, please install byebug:'
      puts '  gem install byebug'
      puts
    end
  else
    begin
      require 'ruby-debug'
      Debugger.start
      debugger
    rescue LoadError => e
      puts
      puts 'Missing debugger, please install ruby-debug:'
      puts '  gem install ruby-debug'
      puts
    end
  end
  puts 'Leaving debug console.'
end

#create(upstream = false) ⇒ Object

Create a new request.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/git-review/commands.rb', line 138

def create(upstream = false)
  # Prepare original_branch and local_branch.
  # TODO: Allow to use the same switches and parameters that prepare takes.
  original_branch, local_branch = prepare
  # Don't create request with uncommitted changes in current branch.
  if local.uncommitted_changes?
    puts 'You have uncommitted changes.'
    puts 'Please stash or commit before creating the request.'
    return
  end
  if local.new_commits?(upstream)
    # Feature branch differs from local or upstream master.
    if server.request_exists_for_branch?(upstream)
      puts 'A pull request already exists for this branch.'
      puts 'Please update the request directly using `git push`.'
      return
    end
    # Push latest commits to the remote branch (create if necessary).
    remote = local.remote_for_branch(local_branch) || 'origin'
    git_call(
      "push --set-upstream #{remote} #{local_branch}", debug_mode, true
    )
    server.send_pull_request upstream
    # Return to the user's original branch.
    git_call "checkout #{original_branch}"
  else
    puts 'Nothing to push to remote yet. Commit something first.'
  end
end

#list(reverse = false) ⇒ Object

List all pending requests.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/git-review/commands.rb', line 9

def list(reverse = false)
  requests = server.current_requests_full.reject do |request|
    # Find only pending (= unmerged) requests and output summary.
    # Explicitly look for local changes git does not yet know about.
    # TODO: Isn't this a bit confusing? Maybe display pending pushes?
    local.merged? request.head.sha
  end
  source = local.source
  if requests.empty?
    puts "No pending requests for '#{source}'."
  else
    puts "Pending requests for '#{source}':"
    puts "ID      Updated    Comments  Title".pink
    print_requests(requests, reverse)
  end
end

#merge(number) ⇒ Object

Accept a specified request by merging it into master.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git-review/commands.rb', line 88

def merge(number)
  request = server.get_request_by_number(number)
  if request.head.repo
    message = "Accept request ##{request.number} " +
        "and merge changes into \"#{local.target}\""
    command = "merge -m '#{message}' #{request.head.sha}"
    puts
    puts "Request title:"
    puts "  #{request.title}"
    puts
    puts "Merge command:"
    puts "  git #{command}"
    puts
    puts git_call(command)
  else
    print_repo_deleted request
  end
end

#prepare(force_new_branch = false, feature_name = nil) ⇒ Object

Prepare local repository to create a new request. NOTE:

People should work on local branches, but especially for single commit
changes, more often than not, they don't. Therefore this is called
automatically before creating a pull request, such that we create a
proper feature branch for them, to be able to use code review the way it
is intended.


124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/git-review/commands.rb', line 124

def prepare(force_new_branch = false, feature_name = nil)
  current_branch = local.source_branch
  if force_new_branch || !local.on_feature_branch?
    feature_name ||= get_branch_name
    feature_branch = move_local_changes(
      current_branch, local.sanitize_branch_name(feature_name)
    )
  else
    feature_branch = current_branch
  end
  [current_branch, feature_branch]
end

#show(number, full = false) ⇒ Object

Show details for a single request.



27
28
29
30
31
32
33
34
35
36
# File 'lib/git-review/commands.rb', line 27

def show(number, full = false)
  request = server.get_request_by_number(number)
  # Determine whether to show full diff or stats only.
  option = full ? '' : '--stat '
  diff = "diff --color=always #{option}HEAD...#{request.head.sha}"
  # TODO: Refactor into using Request model.
  print_request_details request
  puts git_call(diff)
  print_request_discussions request
end