publicity 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,6 +65,7 @@ EOS
65
65
  # will get cleaned up after the begin block.
66
66
  tmpdir = Dir.mktmpdir(nil, '/tmp')
67
67
  begin
68
+ # Determine the credential helper we'll be using
68
69
  if @auth_type === 'basic'
69
70
  # If the user is using basic auth, we'll need to put their credentials
70
71
  # somewhere Git can access.
@@ -85,7 +86,7 @@ EOS
85
86
  dir = "#{tmpdir}/#{remote_repo[:name]}.git"
86
87
 
87
88
  # Clone the original repo
88
- `git clone #{remote_repo.clone_url} #{dir} 1> /dev/null`
89
+ `git clone --bare #{remote_repo.clone_url} #{dir} 1> /dev/null`
89
90
 
90
91
  puts "Finished cloning."
91
92
 
@@ -93,11 +94,11 @@ EOS
93
94
  local_repo = Rugged::Repository.new(dir)
94
95
 
95
96
  # Get a list of all the branches, and filter out irrelevant branches
96
- regex = /(#{Regexp.quote(@username)}|master)/
97
- branches = Rugged::Branch.each_name(local_repo, :remote)
98
- .sort
99
- .select { |branch| branch =~ regex }
100
- .each { |i| i.gsub!(/origin\//, '') }
97
+ branches = []
98
+ regex = /(#{Regexp.quote(@username)}|master|.*2013)/
99
+ Rugged::Branch.each(local_repo) do |branch|
100
+ branches.push(branch) if branch.name =~ regex
101
+ end
101
102
 
102
103
  # Ask the user which branch they want to keep; we'll end up deleting
103
104
  # all other branches. Skip over this prompt if the repo only has a
@@ -109,27 +110,25 @@ way, anyone who looks at your repository will see your code right away, rather
109
110
  than the repository's boilerplate code.
110
111
 
111
112
  EOS
112
- if branches.include?('master') and branches.length === 1
113
- branch_name = 'master'
114
- puts "Only found branch `#{branch_name}`. Using that one."
113
+ if branches.any? { |b| b.name == 'master' } and branches.length === 1
114
+ branch = branches[0]
115
+ puts "Only found branch `#{branch.name}`. Using that one."
115
116
  else
116
- puts "Here's a list of the branches you can keep:", ''
117
- branches.each { |branch| puts branch }
118
- puts ''
119
-
120
- branch_name = ask("Please enter the name of the branch you'd like to keep: ") do |q|
121
- # Validate that the string isn't just whitespace and that it's a
122
- # valid branch name. First check is sort of redundant, but whatev.
123
- q.validate = lambda { |p| p =~ /^(?!\s*$).+/ and branches.include?(p) }
124
- q.responses[:not_valid] = "Sorry, that's an invalid branch name. Please enter the name of the branch you'd like to keep: "
117
+ branch = choose do |menu|
118
+ menu.index = :number
119
+ menu.select_by = :index
120
+ menu.prompt = "Please enter the number of the branch you'd like to keep: "
121
+ menu.choices(*branches.map { |item| item.name }) do |selection|
122
+ branches.detect { |b| b.name == selection }
123
+ end
125
124
  end
126
125
  end
127
126
 
128
127
  # Replace the master branch with the user's branch. This ensures that
129
128
  # anyone viewing the repo on GitHub is first presented with the user's
130
129
  # code, not some empty boilerplate project.
131
- if not branch_name =~ /^master$/
132
- Rugged::Branch.lookup(local_repo, branch_name)
130
+ if not branch.name =~ /^master$/
131
+ branch.move('master')
133
132
  end
134
133
 
135
134
  # Delete all remote branches
@@ -140,7 +139,7 @@ EOS
140
139
  end
141
140
 
142
141
  # Generate a readme and commit it to the repository.
143
- create_readme!(local_repo, project)
142
+ create_readme!(local_repo, project, remote_repo[:name])
144
143
 
145
144
  # Rename the source repo on GitHub to make way for the (nicenamed) new
146
145
  # repository.
@@ -274,26 +273,29 @@ EOS
274
273
  result
275
274
  end
276
275
 
277
- def create_readme!(repo, data)
276
+ def create_readme!(repo, data, remote_repo_name)
278
277
  text = <<EOS
279
- ##{data[:name]}
278
+ ##{remote_repo_name}
280
279
 
281
280
  This is a copy of the work I did on a private repo, originally a project from
282
281
  the [Hack Reactor](http://hackreactor.com) curriculum. This project was worked
283
282
  on with a pair, and as such is representative of the kind of problems that I've
284
283
  tackled, but not of my solo work.
285
284
 
286
- For a better perspective on my own work, please see #{data[:name]}(#{data[:url]}).
285
+ For a better perspective on my own work, please see [#{data[:name]}](#{data[:url]}).
287
286
  EOS
288
287
 
289
288
  oid = repo.write(text, :blob)
290
289
  index = repo.index
290
+ # Read the current tree into the index. This prevents us from overwriting
291
+ # the current index (and thus replacing the entire current tree)
292
+ index.read_tree(repo.lookup(repo.head.target).tree)
291
293
  index.add(:path => "README.md", :oid => oid, :mode => 0100644)
292
294
 
293
295
  author = { :email => "accounts@hackreactor.com", :name => 'Hack Reactor', :time => Time.now }
294
296
 
295
297
  Rugged::Commit.create(repo,
296
- :tree => repo.lookup(repo.index.write_tree),
298
+ :tree => repo.lookup(index.write_tree),
297
299
  :author => author,
298
300
  :committer => author,
299
301
  :message => "Add a readme (automated commit)",
@@ -1,3 +1,3 @@
1
1
  module Publicity
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: publicity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
12
+ date: 2013-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler