Class: Raykit::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/git.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#start_timeObject

Returns the value of attribute start_time.



3
4
5
# File 'lib/raykit/git.rb', line 3

def start_time
  @start_time
end

Class Method Details

.add_remoteObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/raykit/git.rb', line 88

def self.add_remote
    remote = get_remote
    if(remote.length > 0)
        remote_urls_copy = remote_urls
        if(!remote_urls_copy.include?(remote))
            remote_urls_copy.insert(0,remote)
            Git::remote_urls = remote_urls_copy
        end
    end
end

.branchObject



5
6
7
8
# File 'lib/raykit/git.rb', line 5

def self.branch
    branches = `git branch`
    branches.match(/ ([\w.]+)/).captures[0]
end

.get_current_branchObject



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

def self.get_current_branch
    text=`git branch`
    if(text.include?('HEAD detached'))
        return 'master'
    end
    #puts "get_current_branch text=#{text}"

    scan=text.scan(/\*\s([\w\.-]+)/)
    #puts "scan=#{scan}"

    scan[0][0].to_s
end

.get_latest_commit(remote, branch) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/raykit/git.rb', line 52

def self.get_latest_commit(remote,branch)
    clone_dir="#{Environment::get_dev_dir('clone')}/#{get_relative_path(remote)}"
    if(!Dir.exist?(clone_dir))
        parent_dir = File.expand_path('..',clone_dir)
        if(!Dir.exist?(parent_dir))
            FileUtils.mkdir_p(parent_dir)
        end 
        Command.new("git clone #{remote} #{clone_dir}")
    end
    Dir.chdir(clone_dir) do
        Command.new('git pull')
        if(get_current_branch != branch)
            Command.new("git checkout #{branch}")
        end
        text=`git log --name-status HEAD^..HEAD`
        #puts "get_latest_commit text: #{text}"

        scan=text.scan(/commit ([\w]+)\s/)
        #puts "scan: #{scan}"

        return scan[0][0].to_s
    end  
end

.get_latest_tagObject

get the latest tag for the current directory



37
38
39
# File 'lib/raykit/git.rb', line 37

def self.get_latest_tag
    `git describe --abbrev=0 --tags`.strip
end

.get_relative_path(remote) ⇒ Object



74
75
76
# File 'lib/raykit/git.rb', line 74

def self.get_relative_path(remote)
    remote.gsub('https://','').gsub('.git','')
end

.get_remoteObject



78
79
80
81
82
83
84
85
86
# File 'lib/raykit/git.rb', line 78

def self.get_remote
    if(Dir.exist?('.git'))
        cmd = Command.new('git config --get remote.origin.url')
        #cmd.run

        return cmd.output.strip
    else
        ``
    end
end

.last_tagObject



10
11
12
# File 'lib/raykit/git.rb', line 10

def self.last_tag
    `git describe --abbrev=0 --tags`.strip
end

.list_remotesObject



115
116
117
# File 'lib/raykit/git.rb', line 115

def self.list_remotes
    remote_urls.each{|url| puts url}
end

.outstanding_commitObject



28
29
30
31
32
33
34
# File 'lib/raykit/git.rb', line 28

def self.outstanding_commit
    if(Git::user_can_commit)
        return !`git status`.include?('nothing to commit,')
    else
        return false
    end
end

.remote_urlsObject



99
100
101
102
103
104
105
106
# File 'lib/raykit/git.rb', line 99

def self.remote_urls
    filename="#{Environment::get_dev_dir('log')}/Raykit.Git.RemoteUrls.json";
    if(File.exist?(filename))
        JSON.parse(File.read(filename))
    else
        Array.new
    end
end

.remote_urls=(urls) ⇒ Object



108
109
110
111
112
113
# File 'lib/raykit/git.rb', line 108

def self.remote_urls=(urls)
    filename="#{Environment::get_dev_dir('log')}/Raykit.Git.RemoteUrls.json"
    File.open(filename,'w'){|f|
        f.write(JSON.generate(urls))
    }
end

.scan_remote_urlsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/raykit/git.rb', line 119

def self.scan_remote_urls
    remotes = Array.new
    git_dirs = Array.new
    Dir.chdir(Environment::work_dir) do
        Dir.glob('**/.git'){|git_dir|
            dir = File.expand_path('..',git_dir)
            if(dir.length > 0)
                git_dirs.insert(0,dir)
            end
            
        }
    end

    git_dirs.each{|git_dir|
        Dir.chdir(git_dir) do
            remote = get_remote
            if(remote.length > 0)
                remotes.insert(0,remote)
            end
        end
    }
    remotes
end

.user_can_commitObject



22
23
24
25
26
# File 'lib/raykit/git.rb', line 22

def self.user_can_commit
    return false if(user_name.length == 0)
    return false if(user_email.length == 0)
    return true
end

.user_emailObject



18
19
20
# File 'lib/raykit/git.rb', line 18

def self.user_email
    return `git config --get user.email`.strip
end

.user_nameObject



14
15
16
# File 'lib/raykit/git.rb', line 14

def self.user_name
    return `git config --get user.name`.strip
end