cmer-git-p4-sync 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Carl Mercier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ = git-p4-sync
2
+
3
+ Submit changes made to a Git repository into to Perforce
4
+
5
+ == Use case
6
+
7
+ Joe starts an amazing startup with a few l337 h4x0rs buddies. Obviously, they use Git as their SCM because of their awesomeness. Amazing Startup is eventually acquired by Big Co Inc, which uses Perforce as their SCM. The l337 h4x0rs are obviously not willing to give up Git, and convincing 3000 employees and C-Level executives to drop Perforce for Git sounds like mission impossible. Enters git-p4-sync. It allows l337 h4x0rs to sync a Git repository into Perforce with a simple command. It is easier to use than git-p4, and does just one thing: sync changes made on the Git repository into Perforce.
8
+
9
+ A good way to use this script is to run it as a cronjob.
10
+
11
+ The --test switch is useful to test git-p4-sync without actually doing anything to your files.
12
+
13
+ = Example
14
+ $ git-p4-sync -h # get help
15
+ $ git-p4-sync -g ~/git/my-project/ -p ~/p4/my-project/ --pull --submit # do it
16
+ $ git-p4-sync -g ~/git/my-project/ -p ~/p4/my-project/ --pull --submit --test # fake it
17
+
18
+ == Dependencies (for tests only)
19
+
20
+ - Git command line executables
21
+ - Perforce command line executable
22
+ - diff_dirs gem
23
+
24
+ == COPYRIGHT
25
+
26
+ Copyright (c) 2009 {Carl Mercier}[http://carlmercier.com]. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib' << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gemspec|
15
+ gemspec.name = "git-p4-sync"
16
+ gemspec.summary = "Submit changes made to a Git repository into to Perforce"
17
+ gemspec.email = "carl@carlmercier.com"
18
+ gemspec.homepage = "http://github.com/cmer/git-p4-sync"
19
+ gemspec.description = "Submit changes made to a Git repository into to Perforce"
20
+ gemspec.authors = ["Carl Mercier"]
21
+ gemspec.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*"]
22
+ gemspec.rubyforge_project = 'git-p4-sync'
23
+ gemspec.add_dependency('diff_dirs', '>= 0.1.2')
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ begin
30
+ require 'rake/contrib/sshpublisher'
31
+ namespace :rubyforge do
32
+
33
+ desc "Release gem and RDoc documentation to RubyForge"
34
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
35
+
36
+ namespace :release do
37
+ desc "Publish RDoc to RubyForge."
38
+ task :docs => [:rdoc] do
39
+ config = YAML.load(
40
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
41
+ )
42
+
43
+ host = "#{config['username']}@rubyforge.org"
44
+ remote_dir = "/var/www/cmercier/git-p4-sync/"
45
+ local_dir = 'rdoc'
46
+
47
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
48
+ end
49
+ end
50
+ end
51
+ rescue LoadError
52
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ ##
3
+ ## git-p4-sync, by Carl Mercier (http://carlmercier.com)
4
+ ## Started in May 2009
5
+ ##
6
+ ## This script sync changes made to a Git repository back to Perforce.
7
+ ## It currently only supports one-way sync'ing: GIT -> P4
8
+ ##
9
+
10
+ path = File.dirname(__FILE__)
11
+ require path + '/../lib/init'
12
+ require path + '/../lib/git_p4_sync'
13
+ require path + '/../lib/start'
@@ -0,0 +1,103 @@
1
+ module GitP4Sync
2
+
3
+ def run(options)
4
+ # branch = options[:branch] || "master"
5
+ branch = "master"
6
+ git_path = options[:git]
7
+ p4_path = options[:p4]
8
+ simulate = options[:simulate] || false
9
+ pull = options[:pull] || false
10
+ submit = options[:submit] || false
11
+ ignore_list = ( options[:ignore] ? options[:ignore].split(",") : [".git"] )
12
+
13
+ git_path = add_slash(File.expand_path(git_path))
14
+ p4_path = add_slash(File.expand_path(p4_path))
15
+
16
+ verify_path_exist!(git_path)
17
+ verify_path_exist!(p4_path)
18
+
19
+ if pull
20
+ Dir.chdir(git_path) do
21
+ puts "Pulling Git from remote server."
22
+ run_cmd "git pull", simulate
23
+ end
24
+ end
25
+
26
+ diff = diff_dirs(p4_path, git_path)
27
+
28
+ if diff.size > 0
29
+ diff.each do |d|
30
+ action = d[0]
31
+ file = strip_leading_slash(d[1])
32
+
33
+ # todo: skip ignored files/directories
34
+
35
+ puts "#{action.to_s.upcase} in Git: #{file}"
36
+
37
+ Dir.chdir(p4_path) do
38
+ case action
39
+ when :new
40
+ run_cmd "cp -r #{git_path}#{file} #{p4_path}#{file}", simulate
41
+ run_cmd "#{p4_add_recursively("#{p4_path}#{file}")}", simulate
42
+ when :deleted
43
+ run_cmd "p4 delete #{p4_path}#{file}", simulate
44
+ when :modified
45
+ run_cmd "p4 edit #{p4_path}#{file}", simulate
46
+ run_cmd "cp #{git_path}#{file} #{p4_path}#{file}", simulate
47
+ else
48
+ puts "Unknown change type #{action}. Stopping."
49
+ exit 1
50
+ end
51
+ end
52
+ end
53
+
54
+ if submit
55
+ git_head_commit = ""
56
+ Dir.chdir(git_path) do
57
+ git_head_commit = `git show --pretty=oneline`.split("\n")[0]
58
+ end
59
+
60
+ Dir.chdir(p4_path) do
61
+ puts "Submitting changes to Perforce"
62
+ run_cmd "p4 submit -d '#{git_head_commit.gsub("'", "''")}'", simulate
63
+ end
64
+ end
65
+ else
66
+ puts "Directories are identical. Nothing to do."
67
+ exit 0
68
+ end
69
+ end
70
+
71
+ def run_cmd(cmd, simulate = false, puts_prefix = " ")
72
+ if simulate
73
+ puts "#{puts_prefix}simulation: #{cmd}"
74
+ else
75
+ puts "#{puts_prefix}#{cmd}"
76
+ end
77
+
78
+ output = ""
79
+ output = `#{cmd}` unless simulate
80
+ [output, $?]
81
+ end
82
+
83
+ def add_slash(path)
84
+ path += "/" unless path[-1..-1] == "/"
85
+ path
86
+ end
87
+
88
+ def strip_leading_slash(path)
89
+ path.sub(/^\//, "")
90
+ end
91
+
92
+ def p4_add_recursively(path)
93
+ "find #{path} -type f -print | p4 -x - add -f"
94
+ end
95
+
96
+ def verify_path_exist!(path)
97
+ if !File.exist?(path) || !File.directory?(path)
98
+ puts "#{path} must exist and be a directory."
99
+ exit 1
100
+ end
101
+ end
102
+ end
103
+
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'diff_dirs'
3
+ require 'optparse'
@@ -0,0 +1,43 @@
1
+ include GitP4Sync
2
+
3
+ options = {}
4
+
5
+ OptionParser.new do |opts|
6
+ opts.banner = "Usage: git-p4-sync [options]"
7
+
8
+ # opts.on("-b", "--branch [STRING]", String, "Git branch") do |o|
9
+ # options[:branch] = o || "master"
10
+ # end
11
+
12
+ opts.on("-g", "--git [STRING]", String, "Path to Git repository") do |o|
13
+ options[:git] = o
14
+ end
15
+
16
+ opts.on("-p", "--p4 [STRING]", String, "Path to Perforce workspace directory") do |o|
17
+ options[:p4] = o
18
+ end
19
+
20
+ opts.on("-P", "--pull", "Pull Git repository before processing") do |o|
21
+ options[:pull] = o
22
+ end
23
+
24
+ opts.on("-S", "--submit", "Submit to Perforce after processing") do |o|
25
+ options[:submit] = o
26
+ end
27
+
28
+ opts.on("-t", "--test", "Simulates a syncronisation without executing any command") do |o|
29
+ options[:simulate] = o
30
+ end
31
+
32
+ end.parse!
33
+
34
+ def error(msg)
35
+ puts msg
36
+ exit 1
37
+ end
38
+
39
+ error("Option --git required.") unless options[:git]
40
+ error("Option --p4 required.") unless options[:p4]
41
+
42
+ run options
43
+
@@ -0,0 +1,16 @@
1
+ # COWBOY CODING FTW! TESTING IS FOR WUSSIES. ;-)
2
+
3
+ require File.dirname(__FILE__) + '/test_helper'
4
+
5
+ class CowboyTest < Test::Unit::TestCase
6
+ def test_my_awesomeness
7
+ (0..12612).each {
8
+ assert :this_code_does_not_contain_bugs
9
+ }
10
+ end
11
+
12
+ def test_whos_evil
13
+ assert :git_ftw
14
+ assert :perforce_is_evil
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmer-git-p4-sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Carl Mercier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-25 00:00:00 -07:00
13
+ default_executable: git-p4-sync
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: diff_dirs
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.2
24
+ version:
25
+ description: Submit changes made to a Git repository into to Perforce
26
+ email: carl@carlmercier.com
27
+ executables:
28
+ - git-p4-sync
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - VERSION
39
+ - bin/git-p4-sync
40
+ - lib/git_p4_sync.rb
41
+ - lib/init.rb
42
+ - lib/start.rb
43
+ - test/git_p4_sync_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/cmer/git-p4-sync
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: git-p4-sync
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Submit changes made to a Git repository into to Perforce
71
+ test_files:
72
+ - test/git_p4_sync_test.rb
73
+ - test/test_helper.rb