lib/raykit/git.rb



module Raykit
    class Git
        attr_accessor :start_time

        def self.branch
            branches = `git branch`
            branches.match(/ ([\w.]+)/).captures[0]
        end
   
        def self.last_tag
            `git describe --abbrev=0 --tags`.strip
        end

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

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

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

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

        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

        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}")
                puts "cmd.command=#{cmd.command}"
                puts "cmd.exitstatus=#{cmd.exitstatus}"
                puts "cmd.output=#{cmd.output}"
                puts "cmd.error=#{cmd.error}"
            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

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

        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

        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

        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

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

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

        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
    end
end