gitpusher 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new do |task|
7
+ task.rspec_opts = ['--format=documentation', '--colour']
8
+ end
@@ -19,4 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.add_dependency 'grit'
20
20
  s.add_dependency 'octokit'
21
21
  s.add_dependency 'pit'
22
+
23
+ s.add_development_dependency 'rspec'
22
24
  end
@@ -4,6 +4,7 @@ module GitPusher
4
4
  def self.load(options)
5
5
  context = GitPusher::Context.instance
6
6
  context.config = YAML.load_file(File.join(options[:config]))
7
+ context.config[:base_dir] = File.expand_path(context.config[:base_dir])
7
8
  end
8
9
  end
9
10
  end
@@ -1,23 +1,21 @@
1
1
  # -*- coding: utf-8 -*-
2
+ require 'fileutils'
2
3
  require 'grit'
3
4
 
4
5
  module GitPusher
5
6
  class Runner
6
7
  def self.run
7
- context = GitPusher::Context.instance
8
+ context = Context.instance
8
9
 
9
- src = GitPusher::Service::Factory.create(context.config[:src])
10
- dest = GitPusher::Service::Factory.create(context.config[:dest])
11
-
12
- base_dir = context.config[:base_dir]
13
10
  src_repos = src.repos
14
11
  num_per_process = src_repos.length / context.processes
15
12
  num_per_process += 1 unless src_repos.length % context.processes == 0
13
+ FileUtils::Verbose.mkpath base_dir unless File.directory? base_dir
16
14
  Dir.chdir(base_dir) do
17
15
  src_repos.each_slice(num_per_process) do |repos|
18
16
  fork do
19
17
  repos.each do |src_repo|
20
- mirror src_repo, dest, base_dir
18
+ mirror src_repo
21
19
  end
22
20
  end
23
21
  end
@@ -26,53 +24,48 @@ module GitPusher
26
24
  Process.waitall
27
25
  end
28
26
 
29
- def self.mirror(src_repo, dest, base_dir)
27
+ def self.mirror(src_repo)
30
28
  repo_name = File.basename(src_repo.url).gsub(/.git$/, '')
31
- repo_path = File.join(base_dir, repo_name)
32
- puts "[#{Process.pid}][#{repo_name}]Cheking #{src_repo.url} ..."
29
+ repo_path = File.join(base_dir, "#{repo_name}.git")
33
30
  unless File.exist?(repo_path)
34
- `git clone #{src_repo.url}`
31
+ puts "[#{Process.pid}][#{repo_name}]Cloning #{src_repo.url} ..."
32
+ `git clone --mirror #{src_repo.url}`
35
33
  end
36
34
 
37
35
  local_repo = Grit::Repo.new(repo_path)
38
36
 
39
- # local repo の git remote で mirror があるかどうかチェック
40
37
  has_remote_mirror = false
41
38
  local_repo.remote_list.each do |remote|
42
39
  has_remote_mirror = true if remote === 'mirror'
43
40
  end
44
41
 
42
+ mirror_repo = dest.repo(repo_name) || dest.create_repo(repo_name)
45
43
  unless has_remote_mirror
46
- mirror_repo = dest.repo(repo_name) || dest.create_repo(repo_name)
47
44
  local_repo.git.remote({}, 'add', 'mirror', mirror_repo.url)
48
45
  end
49
46
 
50
- local_repo.remotes.each do |remote|
51
- next if remote.name == 'origin/HEAD'
52
- next if remote.name =~ %r!mirror/.+!
53
- branch = remote.name.gsub(%r!^origin/!, '')
47
+ Dir.chdir(repo_path) do
48
+ puts "[#{Process.pid}][#{repo_name}]Pruning all stale branches of #{repo_name} ..."
49
+ local_repo.git.remote({}, 'prune', 'origin')
54
50
 
55
- matched = false
56
- local_repo.branches.each do |x|
57
- matched = true if x.name === branch
58
- end
51
+ puts "[#{Process.pid}][#{repo_name}]Fetching from #{src_repo.url} ..."
52
+ local_repo.git.fetch({ :timeout => 300 }, 'origin')
59
53
 
