Project

General

Profile

Backport #3164 ยป data.txt

paranormal (paranormal dev), 04/18/2010 06:41 PM

 
1
desc "Open this repo's master branch in a web browser."
2
command :home do |user|
3
  if helper.project
4
    homepage = helper.homepage_for(user || helper.owner, 'master')
5
    homepage.gsub!(%r{/tree/master$}, '')
6
    helper.open homepage
7
  end
8
end
9

    
10
desc "Automatically set configuration info, or pass args to specify."
11
usage "github config [my_username] [my_repo_name]"
12
command :config do |user, repo|
13
  user ||= ENV['USER']
14
  repo ||= File.basename(FileUtils.pwd)
15
  git "config --global github.user #{user}"
16
  git "config github.repo #{repo}"
17
  puts "Configured with github.user #{user}, github.repo #{repo}"
18
end
19

    
20
desc "Open this repo in a web browser."
21
usage "github browse [user] [branch]"
22
command :browse do |user, branch|
23
  if helper.project
24
    # if one arg given, treat it as a branch name
25
    # unless it maches user/branch, then split it
26
    # if two args given, treat as user branch
27
    # if no args given, use defaults
28
    user, branch = user.split("/", 2) if branch.nil? unless user.nil?
29
    branch = user and user = nil if branch.nil?
30
    user ||= helper.branch_user
31
    branch ||= helper.branch_name
32
    helper.open helper.homepage_for(user, branch)
33
  end
34
end
35

    
36
desc 'Open the given user/project in a web browser'
37
usage 'github open [user/project]'
38
command :open do |arg|
39
  helper.open "http://github.com/#{arg}"
40
end
41

    
42
desc "Info about this project."
43
command :info do
44
  puts "== Info for #{helper.project}"
45
  puts "You are #{helper.owner}"
46
  puts "Currently tracking:"
47
  helper.tracking.sort { |(a,),(b,)| a == helper.origin ? -1 : b == helper.origin ? 1 : a.to_s <=> b.to_s }.each do |(name,user_or_url)|
48
    puts " - #{user_or_url} (as #{name})"
49
  end
50
end
51

    
52
desc "Track another user's repository."
53
usage "github track remote [user]"
54
usage "github track remote [user/repo]"
55
usage "github track [user]"
56
usage "github track [user/repo]"
57
flags :private => "Use git@github.com: instead of git://github.com/."
58
flags :ssh => 'Equivalent to --private'
59
command :track do |remote, user|
60
  # track remote user
61
  # track remote user/repo
62
  # track user
63
  # track user/repo
64
  user, remote = remote, nil if user.nil?
65
  die "Specify a user to track" if user.nil?
66
  user, repo = user.split("/", 2)
67
  die "Already tracking #{user}" if helper.tracking?(user)
68
  repo = @helper.project if repo.nil?
69
  repo.chomp!(".git")
70
  remote ||= user
71

    
72
  if options[:private] || options[:ssh]
73
    git "remote add #{remote} #{helper.private_url_for_user_and_repo(user, repo)}"
74
  else
75
    git "remote add #{remote} #{helper.public_url_for_user_and_repo(user, repo)}"
76
  end
77
end
78

    
79
desc "Fetch all refs from a user"
80
command :fetch_all do |user|
81
  GitHub.invoke(:track, user) unless helper.tracking?(user)
82
  git "fetch #{user}"
83
end
84

    
85
desc "Fetch from a remote to a local branch."
86
command :fetch do |user, branch|
87
  die "Specify a user to pull from" if user.nil?
88
  user, branch = user.split("/", 2) if branch.nil?
89
  branch ||= 'master'
90
  GitHub.invoke(:track, user) unless helper.tracking?(user)
91

    
92
  die "Unknown branch (#{branch}) specified" unless helper.remote_branch?(user, branch)
93
  die "Unable to switch branches, your current branch has uncommitted changes" if helper.branch_dirty?
94

    
95
  puts "Fetching #{user}/#{branch}"
96
  git "fetch #{user} #{branch}:refs/remotes/#{user}/#{branch}"
97
  git "update-ref refs/heads/#{user}/#{branch} refs/remotes/#{user}/#{branch}"
98
  git_exec "checkout #{user}/#{branch}"
99
end
100

    
101
desc "Pull from a remote."
102
usage "github pull [user] [branch]"
103
flags :merge => "Automatically merge remote's changes into your master."
104
command :pull do |user, branch|
105
  die "Specify a user to pull from" if user.nil?
106
  user, branch = user.split("/", 2) if branch.nil?
107

    
108
  if !helper.network_members(user, {}).include?(user)
109
    git_exec "#{helper.argv.join(' ')}".strip
110
    return
111
  end
112

    
113
  branch ||= 'master'
114
  GitHub.invoke(:track, user) unless helper.tracking?(user)
115

    
116
  die "Unable to switch branches, your current branch has uncommitted changes" if helper.branch_dirty?
117

    
118
  if options[:merge]
119
    git_exec "pull #{user} #{branch}"
120
  else
121
    puts "Switching to #{user}-#{branch}"
122
    git "fetch #{user}"
123
    git_exec "checkout -b #{user}/#{branch} #{user}/#{branch}"
124
  end
