lib/raykit/git/directory.rb



module Raykit
    module Git
        # Functionality to manage a local clone of a git repository

        class Directory
            # The directory name of the local repository clone

            attr_accessor :directory
            @repository

            def initialize(directory)
                @directory=directory
            end

            def outstanding_commit?
                Dir.chdir(directory) do
                    if(user_can_commit)
                        return !`git status`.include?('nothing to commit,')
                    else
                        return false
                    end
                end
            end

            def pull
                Dir.chdir(directory) do
                    diff=`git diff`.strip
                    status=`git status`.strip
                    if(diff.length == 0 && status.include?('nothing to commit'))
                        PROJECT.run('git pull')
                    end
                end
            end

            def rake(task)
                debug=false
                sub_dir='work'
                sub_dir='make' if(@directory.include?('/make/'))
                rake_log=repository.get_dev_dir('log') + "/#{sub_dir}.rake.#{task}.json"

                puts "log filename #{rake_log}" if(debug)
                if(File.exist?(rake_log))
                    if(File.mtime(rake_log) > last_modified_time)
                        puts "using logged data" if(debug)
                        cmd=Raykit::Command::parse(File.read(rake_log))
                        cmd.summary
                        return
                    end
                end

                Dir.chdir(@directory) do
                    if(File.exist?('rakefile.rb'))
                        cmd = Raykit::Command.new("rake #{task}")
                        cmd.summary
                        FileUtils.mkdir_p(File.dirname(rake_log))
                        File.open(rake_log, 'w') {|f| f.write(JSON.generate(cmd.to_hash)) }
                    else
                        puts 'rakefile.rb not found'
                    end
                end
            end

            def last_modified_time
                Dir.chdir(@directory) do
                    File.mtime(Dir.glob("**/*.*").select{|f| File.file?(f)}.max_by {|f| File.mtime(f)})
                end
            end

            # The latest tag for a branch of the repository

            def latest_tag(branch)
                Dir.chdir(directory) do
                    return `git describe --abbrev=0`.strip
                end
                ''
            end

            def user_name
                `git config --get user.name`.strip
            end
    
            def user_email
                `git config --get user.email`.strip
            end
    
            def user_can_commit
                return false if(user_name.length == 0)
                return false if(user_email.length == 0)
                return true
            end

            def branch
                Dir.chdir(directory) do
                    scan=`git branch`.scan(/\*\s([\w\.-]+)/)
                    return scan[0][0].to_s if(!scan.nil? && scan.length > 0 && scan[0].length > 0)
                end
                'master'
            end

            def repository
                if(@repository.nil?)
                    @repository = Raykit::Git::Repository.new(remote)
                end
                @repository
            end

            def remote
                if(Dir.exist?(directory))
                    Dir.chdir(directory) do
                        if(Dir.exist?('.git'))
                            return Command.new('git config --get remote.origin.url').output.strip 
                        end
                    end
                end
                ""
            end
        end
    end
end