60
- unless matched
61
- local_repo.git.branch({}, branch, remote.name)
62
- end
54
+ puts "[#{Process.pid}][#{repo_name}]Pushing to #{mirror_repo.url} ..."
55
+ local_repo.git.push({ :timeout => 300 }, 'mirror','--mirror')
56
+ end
57
+ end
63
58
 
64
- Dir.chdir(repo_path) do
65
- local_repo.git.checkout({}, branch)
59
+ def self.src
60
+ Service::Factory.create(Context.instance.config[:src])
61
+ end
66
62
 
67
- # pull する
68
- puts "[#{Process.pid}][#{repo_name}]Pulling #{branch} ..."
69
- local_repo.git.pull({ :timeout => 300 }, 'origin', branch)
63
+ def self.dest
64
+ Service::Factory.create(Context.instance.config[:dest])
65
+ end
70
66
 
71
- # git push mirror #{branch} する
72
- puts "[#{Process.pid}][#{repo_name}]Pushing #{branch} ..."
73
- local_repo.git.push({ :timeout => 300 }, 'mirror', branch)
74
- end
75
- end
67
+ def self.base_dir
68
+ Context.instance.config[:base_dir]
76
69
  end
77
70
  end
78
71
  end
@@ -1,3 +1,3 @@
1
1
  module GitPusher
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require_relative 'spec_helper'
2
+ require 'tempfile'
3
+ require 'pathname'
4
+
5
+ describe GitPusher::Config do
6
+
7
+ subject { Context.instance.config }
8
+
9
+ describe '.load' do
10
+ context 'with base_dir including tilda in config file' do
11
+ Tempfile.open 'gitpusher' do |file|
12
+ file.write ':base_dir: ~/var/repo'
13
+ file.rewind
14
+ GitPusher::Config.load({ config: file.path })
15
+ it 'ensure base_dir absolute path' do
16
+ expect(Pathname(subject[:base_dir]).absolute?).to be_true
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'with base_dir including relative path in config file' do
22
+ Tempfile.open 'gitpusher' do |file|
23
+ file.write ':base_dir: var/repo'
24
+ file.rewind
25
+ GitPusher::Config.load({ config: file.path })
26
+ it 'ensure base_dir absolute path' do
27
+ expect(Pathname(subject[:base_dir]).absolute?).to be_true
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'spec_helper'
2
+ require 'tmpdir'
3
+
4
+ describe Runner do
5
+
6
+ before :each do
7
+ context = double('context')
8
+ Context.stub(:instance).and_return(context)
9
+ end
10
+
11
+ describe '.run' do
12
+ context 'when base_dir does not exist' do
13
+ it 'make base_dir' do
14
+ Context.instance.stub(:processes).and_return(1)
15
+ Dir.mktmpdir do |dir|
16
+ src = double('src')
17
+ src.stub(:repos).and_return([:src, :repos])
18
+ Runner.stub(:src).and_return(src)
19
+ Runner.stub(:base_dir).and_return(File.join(dir, 'non/exisnting/directory'))
20
+ Runner.stub(:mirror)
21
+ Runner.run
22
+ expect(File.directory? Runner.base_dir).to be_true
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ require 'gitpusher'
2
+
3
+ include GitPusher
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitpusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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: 2012-07-17 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grit
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description:
63
79
  email:
64
80
  - gosukenator@gmail.com
@@ -89,6 +105,9 @@ files:
89
105
  - lib/gitpusher/service/factory.rb
90
106
  - lib/gitpusher/service/github.rb
91
107
  - lib/gitpusher/version.rb
108
+ - spec/config_spec.rb
109
+ - spec/runner_spec.rb
110
+ - spec/spec_helper.rb
92
111
  homepage: https://github.com/mizzy/gitpusher
93
112
  licenses: []
94
113
  post_install_message:
@@ -114,5 +133,7 @@ signing_key:
114
133
  specification_version: 3
115
134
  summary: A command line tool for replicating git repositories from one service to
116
135
  another.
117
- test_files: []
118
- has_rdoc:
136
+ test_files:
137
+ - spec/config_spec.rb
138
+ - spec/runner_spec.rb
139
+ - spec/spec_helper.rb