git_switch 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cacd2da7a06f2667c6c79db5e1af52535b3f60a070251197311f130e94f792c
4
- data.tar.gz: '0029eac18d9453fc078fe5d41060444521652eb867f6299067355a582192d213'
3
+ metadata.gz: 349519b9a5eaf832588e1093490d3f9caf9fb90ce2b3c922b2dde9563def62c7
4
+ data.tar.gz: dedebe431d491af9a10a9a3c1b72d24f872ecb9146b17c220f6cf6587217851b
5
5
  SHA512:
6
- metadata.gz: dc87d4c8c806552627b8b620b4ac7a2c4dda1e973d5828998ebd75207e27710bd09fa6e25833ca8481dc74b037b1a1167e4180ffa26079124bef0a565436853c
7
- data.tar.gz: 60e8cfdbc60e83348b7d9ca21fa0a76c04940bf5d8a68e4153555c6e36e244dc14c83ef7e9e351d3a01110803790d6164161f128eeefe4036d6cfe4098a7db9f
6
+ metadata.gz: b8d5bca901a1c226bff6dafab5d6585c225158f6baf504f7f362d5e7e2bb0f886dcdf4fd895200bcec722f20826e81357bb9cc79606f05fee0c70336d7e65b95
7
+ data.tar.gz: 8749517c89f0b9a26023b777208676a3c42173010bc19af1818bc49eaac3722790eb17a31072177f5a72320b4161efcb78673ae8e0850b09af15b1767ed3d2b5
data/bin/git-switch CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require_relative '../lib/git_switch'
3
3
 
4
- GitSwitch::Switcher.new(ARGV).set!
4
+ GitSwitch::Switcher.new(ARGV).run
@@ -0,0 +1,44 @@
1
+ module GitSwitch
2
+ class GitHelper
3
+ def self.git_repo?
4
+ !find_git_repo.nil?
5
+ end
6
+
7
+ private
8
+ # Copied from https://gist.github.com/nickmccurdy/8466084
9
+ # Script: Find the current git repository
10
+ # Based on https://github.com/mojombo/grit/pull/178 by https://github.com/derricks
11
+
12
+ # Returns true if the given path represents a root directory (/ or C:/)
13
+ def self.root_directory?(file_path)
14
+ # Implementation inspired by http://stackoverflow.com/a/4969416:
15
+ # Does file + ".." resolve to the same directory as file_path?
16
+ File.directory?(file_path) &&
17
+ File.expand_path(file_path) == File.expand_path(File.join(file_path, '..'))
18
+ end
19
+
20
+ # Returns the git root directory given a path inside the repo. Returns nil if
21
+ # the path is not in a git repo.
22
+ def self.find_git_repo(start_path = '.')
23
+ raise NoSuchPathError unless File.exists?(start_path)
24
+
25
+ current_path = File.expand_path(start_path)
26
+
27
+ # for clarity: set to an explicit nil and then just return whatever
28
+ # the current value of this variable is (nil or otherwise)
29
+ return_path = nil
30
+
31
+ until root_directory?(current_path)
32
+ if File.exists?(File.join(current_path, '.git'))
33
+ # done
34
+ return_path = current_path
35
+ break
36
+ else
37
+ # go up a directory and try again
38
+ current_path = File.dirname(current_path)
39
+ end
40
+ end
41
+ return_path
42
+ end
43
+ end
44
+ end
@@ -1,43 +1,91 @@
1
- # require 'version'
2
1
  require 'yaml'
3
2
  require_relative './version'
4
3
 
5
4
  module GitSwitch
6
5
  class Switcher
7
- attr_reader :config, :profile, :valid, :global
8
- # Your code goes here...
6
+ attr_reader :args, :config, :profile, :global, :list
7
+
9
8
  def initialize(args)
10
9
  raise ArgumentError unless args.is_a? Array
