git_switch_branch 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +1 -0
- data/bin/git-switch-branch +3 -0
- data/bin/gsb +12 -0
- data/git_switch_branch.gemspec +24 -0
- data/lib/git_switch_branch/changes.rb +72 -0
- data/lib/git_switch_branch/version.rb +3 -0
- data/lib/git_switch_branch.rb +93 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Daniel Eriksson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Git Switch Branch
|
2
|
+
|
3
|
+
This gem simplifies switching between git branches without losing uncommitted changes. Its meant as an alternative for "git checkout" and "git stash".
|
4
|
+
|
5
|
+
1. If the branch you are switching from contains uncommitted changes they will be automatically
|
6
|
+
saved to the folder ~/.git-switch-branch/REPO_NAME/BRANCH_NAME.
|
7
|
+
|
8
|
+
2. If the branch you are switching to contains saved changes they will be automatically restored.
|
9
|
+
|
10
|
+
3. It makes it easier to sync uncommitted changes between computers.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'git_switch_branch'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install git_switch_branch
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Basic usage is just:
|
29
|
+
|
30
|
+
$ gsb
|
31
|
+
|
32
|
+
This will give you a list of all branches to choose from.
|
33
|
+
|
34
|
+
$ 1. master
|
35
|
+
$ 2. 123-nasty-bug
|
36
|
+
|
37
|
+
If you know the name of the branch or part of the name you can do:
|
38
|
+
|
39
|
+
$ gsb 123
|
40
|
+
|
41
|
+
## Syncing between computers
|
42
|
+
|
43
|
+
If you are using Dropbox you can easily sync your uncommitted changes between all your computers.
|
44
|
+
|
45
|
+
mkdir -p ~/.git-switch-branch
|
46
|
+
ln -s ~/.git-switch-branch ~/Dropbox/.git-switch-branch
|
47
|
+
|
48
|
+
## Known Bugs
|
49
|
+
|
50
|
+
* Empty files are not restored.
|
51
|
+
|
52
|
+
## Todo
|
53
|
+
|
54
|
+
* Make it possible to find and automatically track remote branches.
|
55
|
+
* Use the Git gem more (instead of the git shell command).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
64
|
+
|
65
|
+
## Supported platforms
|
66
|
+
|
67
|
+
git_switch_branch has been tested with:
|
68
|
+
|
69
|
+
* OS X Lion
|
70
|
+
* Ruby 1.8.7, 1.9.3
|
71
|
+
* Git 1.7.7, 1.7.8, 1.8.0
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/gsb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
THIS_FILE = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
|
4
|
+
require File.expand_path('../lib/git_switch_branch', File.dirname(THIS_FILE))
|
5
|
+
|
6
|
+
if ARGV[0] == '--help' || ARGV.size > 1
|
7
|
+
GitSwitchBranch.show_usage
|
8
|
+
elsif ARGV[0] == '--version' || ARGV[0] == '-v'
|
9
|
+
GitSwitchBranch.show_version
|
10
|
+
else
|
11
|
+
GitSwitchBranch.find_and_checkout_branch(ARGV[0])
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'git_switch_branch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "git_switch_branch"
|
8
|
+
gem.version = GitSwitchBranch::VERSION
|
9
|
+
gem.authors = ["Daniel Eriksson"]
|
10
|
+
gem.email = ["daniel.eriksson@bukowskis.com"]
|
11
|
+
gem.description = %q(Easy git branch switching)
|
12
|
+
gem.summary = %q(Simplifies switching between branches without losing uncommitted changes.)
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "git", ">= 1.2.5"
|
21
|
+
gem.add_dependency "colored", ">= 1.2"
|
22
|
+
gem.add_dependency "highline", ">= 1.6.15"
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'git'
|
2
|
+
require 'colored'
|
3
|
+
|
4
|
+
module GitSwitchBranch
|
5
|
+
|
6
|
+
def self.save_changes
|
7
|
+
if saved_changes?
|
8
|
+
puts "This branch already has saved changes.".red
|
9
|
+
elsif uncommitted_changes?
|
10
|
+
stash_uncommitted_changes
|
11
|
+
save_stash_to_patch
|
12
|
+
else
|
13
|
+
puts "No uncommitted changes to save.".green
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.restore_changes
|
18
|
+
return unless saved_changes?
|
19
|
+
puts "Restoring changes...".green
|
20
|
+
if apply_patch
|
21
|
+
File.delete(@patch_path)
|
22
|
+
@git.reset
|
23
|
+
else
|
24
|
+
puts "Failed to restore saved changes!"
|
25
|
+
puts "Please check #{@patch_path}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.check_changes
|
30
|
+
if saved_changes?
|
31
|
+
puts "This branch has saved changes.".green
|
32
|
+
else
|
33
|
+
puts "This branch has no saved changes.".green
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.show_changes
|
38
|
+
if saved_changes?
|
39
|
+
system "cat #{@patch_path} | less"
|
40
|
+
else
|
41
|
+
puts "No saved changes found for this branch at #{@patch_path})"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def self.stash_uncommitted_changes
|
48
|
+
puts "Saving uncommitted changes...".green
|
49
|
+
system "mkdir -p #{@repo_changes_path}"
|
50
|
+
@git.add('.')
|
51
|
+
system "git stash save"
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.save_stash_to_patch
|
55
|
+
puts "Saving stash to #{@patch_path}"
|
56
|
+
system "git stash show -p > #{@patch_path}"
|
57
|
+
system "git stash drop"
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.saved_changes?
|
61
|
+
File.exists?(@patch_path)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.uncommitted_changes?
|
65
|
+
`git status -s` != ''
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.apply_patch
|
69
|
+
system "patch -p1 < #{@patch_path}"
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'colored'
|
3
|
+
require 'highline/import'
|
4
|
+
|
5
|
+
gsb_app_root = File.expand_path(File.dirname(__FILE__) + '/..')
|
6
|
+
$LOAD_PATH.unshift(gsb_app_root + '/lib')
|
7
|
+
|
8
|
+
require 'git_switch_branch/version'
|
9
|
+
require 'git_switch_branch/changes'
|
10
|
+
|
11
|
+
module GitSwitchBranch
|
12
|
+
|
13
|
+
GSB_APP_DATA_DIR = '.git-switch-branch'
|
14
|
+
|
15
|
+
attr_accessor :git, :repo_changes_path, :patch_path
|
16
|
+
|
17
|
+
def self.initialize
|
18
|
+
@git = Git.open(Dir.pwd)
|
19
|
+
current_dir = File.basename(Dir.getwd)
|
20
|
+
@repo_changes_path = File.join(ENV['HOME'], GSB_APP_DATA_DIR, current_dir)
|
21
|
+
@patch_path = File.join(@repo_changes_path, @git.current_branch)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.find_and_checkout_branch(string)
|
25
|
+
initialize
|
26
|
+
checkout select_branch(string)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.show_version
|
30
|
+
puts GitSwitchBranch::VERSION
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.show_usage
|
34
|
+
puts "git_switch_branch #{GitSwitchBranch::VERSION}\n\n"
|
35
|
+
puts "Usage: gsb <branchname_or_part_of_branchname>"
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def self.select_branch(string)
|
41
|
+
matching_branches = find_matching_branches(string)
|
42
|
+
if matching_branches.count > 0
|
43
|
+
choose_branch(matching_branches)
|
44
|
+
else
|
45
|
+
choose_branch(local_branches)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.find_matching_branches(string)
|
50
|
+
extract_branch_names(`git branch | grep -v '*' | grep '#{string}'`)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.checkout(branch)
|
54
|
+
abort "Already on #{branch}" if @git.current_branch == branch
|
55
|
+
save_changes
|
56
|
+
puts "Checking out #{branch}...".green
|
57
|
+
puts `git checkout #{branch}`
|
58
|
+
restore_changes
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.local_branches
|
62
|
+
extract_branch_names(`git branch`)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.extract_branch_names(output)
|
66
|
+
output.split("\n").map do |line|
|
67
|
+
line.strip.sub('*','').strip.sub('remotes/','').sub('origin/','')
|
68
|
+
end.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.choose_branch(branches)
|
72
|
+
if branches.count == 0
|
73
|
+
abort "No branches found."
|
74
|
+
end
|
75
|
+
|
76
|
+
if branches.count == 1
|
77
|
+
puts "Matching branch found: #{branches.first}".green
|
78
|
+
return branches.first
|
79
|
+
end
|
80
|
+
|
81
|
+
puts "Branches found:".magenta.underline
|
82
|
+
choose do |menu|
|
83
|
+
menu.prompt = "\nChoose a branch to switch to: ".cyan
|
84
|
+
branches.each do |branch|
|
85
|
+
menu.choice(branch) { return branch }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: git_switch_branch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Eriksson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: git
|
16
|
+
requirement: &70155828829220 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.2.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70155828829220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: colored
|
27
|
+
requirement: &70155828828760 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.2'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70155828828760
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: highline
|
38
|
+
requirement: &70155828828300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.6.15
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70155828828300
|
47
|
+
description: Easy git branch switching
|
48
|
+
email:
|
49
|
+
- daniel.eriksson@bukowskis.com
|
50
|
+
executables:
|
51
|
+
- git-switch-branch
|
52
|
+
- gsb
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- .gitignore
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE.txt
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- bin/git-switch-branch
|
62
|
+
- bin/gsb
|
63
|
+
- git_switch_branch.gemspec
|
64
|
+
- lib/git_switch_branch.rb
|
65
|
+
- lib/git_switch_branch/changes.rb
|
66
|
+
- lib/git_switch_branch/version.rb
|
67
|
+
homepage: ''
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.11
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Simplifies switching between branches without losing uncommitted changes.
|
91
|
+
test_files: []
|
92
|
+
has_rdoc:
|