125
end
126

    
127
desc "Clone a repo. Uses ssh if current user is "
128
usage "github clone [user] [repo] [dir]"
129
flags :ssh => "Clone using the git@github.com style url."
130
flags :search => "Search for [user|repo] and clone selected repository"
131
command :clone do |user, repo, dir|
132
  die "Specify a user to pull from" if user.nil?
133
  if options[:search]
134
    query = [user, repo, dir].compact.join(" ")
135
    data = JSON.parse(open("http://github.com/api/v1/json/search/#{URI.escape query}").read)
136
    if (repos = data['repositories']) && !repos.nil? && repos.length > 0
137
      repo_list = repos.map do |r|
138
        { "name" => "#{r['username']}/#{r['name']}", "description" => r['description'] }
139
      end
140
      formatted_list = helper.format_list(repo_list).split("\n")
141
      if user_repo = GitHub::UI.display_select_list(formatted_list)
142
        user, repo = user_repo.strip.split('/', 2)
143
      end
144
    end
145
    die "Perhaps try another search" unless user && repo
146
  end
147

    
148
  if user.include?('/') && !user.include?('@') && !user.include?(':')
149
    die "Expected user/repo dir, given extra argument" if dir
150
    (user, repo), dir = [user.split('/', 2), repo]
151
  end
152

    
153
  if repo
154
    if options[:ssh] || current_user?(user)
155
      git_exec "clone git@github.com:#{user}/#{repo}.git" + (dir ? " #{dir}" : "")
156
    else
157
      git_exec "clone git://github.com/#{user}/#{repo}.git" + (dir ? " #{dir}" : "")
158
    end
159
  else
160
    git_exec "#{helper.argv.join(' ')}".strip
161
  end
162
end
163

    
164
desc "Generate the text for a pull request."
165
usage "github pull-request [user] [branch]"
166
command :'pull-request' do |user, branch|
167
  if helper.project
168
    die "Specify a user for the pull request" if user.nil?
169
    user, branch = user.split('/', 2) if branch.nil?
170
    branch ||= 'master'
171
    GitHub.invoke(:track, user) unless helper.tracking?(user)
172

    
173
    git_exec "request-pull #{user}/#{branch} #{helper.origin}"
174
  end
175
end
176

    
177
desc "Create a new, empty GitHub repository"
178
usage "github create [repo]"
179
flags :markdown => 'Create README.markdown'
180
flags :mdown => 'Create README.mdown'
181
flags :textile => 'Create README.textile'
182
flags :rdoc => 'Create README.rdoc'
183
flags :rst => 'Create README.rst'
184
flags :private => 'Create private repository'
185
command :create do |repo|
186
  sh "curl -F 'repository[name]=#{repo}' -F 'repository[public]=#{!options[:private]}' -F 'login=#{github_user}' -F 'token=#{github_token}' http://github.com/repositories"
187
  mkdir repo
188
  cd repo
189
  git "init"
190
  extension = options.keys.first
191
  touch extension ? "README.#{extension}" : "README"
192
  git "add *"
193
  git "commit -m 'First commit!'"
194
  git "remote add origin git@github.com:#{github_user}/#{repo}.git"
195
  git_exec "push origin master"
196
end
197

    
198
desc "Forks a GitHub repository"
199
usage "github fork"
200
usage "github fork [user]/[repo]"
201
command :fork do |user, repo|
202
  if repo.nil?
203
    if user
204
      user, repo = user.split('/')
205
    else
206
      unless helper.remotes.empty?
207
        is_repo = true
208
        user = helper.owner
209
        repo = helper.project
210
      else
211
        die "Specify a user/project to fork, or run from within a repo"
212
      end
213
    end
214
  end
215

    
216
  sh "curl -F 'login=#{github_user}' -F 'token=#{github_token}' http://github.com/#{user}/#{repo}/fork"
217

    
218
  url = "git@github.com:#{github_user}/#{repo}.git"
219
  if is_repo
220
    git "config remote.origin.url #{url}"
221
    puts "#{user}/#{repo} forked"
222
  else
223
    puts "Giving GitHub a moment to create the fork..."
224
    sleep 3
225
    git_exec "clone #{url}"
226
  end
227
end
228

    
229
desc "Create a new GitHub repository from the current local repository"
230
flags :private => 'Create private repository'
231
command :'create-from-local' do
232
  cwd = sh "pwd"
233
  repo = File.basename(cwd)
234
  is_repo = !git("status").match(/fatal/)
235
  raise "Not a git repository. Use gh create instead" unless is_repo
236
  sh  "curl -F 'repository[name]=#{repo}' -F 'repository[public]=#{!options[:private].inspect}' -F 'login=#{github_user}' -F 'token=#{github_token}' http://github.com/repositories"
237
  git "remote add origin git@github.com:#{github_user}/#{repo}.git"
238
  git_exec "push origin master"
239
end
240

    
241
desc "Search GitHub for the given repository name."
242
usage "github search [query]"
243
command :search do |query|
244
  die "Usage: github search [query]" if query.nil?
245
  data = JSON.parse(open("http://github.com/api/v1/json/search/#{URI.escape query}").read)
246
  if (repos = data['repositories']) && !repos.nil? && repos.length > 0
247
    puts repos.map { |r| "#{r['username']}/#{r['name']}"}.sort.uniq
248
  else
249
    puts "No results found"
250
  end
251
end