lib/raykit/git/repository.rb



module Raykit
    module Git
        # Functionality to manage a remote git repository

        class Repository
            # The url of the remote repository

            attr_accessor :url
            attr_accessor :clone_directory
            attr_accessor :work_directory

            def initialize(url)
                @url=url
                @clone_directory = Raykit::Git::Directory.new(get_dev_dir('clone'))
                @work_directory = Raykit::Git::Directory.new(get_dev_dir('work'))
            end

            def to_json
                JSON.generate({"url" => @url})
            end

            def self.parse(json)
                hash=JSON.parse(json)
                repo=Repository.new(hash["url"])
                repo
            end

            # The relative path is a valid local path name derived from the url

            def relative_path
                @url.gsub('https://','').gsub('.git','')
            end

            def get_dev_dir(dir)
                dev_dir=Environment::get_dev_dir(dir)
                return "#{dev_dir}/#{relative_path}"
            end

            # Clone the repository to a specific directory

            def clone(directory,depth=0)
                if(depth == 0)
                    Raykit::run("git clone #{@url} #{directory}") 
                else
                    Raykit::run("git clone #{@url} #{directory} --depth #{depth}")
                end
            end

            # The branches for the git repository

            def branches
                results = Array.new
                update_local_clone_directory
                if(Dir.exist?(local_clone_directory))
                    Dir.chdir(local_clone_directory) do
                        `git branch`.split('\n').each{|line|
                            branch = line.gsub('*','').strip
                            results.insert(-1,branch) if(branch.length > 0)
                        }
                    end
                end
                results
            end

            # The latest commit id for a branch of the repostiory

            def latest_commit(branch)
                if(checkout_local_clone_directory_branch(branch))
                    text=`git log -n 1`
                    scan=text.scan(/commit ([\w]+)\s/)
                    return scan[0][0].to_s 
                end
                ''
            end

            # The latest tag for a branch of the repository

            def latest_tag(branch)
                if(checkout_local_clone_directory_branch(branch))
                    return `git describe --abbrev=0`.strip
                end
                ''
            end

            private def local_clone_directory
                clone_dir="#{Environment::get_dev_dir('clone')}/#{relative_path}"
            end

            private def update_local_clone_directory
                if(Dir.exist?(local_clone_directory))
                    Dir.chdir(local_clone_directory) do
                        `git pull`
                    end
                else
                    Raykit::run("git clone #{@url} #{local_clone_directory}")
                end
            end

            private def checkout_local_clone_directory_branch(branch)
                update_local_clone_directory
                if(Dir.exist?(local_clone_directory))
                    Dir.chdir(local_clone_directory) do
                        check=`git branch`
                        if(!check.include?("* #{branch}"))
                            `git checkout #{branch}`
                        end
                        check=`git branch`
                        return check.include?("* #{branch}")
                    end
                end
                false
            end
        end
    end
end