Class: BuildTool::VCS::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/build-tool/vcs/git.rb

Overview

Implementation for the git version control system.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#apply_patches_after_rebase?, #configure, #local_path, #local_path_exist?, #patches_supported?, #recipe, #remote_path, #repository

Constructor Details

#initialize(config) ⇒ Git

Returns a new instance of Git.



55
56
57
58
59
# File 'lib/build-tool/vcs/git.rb', line 55

def initialize( config )
    super( config )
    @remote = {}
    @vcs = nil
end

Instance Method Details

#checkedout?Boolean

METHODS

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/build-tool/vcs/git.rb', line 76

def checkedout?
    return false if !local_path_exist?
    if !Pathname.new( local_path ).join( ".git" ).exist?
        raise Base::VcsError, "Checkout path #{local_path} is not a git repo!"
    end
    return true
end

#cloneObject

Initialize the local repository



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/build-tool/vcs/git.rb', line 85

def clone
    # Check if path exists
    if local_path_exist?
        raise GitError, "Failed to create repository at '#{local_path}': Path exists"
    end

    # Create the directory
    FileUtils.mkdir_p( local_path ) if !$noop

    # Initialize the repository
    if git( "init", local_path ) != 0
        raise GitError, "Error while initializing the repo `git init #{local_path}'`: #{$?}"
    end

    config.remote.each do |name, val|
        if git( "remote add #{name} #{val.url}" ) != 0
            raise GitError, "Error while initializing the repo `git remote add #{name} #{val.url}`: #{$?}"
        end
    end

    cmd = "remote add origin #{repository.url}"
    if git( cmd, local_path ) != 0
        raise GitError, "Error while initializing the repo `#{cmd}`: #{$?}"
    end

    logger.info <<-EOS
The following command sometimes fails when issued from this script. Reason unknown. The
best chance you have is issuing the command manually!
#{local_path}: git fetch #{config.track_remote}
#{local_path}: git checkout -b #{config.track_branch} #{config.track_remote}/#{config.track_branch}
    EOS

    fetch()

    cmd = "checkout -b #{config.track_branch} #{config.track_remote}/#{config.track_branch}"
    if git( cmd, local_path ) != 0
        raise GitError, "Error while initializing the repo `#{cmd}`: #{$?}"
    end

end

#fetchObject

Fetch from repository

Initializes the local clone if it does not exist.



141
142
143
144
145
146
147
148
149
# File 'lib/build-tool/vcs/git.rb', line 141

def fetch()
    if !checkedout? and !$noop
        clone
    end
    cmd = "fetch -q --prune #{config.track_remote}"
    if ( rc = git( cmd ) ) != 0
        raise GitError, "Error while fetching: #{rc}"
    end
end

#fetching_supported?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/build-tool/vcs/git.rb', line 68

def fetching_supported?
    true
end

#gcObject



126
127
128
# File 'lib/build-tool/vcs/git.rb', line 126

def gc
    git( "gc" )
end

#git(command, wd = local_path, &block) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/build-tool/vcs/git.rb', line 151

def git( command, wd = local_path, &block )
    rc = self.class.execute "git #{command}", wd, &block
    if rc != 0
        raise GitError, "git #{command || "" } failed with error code #{rc}";
    end
    rc
end

#nameObject

ATTRIBUTES



64
65
66
# File 'lib/build-tool/vcs/git.rb', line 64

def name
    "git"
end

#rebaseObject



159
160
161
162
163
# File 'lib/build-tool/vcs/git.rb', line 159

def rebase
    if 0 != ( git "rebase #{config.track_remote}/#{config.track_branch}" )
        raise GitSvnError, "Error while rebasing the repo with `git rebase git-svn: #{$?}"
    end
end

#remote?(name) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/build-tool/vcs/git.rb', line 130

def remote?( name )
    found = false
    git( "remote" ) do |line|
        found = true if line == name
    end
    return found
end