Class: YJCocoa::GitCheckout

Inherits:
Git
  • Object
show all
Defined in:
lib/yjcocoa/git/git_checkout.rb

Constant Summary

Constants inherited from Command

Command::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Git

#gitExist?

Methods inherited from Command

#askWithAnswers

Constructor Details

#initialize(argv) ⇒ GitCheckout

初始化



29
30
31
32
# File 'lib/yjcocoa/git/git_checkout.rb', line 29

def initialize(argv)
    super
    self.branch = argv.option('branch')
end

Instance Attribute Details

#branchObject

property



25
26
27
# File 'lib/yjcocoa/git/git_checkout.rb', line 25

def branch
  @branch
end

#gitsObject

Returns the value of attribute gits.



26
27
28
# File 'lib/yjcocoa/git/git_checkout.rb', line 26

def gits
  @gits
end

Class Method Details

.optionsObject



20
21
22
# File 'lib/yjcocoa/git/git_checkout.rb', line 20

def self.options
    [['--branch', '切换 branch'],]
end

Instance Method Details

#buildGitPathsObject



52
53
54
55
56
57
# File 'lib/yjcocoa/git/git_checkout.rb', line 52

def buildGitPaths
    self.gits = Dir["**/.git"]
    self.gits.map! { |path|
        File.dirname(path)
    }
end

#gitCheckout(path = ".") ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/yjcocoa/git/git_checkout.rb', line 59

def gitCheckout(path=".")
    Dir.chdir(path) {
        puts "YJCocoa git checkout #{path}/.git".green
        localChanges = !(`git stash` =~ /No local changes to save/)
        system("git checkout -b #{self.branch}") unless system("git checkout #{self.branch}")
        `git stash pop` if localChanges
        puts
    }
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yjcocoa/git/git_checkout.rb', line 39

def run
    self.buildGitPaths
    if self.gits.empty?
        if self.gitExist?
            self.gitCheckout
        end
    else
        self.gits.each { |path|
            self.gitCheckout(path)
        }
    end
end

#validate!Object

businrss



35
36
37
# File 'lib/yjcocoa/git/git_checkout.rb', line 35

def validate!
    self.banner! unless self.branch
end