11
- @config = YAML.load_file(File.expand_path('~/.gitswitch'))
10
+ @args = args
11
+ @config = load_config
12
12
  @global = check_global(args)
13
13
  @profile = get_profile(args)
14
- @valid = valid_args?(args)
14
+ @list = check_list(args)
15
+ end
16
+
17
+ def run
18
+ list ? print_list : set!
19
+ end
20
+
21
+ def load_config
22
+ YAML.load_file(File.expand_path('~/.gitswitch')) || {}
15
23
  end
16
24
 
17
25
  def check_global(args)
18
26
  (args.include? '-g') || (args.include? '--global')
19
27
  end
20
28
 
29
+ def check_list(args)
30
+ (args.include? '-l') || (args.include? '--list')
31
+ end
32
+
21
33
  def get_profile(args)
22
34
  # TODO: RCR - Verify profile exists in config file
23
35
  # TODO: RCR - Handle missing or empty config file
24
36
  args.detect {|a| !a.start_with? '-'}
25
37
  end
26
38
 
27
- def valid_args?(args)
28
- no_flags?(args) || one_flag?(args)
39
+ def valid_args?
40
+ if check_list(args) && args.count > 1
41
+ puts "Invalid args"
42
+ return false
43
+ elsif no_flags?(args) || one_flag?(args)
44
+ return true
45
+ else
46
+ puts "Invalid args"
47
+ return false
48
+ end
49
+ end
50
+
51
+ def valid_profile?
52
+ if config.has_key?(profile)
53
+ return true
54
+ else
55
+ puts "Profile '#{profile}' not found!"
56
+ return false
57
+ end
58
+ end
59
+
60
+ def git_repo?
61
+ if GitHelper.git_repo?
62
+ return true
63
+ else
64
+ if global
65
+ return true
66
+ else
67
+ puts "Not a git repo. Please run from a git repo or run with `-g` to update global settings."
68
+ return false
69
+ end
70
+ end
29
71
  end
30
72
 
31
73
  def no_flags?(args)
32
- args.length == 1 && args.count {|a| a.start_with? '-'} == 0
74
+ args.length == 1 && flag_count(args) == 0
33
75
  end
34
76
 
35
77
  def one_flag?(args)
36
- args.length == 2 && args.count {|a| a.start_with?('-')} == 1
78
+ args.length == 2 && flag_count(args) == 1
79
+ end
80
+
81
+ def flag_count(args)
82
+ args.count {|a| a.start_with? '-'}
37
83
  end
38
84
 
39
85
  def set!
40
- return unless valid
86
+ return unless valid_args? && valid_profile?
87
+ return unless git_repo?
88
+
41
89
  flag = global ? '--global' : ''
42
90
 
43
91
  puts "\nGit Config:"
@@ -52,6 +100,10 @@ module GitSwitch
52
100
  puts `ssh-add -l`
53
101
  end
54
102
 
103
+ def print_list
104
+ puts config.keys
105
+ end
106
+
55
107
  private
56
108
 
57
109
  def name
@@ -1,3 +1,3 @@
1
1
  module GitSwitch
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/git_switch.rb CHANGED
@@ -1 +1,2 @@
1
1
  require_relative 'git_switch/switcher'
2
+ require_relative 'git_switch/git_helper'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randall Reed, Jr.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-23 00:00:00.000000000 Z
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - bin/git-switch
92
92
  - lib/git_switch.rb
93
+ - lib/git_switch/git_helper.rb
93
94
  - lib/git_switch/switcher.rb
94
95
  - lib/git_switch/version.rb
95
96
  homepage: https://www.github.com/randallreedjr/git_switch
@@ -112,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  version: '0'
113
114
  requirements: []
114
115
  rubyforge_project:
115
- rubygems_version: 2.7.7
116
+ rubygems_version: 2.7.6
116
117
  signing_key:
117
118
  specification_version: 4
118
119
  summary: Switch between git profiles