git-inside-branch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in git-inside-branch.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2011, Guillermo Álvarez <guillermo@cientifico.net>
2
+ All rights reserved.
3
+
4
+ "THE BEER-WARE LICENSE" (Revision 42):
5
+ <guillermo@cientifico.net> wrote this file. As long as you retain this notice you
6
+ can do whatever you want with this stuff. If we meet some day, and you think
7
+ this stuff is worth it, you can buy me a beer in return Guillermo Álvarez.
8
+
9
+
data/README ADDED
@@ -0,0 +1,57 @@
1
+
2
+
3
+ git-inside-branch
4
+ =================
5
+
6
+ git-inside-branch allows you to inspect and modify another branch in a temporarily directory without affecting current working tree.
7
+
8
+
9
+ Usage
10
+ =====
11
+
12
+ From command line
13
+ -----------------
14
+
15
+ git inside-branch doc_branch bash
16
+
17
+ This will checkout doc_branch and run bash. Then you edit your docs with vim or emacs, commit the results and exit bash.
18
+
19
+ git inside-branch gh_pages "cp $HOME/projects/my_proj/rdoc . && git add . && git commit -am 'Doc updates' && git push"
20
+
21
+
22
+ From ruby
23
+ ---------
24
+
25
+ Git::Inside::Branch.tempory_checkout("gh_pages") do
26
+ `cp $HOME/projects/my_lib/rdoc .`
27
+ `git commit -a -m 'Doc updates' && git push`
28
+ end
29
+
30
+ I think is easy.
31
+
32
+ Documentation
33
+ =============
34
+
35
+ * Git::Inside::Branch.tempory_checkout( *branch_name*, *&block* )
36
+
37
+ Run the code passed in the block in another temporarily checkouted directory from the branch __branch_name__
38
+
39
+ AUTHOR
40
+ ======
41
+
42
+ * Guillermo Álvarez <guillermo@cientifico.net>
43
+
44
+
45
+ LICENSE
46
+ =======
47
+
48
+
49
+ Copyright (c) 2011, Guillermo Álvarez <guillermo@cientifico.net>
50
+ All rights reserved.
51
+
52
+ "THE BEER-WARE LICENSE" (Revision 42):
53
+ <guillermo@cientifico.net> wrote this file. As long as you retain this notice you
54
+ can do whatever you want with this stuff. If we meet some day, and you think
55
+ this stuff is worth it, you can buy me a beer in return Guillermo Álvarez.
56
+
57
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+ require 'rake/testtask'
5
+
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ Rake::TestTask.new do |t|
10
+ t.libs << "test"
11
+ t.test_files = FileList['test/*_test.rb']
12
+ t.verbose = true
13
+ end
14
+
15
+
16
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git-inside-branch'
4
+
5
+ args = ARGV.dup
6
+ if args.size > 0 || args.include?("-h") || args.include?("--help")
7
+
8
+ Git::Inside::Branch.tempory_checkout(args.shift) do
9
+ args = %(bash) if args.empty?
10
+ pid = fork do
11
+ exec(*args)
12
+ end
13
+ Process.wait(pid)
14
+ end
15
+
16
+ else
17
+
18
+ $stderr.puts "usage: #{$0} branch_name [command]\n\n"
19
+ $stderr.puts " If no command given, bash will be used."
20
+ $stderr.puts "WARNING: Do work with both checkouts at the same time."
21
+
22
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "git-inside-branch/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "git-inside-branch"
7
+ s.version = Git::Inside::Branch::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Guillermo Álvarez"]
10
+ s.email = ["guillermo@cientifico.net"]
11
+ s.homepage = ""
12
+ s.summary = %q{git-inside-branch let you inspect and modify other branches without affecting current directory}
13
+ s.description = %q{It set GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE and checkout temporarily in a temp directory}
14
+
15
+ s.rubyforge_project = "git-inside-branch"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "bundler"
24
+ end
@@ -0,0 +1,91 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ module Git
5
+ module Inside
6
+ class Branch
7
+
8
+ def self.tempory_checkout(branch)
9
+ dir = find_git_dir
10
+ return dir unless dir || !block_given?
11
+
12
+ new(dir,branch).start do
13
+ yield if block_given?
14
+ end
15
+ end
16
+
17
+ def initialize(git_dir, branch)
18
+ @branch, @git_dir = branch, git_dir
19
+ end
20
+
21
+ def start
22
+ prepare do |temp_dir|
23
+ Dir.chdir(temp_dir) do
24
+ `git co #{@branch} 2>&1`
25
+ yield
26
+ end
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def self.find_git_dir
33
+ dir = File.expand_path Dir.pwd
34
+
35
+ found = false
36
+ dir = Dir.basename(dir) until(found = File.directory?(File.join(dir,'.git')) || dir == "/")
37
+
38
+ return found ? File.join(dir,'.git') : false
39
+ end
40
+
41
+ def prepare
42
+ in_temp_dir do
43
+ preserve_head do
44
+ set_git_env do |code_dir|
45
+ yield code_dir
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def in_temp_dir()
52
+ @temp_dir = Dir.mktmpdir(@branch)
53
+ yield
54
+ ensure
55
+ FileUtils.remove_entry_secure @temp_dir if File.directory? @temp_dir
56
+ end
57
+
58
+ def preserve_head(*args)
59
+ old_HEAD_ref = `git symbolic-ref HEAD 2>&1`.strip
60
+ `git symbolic-ref HEAD refs/heads/#{@branch} 2>&1`
61
+
62
+ yield *args
63
+
64
+ ensure
65
+ `git symbolic-ref HEAD #{old_HEAD_ref} 2>&1` unless old_HEAD_ref.empty?
66
+ end
67
+
68
+ def set_git_env
69
+ code_dir = File.join(@temp_dir, "code")
70
+ old_env = ENV.dup
71
+ Dir.mkdir(code_dir)
72
+
73
+ ENV["GIT_INDEX_FILE"] = File.join(@temp_dir,'index')
74
+ ENV["GIT_WORK_TREE"] = code_dir
75
+ ENV["GIT_DIR"] = @git_dir
76
+ yield code_dir
77
+ ensure
78
+ ENV.delete "GIT_INDEX_FILE"
79
+ ENV.delete "GIT_WORK_TREE"
80
+ ENV.delete "GIT_DIR"
81
+ end
82
+
83
+ def in_git_dir?
84
+ return false if `git status --porcelain 2>&1 2>&1` =~ /^fatal.*/
85
+ true
86
+ end
87
+
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,7 @@
1
+ module Git
2
+ module Inside
3
+ class Branch
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
data/task.rb ADDED
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+
6
+ module Task
7
+ extend self
8
+
9
+ def start
10
+ look_for_git_dir
11
+
12
+ prepare_git do |temp_dir|
13
+ `git clean -fdx && git co taskwarrior`
14
+ begin
15
+ old_home = ENV["HOME"]
16
+ ENV["HOME"] = temp_dir
17
+ #system("task", *ARGV)
18
+ system( *ARGV)
19
+ ensure
20
+ ENV["HOME"] = old_home
21
+ end
22
+
23
+ `find #{temp_dir} | xargs git add `
24
+ `git commit -m "Changes"`
25
+ end
26
+
27
+ # create_temp_dir
28
+ # import_files_from_git
29
+ # call_taskwarrior
30
+ # export_files_to_git
31
+ # push_changes
32
+ end
33
+
34
+ protected
35
+
36
+ def prepare_git
37
+ temp_dir = Dir.mktmpdir("git-task")
38
+ old_HEAD_ref = `git symbolic-ref HEAD`.strip
39
+ `git symbolic-ref HEAD refs/heads/taskwarrior`
40
+ code_dir = File.join(temp_dir, "code")
41
+
42
+ Dir.mkdir(File.join(code_dir))
43
+
44
+ ENV["GIT_INDEX_FILE"] = File.join(temp_dir,'index')
45
+ ENV["GIT_WORK_TREE"] = code_dir
46
+ yield code_dir
47
+ ensure
48
+ `git symbolic-ref HEAD #{old_HEAD_ref}`
49
+ FileUtils.remove_entry_secure temp_dir if File.directory? temp_dir
50
+ end
51
+
52
+ def look_for_git_dir
53
+ die("You are not in a git tracked directory!") if `git status --porcelain 2>&1` =~ /^fatal.*/
54
+ end
55
+
56
+
57
+ def die(msg)
58
+ $stderr.puts(msg)
59
+ exit -1
60
+ end
61
+ end
62
+
63
+
64
+ Task.start
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+ #
3
+
4
+ require "test_helper"
5
+
6
+ class TestGitInsideBranch < Test::Unit::TestCase
7
+ include Git::Inside::Branch::Assertions
8
+
9
+ def test_env_setup_correctly
10
+ assert_git_doesnt_change do
11
+ assert_match /Guillermo.*initial import/m, `git log`
12
+ end
13
+ end
14
+
15
+ def test_env_doesnt_change_on_non_existing_branches
16
+ assert_git_doesnt_change do
17
+ Git::Inside::Branch.tempory_checkout("pepe")
18
+ end
19
+ end
20
+
21
+
22
+ def test_env_doesnt_change_on_existing_branch
23
+ assert_git_doesnt_change do
24
+ Git::Inside::Branch.tempory_checkout("other_branch")
25
+ end
26
+ end
27
+
28
+ def test_env_doesnt_change_when_doing_operations
29
+ assert_git_doesnt_change do
30
+ Git::Inside::Branch.tempory_checkout("other_branch") do
31
+ assert_match /clean/, `git status`
32
+ assert_match /other_branch/, `git branch -a | grep '*'`
33
+ assert_match /other_branch/i, Dir.pwd
34
+ `echo ¿Qué tal? > Hello2.txt`
35
+ `echo adios >> Hello.txt`
36
+ `git add .`
37
+ `git commit -m "New cambios"`
38
+ end
39
+ assert_no_match /other_branch/i, Dir.pwd
40
+ end
41
+ end
42
+
43
+ def test_correct_dir
44
+ assert_git_doesnt_change do
45
+
46
+ Git::Inside::Branch.tempory_checkout("other_branch") do
47
+ assert_match /.*\/code\z/, Dir.pwd
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ def test_changes_are_persistent
55
+ assert_git_doesnt_change do
56
+ Git::Inside::Branch.tempory_checkout("other_branch") do
57
+ `echo Hola2 >> Hello.txt`
58
+ `git add *`
59
+ `git commit -a -m "que pasa tron" `
60
+ assert_match /que pasa tron/, `git log`
61
+ end
62
+ Git::Inside::Branch.tempory_checkout("other_branch") do
63
+ assert_match /que pasa tron/, `git log`
64
+ end
65
+
66
+ end
67
+ end
68
+
69
+ def test_a_new_branch_is_created_correctly
70
+ assert_git_doesnt_change do
71
+ Git::Inside::Branch.tempory_checkout("new_branch") do
72
+ `echo Hola2 >> Hello.txt`
73
+ `git add *`
74
+ `git commit -a -m "que pasa tron" `
75
+ assert_match /que pasa tron/, `git log`
76
+ end
77
+ Git::Inside::Branch.tempory_checkout("new_branch") do
78
+ assert_match /que pasa tron/, `git log`
79
+ end
80
+
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ require 'test/unit'
3
+ require 'git-inside-branch'
4
+ require 'tmpdir'
5
+ require 'fileutils'
6
+
7
+
8
+ module Git::Inside::Branch::Assertions
9
+
10
+ def assert_git_doesnt_change
11
+ in_new_repo do
12
+ info = get_git_info()
13
+ yield
14
+ assert_equal get_git_info, info, "The environment changed and it should not."
15
+ end
16
+ end
17
+
18
+ def get_git_info
19
+ info = {}
20
+ info["HEAD"] = `git symbolic-ref HEAD 2>&1`.strip
21
+ info["status"] = `git status --porcelain 2>&1`.strip
22
+ info["branch"] = `git branch -a | grep '*' 2>&1`.strip
23
+ info["env"] = `env 2>&1`.strip
24
+ info
25
+ end
26
+
27
+
28
+ def in_new_repo
29
+ dir = new_repo
30
+ Dir.chdir(dir) do
31
+ yield dir
32
+ end
33
+ ensure
34
+ remove_repo dir
35
+ end
36
+
37
+ def new_repo
38
+ dir = Dir.mktmpdir("git-inside-branch_test-repo")
39
+ Dir.chdir(dir) do
40
+ `git init 2>&1`
41
+ `echo Hola > Hello.txt 2>&1`
42
+ `git add Hello.txt 2>&1`
43
+ `git commit -a -m 'initial import' --author 'Guillermo Álvarez <guillermo@cientifico.net>' 2>&1`
44
+ `git checkout -b other_branch 2>&1`
45
+ `git checkout master 2>&1`
46
+ assert `git log 2>&1`.include?("initial import"), "The repository could not be created correctly"
47
+ end
48
+ return dir
49
+ end
50
+
51
+ def remove_repo(repo)
52
+ FileUtils.remove_entry_secure repo if File.directory? repo
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-inside-branch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guillermo Álvarez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &2152690840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152690840
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &2152689700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152689700
36
+ description: It set GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE and checkout temporarily
37
+ in a temp directory
38
+ email:
39
+ - guillermo@cientifico.net
40
+ executables:
41
+ - git-inside-branch
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - LICENSE
48
+ - README
49
+ - Rakefile
50
+ - bin/git-inside-branch
51
+ - git-inside-branch.gemspec
52
+ - lib/git-inside-branch.rb
53
+ - lib/git-inside-branch/version.rb
54
+ - task.rb
55
+ - test/git-inside-branch_test.rb
56
+ - test/test_helper.rb
57
+ homepage: ''
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: 965474215564295548
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 965474215564295548
81
+ requirements: []
82
+ rubyforge_project: git-inside-branch
83
+ rubygems_version: 1.8.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: git-inside-branch let you inspect and modify other branches without affecting
87
+ current directory
88
+ test_files:
89
+ - test/git-inside-branch_test.rb
90
+ - test/test_helper.rb