Defined Type: git::clone

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

Summary

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

Overview

Optional parameters

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',
 }

 # Example: check out from gitlab:
 git::clone { 'repos/cloud/wikistats':
     directory => '/srv/wikistats',
     source    => 'gitlab',
 }

Examples:

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

Parameters:

  • directory (Stdlib::Unixpath)

    path to clone the repository into.

  • origin (Optional[String[1]]) (defaults to: undef)

    If this is not specified, 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 (Optional[String[1]]) (defaults to: undef)

    Branch you would like to check out.

  • git_tag (Optional[String[1]]) (defaults to: undef)

    Tag you would like to check out. Only one of 'branch' or 'git_tag' can be used.

  • ensure,,or. ('absent''present''latest')
    • 'present' will just clone once.

    • 'latest' will update the checkout according to 'update_method' (see below).

    • 'absent' will ensure the directory is deleted.

  • owner (String[1]) (defaults to: 'root')

    Owner of $directory. git commands will be run by this user.

  • group (String[1]) (defaults to: 'root')

    Group owner of $directory.

  • bare (Boolean) (defaults to: false)

    $directory is the GIT_DIR itself. Workspace is not checked out.

  • recurse_submodules (Boolean) (defaults to: false)

    If true, git recurse submodules

  • shared (Boolean) (defaults to: false)

    Enable git's core.sharedRepository=group setting for sharing the repository between serveral users, default: false

  • umask (Optional[Pattern[/\A\d{3,4}\z/]]) (defaults to: undef)

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

  • mode (Optional[Stdlib::Filemode]) (defaults to: undef)

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

  • ssh (Optional[String[1]]) (defaults to: undef)

    SSH command/wrapper to use when checking out

  • timeout (Integer) (defaults to: 300)

    Time out in seconds for the git clone command

  • depth (Optional[Integer[1]]) (defaults to: undef)

    the depth to clone if not present use full

  • source (String[1]) (defaults to: 'gerrit')

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

  • environment_variables (Array[String[1]]) (defaults to: [])

    An array of additional environment variables to pass to the git exec.

  • remote_name (String[1]) (defaults to: 'origin')

    the remote name used when setting the url

  • update_method (Enum['pull', 'checkout']) (defaults to: 'pull')

    Specifies the method to use to update the checkout when The value must be pull or checkout.

    • 'pull' will perform a merging pull if upstream changes.

    • 'checkout' will perform a forced checkout of the designated

    branch if upstream changes. Defaults to 'pull' for compatibility, but 'checkout' is the recommended value for clones that you want to be automatically maintained.

  • ensure (Enum['absent', 'latest', 'present']) (defaults to: 'present')


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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'modules/git/manifests/clone.pp', line 64

define git::clone(
    Stdlib::Unixpath                    $directory,
    Enum['absent', 'latest', 'present'] $ensure                = 'present',
    Enum['pull', 'checkout']            $update_method         = 'pull',
    String[1]                           $owner                 = 'root',
    String[1]                           $group                 = 'root',
    Boolean                             $shared                = false,
    Integer                             $timeout               = 300,
    Boolean                             $bare                  = false,
    Boolean                             $recurse_submodules    = false,
    String[1]                           $source                = 'gerrit',
    Array[String[1]]                    $environment_variables = [],
    String[1]                           $remote_name           = 'origin',
    Optional[Integer[1]]                $depth                 = undef,
    Optional[String[1]]                 $origin                = undef,
    Optional[String[1]]                 $branch                = undef,
    Optional[String[1]]                 $git_tag               = undef,
    Optional[String[1]]                 $ssh                   = undef,
    Optional[Pattern[/\A\d{3,4}\z/]]    $umask                 = undef,
    Optional[Stdlib::Filemode]          $mode                  = undef,
) {

    ensure_packages('git')

    $default_url_format = $source ? {
        'phabricator'      => 'https://phabricator.wikimedia.org/diffusion/%.git',
        'github'           => 'https://github.com/wikimedia/%s.git',
        'github-toolforge' => 'https://github.com/toolforge/%s.git',
        'gerrit'           => 'https://gerrit.wikimedia.org/r/%s',
        'gitlab'           => 'https://gitlab.wikimedia.org/%s.git',
        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
    }

    if $branch and $git_tag {
        fail('"branch" and "git_tag" cannot be used together.  Choose one')
    }

    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.bool2str('--recurse-submodules', '')

            $branch_or_tag = $branch.lest || { $git_tag }
            $brancharg = $branch_or_tag.then |$x| { "-b ${branch_or_tag}" }
            $env = $ssh ? {
                undef   => $environment_variables,
                default => $environment_variables << "GIT_SSH=${ssh}",
            }

            $deptharg = $depth.then |$x| { "--depth=${depth}" }

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


            $shared_arg = $shared.bool2str('-c core.sharedRepository=group', '')
            $git = '/usr/bin/git'


            $clone_cmd = @("COMMAND"/L)
            ${git} ${shared_arg} clone \
                ${recurse_submodules_arg} \
                ${brancharg} \
                ${remote} \
                ${deptharg} \
                ${barearg} \
                ${directory}
            |- COMMAND
            # clone the repository
            exec { "git_clone_${title}":
                command     => $clone_cmd.split(/\s+/).join(' '),
                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}"],
                }
            }

            # Ensure that the URL for 'origin' is always up-to-date.
            exec { "git_set_${remote_name}_${title}":
                cwd       => $directory,
                command   => "${git} remote set-url ${remote_name} ${remote}",
                provider  => shell,
                logoutput => on_failure,
                unless    => "[ \"\$(${git} remote get-url ${remote_name})\" = \"${remote}\" ]",
                user      => $owner,
                group     => $group,
                umask     => $git_umask,
                require   => Exec["git_clone_${title}"],
            }

            # if $ensure == latest, update the checkout when there are upstream changes.
            if $ensure == 'latest' {
                $local_branch_expression = $branch_or_tag.lest || {
                    # Use the default branch name obtained from the remote.
                    "$(git remote show ${remote_name} | awk -F': ' '\$1~/HEAD branch/ {print \$2; exit}')"
                }
                $ref_to_check = $git_tag ? {
                    undef => $branch ? {
                        undef   => "remotes/${remote_name}/HEAD",
                        default => "remotes/${remote_name}/${branch}",
                    },
                    default => "refs/tags/${git_tag}",
                }
                $checkout_cmd = @("COMMAND"/L)
                ${git} ${shared_arg} checkout --force --quiet \
                    -B ${local_branch_expression} \
                    ${ref_to_check} \
                    ${recurse_submodules_arg}
                |- COMMAND
                $update_cmd = $update_method ? {
                    'checkout' => $checkout_cmd,
                    'pull'     => "${git} ${shared_arg} pull ${recurse_submodules_arg} --quiet ${deptharg}",
                }.split(/\s+/).join(' ')
                exec { "git_${update_method}_${title}":
                    cwd       => $directory,
                    command   => $update_cmd,
                    provider  => shell,
                    logoutput => on_failure,
                    # git diff --quiet will exit 1 (return false)
                    #  if there are differences
                    unless    => "${git} fetch --tags --prune --prune-tags && ${git} diff --quiet ${ref_to_check}",
                    user      => $owner,
                    group     => $group,
                    umask     => $git_umask,
                    require   => Exec["git_set_${remote_name}_${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_${update_method}_${title}"],
                    }
                }
            }
        }
    }
}