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



75
76
77
78
79
80
81
82
83
84
# File 'lib/raykit/git.rb', line 75

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



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

def self.get_current_branch
    `git branch`.scan(/\*\s([\w\.-]+)/)[0][0].to_s
end

.get_latest_commit(remote, branch) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/raykit/git.rb', line 40

def self.get_latest_commit(remote,branch)
    clone_dir="#{Environment::clone_dir}/#{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 
        cmd = Command.new("git clone #{remote} #{clone_dir}")
        #cmd.run
    else
        Dir.chdir(clone_dir) do
            cmd = Command.new('git pull')
            #cmd.run
            if(get_current_branch != branch)
                cmd = Command.new("git checkout #{branch}")
            end
            return `git log --name-status HEAD^..HEAD`.scan(/commit ([\w]+)\s/)[0][0].to_s
        end 
    end   
end

.get_relative_path(remote) ⇒ Object



61
62
63
# File 'lib/raykit/git.rb', line 61

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

.get_remoteObject



65
66
67
68
69
70
71
72
73
# File 'lib/raykit/git.rb', line 65

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



102
103
104
# File 'lib/raykit/git.rb', line 102

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



86
87
88
89
90
91
92
93
# File 'lib/raykit/git.rb', line 86

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

.remote_urls=(urls) ⇒ Object



95
96
97
98
99
100
# File 'lib/raykit/git.rb', line 95

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

.scan_remote_urlsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/raykit/git.rb', line 106

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