Defined Type: git::clone

Defined in:
modules/git/manifests/clone.pp

Overview

Definition: git::clone

Creates a git clone of a specified origin into a top level directory.

Required parameters

$directory

path to clone the repository into.

Optional parameters

$origin

If this is not specified, the the $title repository will be checked out from gerrit using a default gerrit url. If you set this, please specify the full repository url.

$branch

Branch you would like to check out.

$ensure

absent, present, or latest. Defaults to present.

  • present (default) will just clone once.

  • latest will execute a git pull if there are any changes.

  • absent will ensure the directory is deleted.

$owner

Owner of $directory, default: root. git commands will be run

by this user.
$group

Group owner of $directory, default: 'root'

$bare

$directory is the GIT_DIR itself. Workspace is not checked out. Default: false

$+recurse_submodules

If true, git

$shared

Enable git's core.sharedRepository=group setting for sharing the

repository between serveral users, default: false

$umask

umask value that git operations should run under, default 002 if shared, 022 otherwise.

$mode

Permission mode of $directory, default: 2755 if shared, 0755 otherwise

$ssh

SSH command/wrapper to use when checking out, default: ''

$timeout

Time out in seconds for the exec command, default: 300

$source

Where to request the repo from, if $origin isn't specified 'phabricator', 'github' and 'gerrit' accepted, default is 'gerrit'

Example usage

git::clone { 'my_clone_name':
    directory => '/path/to/clone/container',
    origin    => 'http://blabla.org/core.git',
    branch    => 'the_best_branch'
}

Will clone http://blabla.org/core.git branch the_best_branch at

+/path/to/clone/container/+

 # Example: check out from gerrit:
 git::clone { 'analytics/wikistats2':
     directory = '/srv/wikistats2',
 }

Parameters:

  • directory (Any)
  • origin (Any) (defaults to: undef)
  • branch (Any) (defaults to: '')
  • ssh (Any) (defaults to: '')
  • ensure (Any) (defaults to: 'present')
  • owner (Any) (defaults to: 'root')
  • group (Any) (defaults to: 'root')
  • shared (Any) (defaults to: false)
  • timeout (Any) (defaults to: '300')
  • depth (Any) (defaults to: 'full')
  • bare (Any) (defaults to: false)
  • recurse_submodules (Any) (defaults to: false)
  • umask (Any) (defaults to: undef)
  • mode (Any) (defaults to: undef)
  • source (Any) (defaults to: 'gerrit')


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'modules/git/manifests/clone.pp', line 51

define git::clone(
    $directory,
    $origin=undef,
    $branch='',
    $ssh='',
    $ensure='present',
    $owner='root',
    $group='root',
    $shared=false,
    $timeout='300',
    $depth='full',
    $bare=false,
    $recurse_submodules=false,
    $umask=undef,
    $mode=undef,
    $source='gerrit') {

    require_package('git')

    $default_url_format = $source ? {
        'phabricator' => 'https://phabricator.wikimedia.org/diffusion/%.git',
        'github'      => 'https://github.com/wikimedia/%s.git',
        'gerrit'      => 'https://gerrit.wikimedia.org/r/%s',
        default       => 'https://gerrit.wikimedia.org/r/%s',
    }

    $remote = $origin ? {
        undef   => sprintf($default_url_format, $title),
        default => $origin,
    }

    if $mode == undef {
        $file_mode = $shared ? {
            true    => '2775',
            default => '0755',
        }
    } elsif $shared and $mode !~ /^277\d/ {
        fail('Shared repositories must leave "mode" unspecified or set to 277?, specified as octal.')
    } else {
        $file_mode = $mode
    }

    if $umask == undef {
        $git_umask = $shared ? {
            true    => '002',
            default => '022',
        }
    } elsif $shared and $umask !~ /^00\d$/ {
        fail('Shared repositories must leave "umask" unspecified or set to 00?, specified as octal.')
    } else {
        $git_umask = $umask
    }

    case $ensure {
        'absent': {
            # make sure $directory does not exist
            file { $directory:
                ensure  => 'absent',
                recurse => true,
                force   => true,
            }
        }

        # otherwise clone the repository
        default: {
            $recurse_submodules_arg = $recurse_submodules ? {
                true    => '--recurse-submodules ',
                default => '',
            }
            # if branch was specified
            if !empty($branch) {
                $brancharg = "-b ${branch} "
            }
            # else don't checkout a non-default branch
            else {
                $brancharg = ''
            }
            if !empty($ssh) {
                $env = "GIT_SSH=${ssh}"
            } else {
                $env = undef
            }

            $deptharg = $depth ?  {
                'full'  => '',
                default => " --depth=${depth}"
            }

            if $bare == true {
                $barearg = ' --bare'
                $git_dir = $directory
            } else {
                $barearg = ''
                $git_dir = "${directory}/.git"
            }

            if $shared {
                $shared_arg = '-c core.sharedRepository=group'
            } else {
                $shared_arg = ''
            }

            $git = '/usr/bin/git'

            # set PATH for following execs
            Exec { path => '/usr/bin:/bin' }
            # clone the repository
            exec { "git_clone_${title}":
                command     => "${git} ${shared_arg} clone ${recurse_submodules_arg}${brancharg}${remote}${deptharg}${barearg} ${directory}",
                provider    => shell,
                logoutput   => on_failure,
                cwd         => '/tmp',
                environment => $env,
                creates     => "${git_dir}/config",
                user        => $owner,
                group       => $group,
                umask       => $git_umask,
                timeout     => $timeout,
                require     => Package['git'],
            }

            if (!defined(File[$directory])) {
                file { $directory:
                    ensure => 'directory',
                    mode   => $file_mode,
                    owner  => $owner,
                    group  => $group,
                    before => Exec["git_clone_${title}"],
                }
            }

            # pull if $ensure == latest and if there are changes to merge in.
            if $ensure == 'latest' {
                $remote_to_check = $branch ? {
                    ''      => 'remotes/origin/HEAD',
                    default => "remotes/origin/${branch}",
                }
                exec { "git_pull_${title}":
                    cwd       => $directory,
                    command   => "${git} ${shared_arg} pull ${recurse_submodules_arg}--quiet${deptharg}",
                    provider  => shell,
                    logoutput => on_failure,
                    # git diff --quiet will exit 1 (return false)
                    #  if there are differences
                    unless    => "${git} fetch && ${git} diff --quiet ${remote_to_check}",
                    user      => $owner,
                    group     => $group,
                    umask     => $git_umask,
                    require   => Exec["git_clone_${title}"],
                }
                # If we want submodules up to date, then we need
                # to run git submodule update --init after
                # git pull is run.
                if $recurse_submodules {
                    exec { "git_submodule_update_${title}":
                        command     => "${git} ${shared_arg} submodule update --init",
                        provider    => shell,
                        cwd         => $directory,
                        environment => $env,
                        refreshonly => true,
                        user        => $owner,
                        group       => $group,
                        umask       => $git_umask,
                        subscribe   => Exec["git_pull_${title}"],
                    }
                }
            }

        }
    }
}