Module: GitReview::Commands

Extended by:
Commands
Includes:
Internals
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.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/git-review/commands.rb', line 59

def approve(number)
  request = get_request_by_number(number)
  repo = github.source_repo
  # TODO: Make this configurable.
  comment = 'Reviewed and approved.'
  response = github.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.



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

def browse(number)
  request = get_request_by_number(number)
  Launchy.open(request.html_url)
end

#checkout(number, branch = false) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git-review/commands.rb', line 44

def checkout(number, branch=false)
  request = 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'
  puts
  if branch
    git_call("checkout #{request.head.ref}")
  else
    git_call("checkout pr/#{request.number}")
  end
end

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

delete obsolete branches (left over from already closed requests)



148
149
150
151
152
153
154
155
156
157
# File 'lib/git-review/commands.rb', line 148

def clean(number=nil, force=false, all=false)
  # pruning is needed to remove deleted branches from your local track
  git_call('remote prune origin')
  # determine strategy to clean.
  if all
    local.clean_all
  else
    local.clean_single(number, force)
  end
end

#close(number) ⇒ Object

Close a specified request.



93
94
95
96
97
98
99
100
# File 'lib/git-review/commands.rb', line 93

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

#consoleObject

Start a console session (used for debugging)



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/git-review/commands.rb', line 160

def console
  puts 'Entering debug console.'
  if RUBY_VERSION == '2.0.0'
    require 'byebug'
    byebug
  else
    require 'ruby-debug'
    Debugger.start
    debugger
  end
  puts 'Leaving debug console.'
end

#create(upstream = false) ⇒ Object

Create a new request. TODO: Support creating requests to other repositories and branches (like

the original repo, this has been forked from).


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git-review/commands.rb', line 121

def create(upstream=false)
  # prepare original_branch and local_branch
  original_branch, local_branch = prepare
  # don't create request with uncommitted changes in current branch
  unless git_call('diff HEAD').empty?
    puts 'You have uncommitted changes.'
    puts 'Please stash or commit before creating the request.'
    return
  end
  if git_call("cherry #{local.target_branch}").empty?
    puts 'Nothing to push to remote yet. Commit something first.'
  else
    if github.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)
    git_call("push --set-upstream origin #{local_branch}", debug_mode, true)
    create_pull_request(upstream)
    # return to the user's original branch
    # FIXME: keep track of original branch etc
    git_call("checkout #{original_branch}")
  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 = github.current_requests_full.reject { |request|
    # find only pending (= unmerged) requests and output summary
    # explicitly look for local changes Github does not yet know about
    local.merged?(request.head.sha)
  }
  requests.reverse! if reverse
  source = local.source
  if requests.empty?
    puts "No pending requests for '#{source}'."
  else
    puts "Pending requests for '#{source}':"
    puts "ID      Updated    Comments  Title"
    requests.each { |request| print_request(request) }
  end
end

#merge(number) ⇒ Object

Accept a specified request by merging it into master.



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

def merge(number)
  request = 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(new = false, name = nil) ⇒ Array(String, String)

Prepare local repository to create a new request. People should work on local branches, but especially for single commit

changes, more often than not, they don't. Therefore we create a branch
for them, to be able to use code review the way it is intended.

Returns:

  • (Array(String, String))

    the original branch and the local branch



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

def prepare(new=false, name=nil)
  # remember original branch the user was currently working on
  original_branch = local.source_branch
  if new || !local.on_feature_branch?
    local_branch = move_uncommitted_changes(local.target_branch, name)
  else
    local_branch = original_branch
  end
  [original_branch, local_branch]
end

#show(number, full = false) ⇒ Object

Show details for a single request.



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

def show(number, full=false)
  request = get_request_by_number(number)
  # determine whether to show full diff or just stats
  option = full ? '' : '--stat '
  diff = "diff --color=always #{option}HEAD...#{request.head.sha}"
  print_request_details(request)
  puts git_call(diff)
  print_request_discussions(request)
end