Class: Autobuild::Git

Inherits:
Importer show all
Defined in:
lib/autobuild/import/git.rb

Defined Under Namespace

Classes: NetworkAccessNeeded, Status

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Importer

#interactive, #options, #post_hooks, #repository_id, #source_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Importer

add_post_hook, #add_post_hook, #apply, cache_dirs, #call_patch, #currently_applied_patches, default_cache_dirs, default_cache_dirs=, each_post_hook, #each_post_hook, #execute_post_hooks, fallback, #fallback, #fingerprint, #import, #interactive?, #parse_patch_list, #patch, #patchdir, #patches, #patches_fingerprint, #patchlist, #perform_checkout, #perform_update, #retry_count, #retry_count=, #save_patch_state, set_cache_dirs, #supports_relocation?, #unapply, unset_cache_dirs, #update_retry_count

Constructor Details

#initialize(repository, branch = nil, options = {}) ⇒ Git

Creates an importer which tracks a repository and branch.

This importer uses the ‘git’ tool to perform the import. It defaults to ‘git’ and can be configured by doing

Autobuild.programs['git'] = 'my_git_tool'

Parameters:

  • branch (String) (defaults to: nil)

    deprecated, use the ‘branch’ named option instead

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • push_to (String) — default: repository

    the URL to set up as push_to URL in the remote(s). Note that it is not used internally by this class

  • branch (String) — default: master

    the branch we should track. It is used both as #local_branch and #remote_branch

  • tag (String) — default: nil

    a tag at which we should pin the checkout. Cannot be given at the same time than :commit

  • commit (String) — default: nil

    a commit ID at which we should pin the checkout. Cannot be given at the same time than :tag

  • repository_id (String) — default: git:#repositoryy}

    a string that allows to uniquely identify a repository. The meaning is caller-specific. For instance, autoproj uses repository_id to check whether two Git importers fetches from the same repository.

  • with_submodules (Boolean) — default: false

    whether the importer should checkout and update submodules. Note that in an autobuild-based workflow, it is recommended to not use submodules but checkout all repositories separately instead.



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
# File 'lib/autobuild/import/git.rb', line 146

def initialize(repository, branch = nil, options = {})
    @git_dir_cache = Array.new
    @local_branch = @remote_branch = nil
    @tag = @commit = nil

    @merge = false

    if branch.respond_to?(:to_hash)
        options = branch.to_hash
        branch = nil
    end

    if branch
        Autobuild.warn "the git importer now expects you to provide the branch "\
            "as a named option"
        Autobuild.warn "this form is deprecated:"
        Autobuild.warn "Autobuild.git 'git://gitorious.org/rock/buildconf.git',"
        Autobuild.warn "    'master'"
        Autobuild.warn "and should be replaced by"
        Autobuild.warn "Autobuild.git 'git://gitorious.org/rock/buildconf.git',"
        Autobuild.warn "    branch: 'master'"
    end

    gitopts, common = Kernel.filter_options(
        options,
        push_to: nil,
        branch: nil,
        local_branch: nil,
        remote_branch: nil,
        tag: nil,
        commit: nil,
        repository_id: nil,
        source_id: nil,
        with_submodules: false,
        fingerprint_mode: Git.default_fingerprint_mode,
        single_branch: Git.single_branch?,
        shallow: nil
    )
    if gitopts[:shallow].nil? && Git.shallow?
        # Support for shallow clones is limited ... do not auto-set it
        # if we are in conditions where it will generate an error later
        gitopts[:shallow] =
            if gitopts[:tag]
                gitopts[:single_branch]
            else
                !gitopts[:commit]
            end
    end

    if gitopts[:branch] && branch
        raise ConfigException, "git branch specified with both the option hash "\
            "and the explicit parameter"
    end
    gitopts[:branch] ||= branch

    super(common)

    @single_branch = gitopts[:single_branch]
    @shallow = gitopts[:shallow]
    @with_submodules = gitopts.delete(:with_submodules)
    @alternates =
        if @with_submodules
            []
        else
            Git.default_alternates.dup
        end

    @remote_name = 'autobuild'
    @push_to = nil
    @fingerprint_mode = gitopts[:fingerprint_mode]
    relocate(repository, gitopts)
    @additional_remotes = Array.new
end

Class Attribute Details

.default_alternatesArray

A default list of repositories that should be used as reference repositories for all Git importers

It is initialized (by order of priority) using the AUTOBUILD_GIT_CACHE_DIR and AUTOBUILD_CACHE_DIR environment variables

Because of its role within the caching system in autobuild/autoproj, these defaults are not applied to git repositories that are using submodules. The autoproj cache builder does not generate repositories compatible with having submodules

Returns:

  • (Array)

See Also:

  • Git#alternates


49
50
51
52
53
54
55
56
57
58
# File 'lib/autobuild/import/git.rb', line 49

def default_alternates
    if @default_alternates then @default_alternates
    elsif (cache_dirs = Importer.cache_dirs('git'))
        @default_alternates = cache_dirs.map do |path|
            File.join(File.expand_path(path), '%s')
        end
    else
        Array.new
    end
end

.default_fingerprint_modeString

What git repository fingerprinting uses as reference

Can either be

  • “commit” (the default). Use the commit hash of HEAD

  • “tree”. Use the tree hash. This will return the same fingerprint

    for new commits that do not change the source code. However, it
    will fail to detect changes to the working copy that are due
    to git checkout filters.
    

Returns:

  • (String)


70
71
72
# File 'lib/autobuild/import/git.rb', line 70

def default_fingerprint_mode
  @default_fingerprint_mode
end

.shallow=(value) ⇒ Object (writeonly)

Sets shallow clones globally (applies to submodules as well)



19
20
21
# File 'lib/autobuild/import/git.rb', line 19

def shallow=(value)
  @shallow = value
end

.single_branch=(value) ⇒ Object (writeonly)

Sets the single_branch option globally for all Git importers This can can be overriden in the oporter options



11
12
13
# File 'lib/autobuild/import/git.rb', line 11

def single_branch=(value)
  @single_branch = value
end

Instance Attribute Details

#additional_remotes(String,String,String) (readonly)

A list of remotes that should be set up in the git config

Use #declare_alternate_repository to add one

Returns:

  • ((String,String,String))

    a list of (name, repository, push_to) triplets



281
282
283
# File 'lib/autobuild/import/git.rb', line 281

def additional_remotes
  @additional_remotes
end

#alternatesArray<String>

A list of local (same-host) repositories that will be used instead of the remote one when possible. It has one major issue (see below), so use at your own risk.

The paths must point to the git directory, so either the .git directory in a checked out git repository, or the repository itself in a bare repository.

A default reference repository can be given through the AUTOBUILD_GIT_CACHE environment variable.

Note that it has the major caveat that if objects disappear from the reference repository, the current one will be broken. See the git documentation for more information.

Returns:

  • (Array<String>)


273
274
275
# File 'lib/autobuild/import/git.rb', line 273

def alternates
  @alternates
end

#branchObject

The branch this importer is tracking



249
250
251
# File 'lib/autobuild/import/git.rb', line 249

def branch
  @branch
end

#commitObject

The commit we are pointing to. It is a commit ID.

Setting it through this method is deprecated, use #relocate to set the commit



307
308
309
# File 'lib/autobuild/import/git.rb', line 307

def commit
  @commit
end

#local_branchObject

The branch that should be used on the local clone

Defaults to #branch



286
287
288
# File 'lib/autobuild/import/git.rb', line 286

def local_branch
    @local_branch || branch
end

#merge=(value) ⇒ Object (writeonly)

Set the merge flag. See #merge?



317
318
319
# File 'lib/autobuild/import/git.rb', line 317

def merge=(value)
  @merge = value
end

#push_toObject

If set, this URL will be listed as a pushurl for the tracked branch. It makes it possible to have a read-only URL for fetching and specify a push URL for people that have commit rights

It is not used by the importer itself

#repository is always used for read-only operations



241
242
243
# File 'lib/autobuild/import/git.rb', line 241

def push_to
  @push_to
end

#remote_branchObject

The remote branch to which we should push

Defaults to #branch



293
294
295
# File 'lib/autobuild/import/git.rb', line 293

def remote_branch
    @remote_branch || branch
end

#remote_nameObject

The name of the remote that should be set up by the importer

Defaults to ‘autobuild’



227
228
229
# File 'lib/autobuild/import/git.rb', line 227

def remote_name
  @remote_name
end

#repositoryObject

The remote repository URL.

See Also:



232
233
234
# File 'lib/autobuild/import/git.rb', line 232

def repository
  @repository
end

#shallow=(value) ⇒ Object (writeonly)

Set the #shallow? predicate



339
340
341
# File 'lib/autobuild/import/git.rb', line 339

def shallow=(value)
  @shallow = value
end

#single_branch=(value) ⇒ Object (writeonly)

Set the #single_branch? predicate



336
337
338
# File 'lib/autobuild/import/git.rb', line 336

def single_branch=(value)
  @single_branch = value
end

#tagObject

The tag we are pointing to. It is a tag name.

Setting it through this method is deprecated, use #relocate to set the tag



301
302
303
# File 'lib/autobuild/import/git.rb', line 301

def tag
  @tag
end

#with_submodules=(value) ⇒ Object (writeonly)

Set to true if checkout should be done with submodules

Defaults to false



246
247
248
# File 'lib/autobuild/import/git.rb', line 246

def with_submodules=(value)
  @with_submodules = value
end

Class Method Details

.at_least_version(*version) ⇒ Boolean

Tests the git version

Parameters:

  • version (Array<Integer>)

    the git version as an array of integer

Returns:

  • (Boolean)

    true if the git version is at least the requested one, and false otherwise



115
116
117
# File 'lib/autobuild/import/git.rb', line 115

def self.at_least_version(*version)
    compare_versions(self.version, version) <= 0
end

.can_handle?(path) ⇒ Boolean

Tests whether the given directory is a git repository

Returns:

  • (Boolean)


1418
1419
1420
1421
# File 'lib/autobuild/import/git.rb', line 1418

def self.can_handle?(path)
    _, style = Git.resolve_git_dir(path)
    style == :normal
end

.compare_versions(actual, required) ⇒ Integer

Helper method to compare two (partial) versions represented as array of integers

Returns:

  • (Integer)

    -1 if actual is greater than required, 0 if equal, and 1 if actual is smaller than required



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/autobuild/import/git.rb', line 98

def self.compare_versions(actual, required)
    return -compare_versions(required, actual) if actual.size > required.size

    actual += [0] * (required.size - actual.size)
    actual.zip(required).each do |v_act, v_req|
        if v_act > v_req then return -1
        elsif v_act < v_req then return 1
        end
    end
    0
end

.default_configObject



73
74
75
# File 'lib/autobuild/import/git.rb', line 73

def self.default_config
    @default_config ||= Hash.new
end

.git_dir(package, require_working_copy) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

(see Git#git_dir)



458
459
460
461
462
# File 'lib/autobuild/import/git.rb', line 458

def self.git_dir(package, require_working_copy)
    dir, style = Git.resolve_git_dir(package.importdir)
    validate_git_dir(package, require_working_copy, dir, style)
    dir
end

.has_uncommitted_changes?(package, with_untracked_files = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Tests whether the package’s working copy has uncommitted changes

Parameters:

  • package (Package)
  • with_untracked_files (Boolean) (defaults to: false)

    whether untracked files are considered uncommitted changes

Returns:

  • (Boolean)


724
725
726
727
728
729
730
731
# File 'lib/autobuild/import/git.rb', line 724

def self.has_uncommitted_changes?(package, with_untracked_files = false)
    status = run_git(package, 'status', '--porcelain').map(&:strip)
    if with_untracked_files
        !status.empty?
    else
        status.any? { |l| l[0, 2] !~ /^\?\?|^  / }
    end
end

.lfs_installed?Boolean

Returns:

  • (Boolean)


1300
1301
1302
1303
1304
1305
# File 'lib/autobuild/import/git.rb', line 1300

def self.lfs_installed?
    return @lfs_installed unless @lfs_installed.nil?

    _, _, status = Open3.capture3('git lfs')
    @lfs_installed = status.success?
end

.resolve_git_dir(path) ⇒ (String,Symbol)?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves the git directory associated with path, and tells whether it is a bare repository or not

Parameters:

  • path (String)

    the path from which we should resolve

Returns:

  • ((String,Symbol), nil)

    either the path to the git folder and :bare or :normal, or nil if path is not a git repository.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/autobuild/import/git.rb', line 419

def self.resolve_git_dir(path)
    gitdir = File.join(path, '.git')
    path = gitdir if File.exist?(gitdir)

    result = `#{Autobuild.tool(:git)} --git-dir="#{path}" rev-parse \
        --is-bare-repository 2>&1`
    if $CHILD_STATUS.success?
        if result.strip == "true"
            [path, :bare]
        else
            [path, :normal]
        end
    end
end

.run_git(package, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

(see Git#run_git)



552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/autobuild/import/git.rb', line 552

def self.run_git(package, *args)
    options =
        if args.last.kind_of?(Hash)
            args.pop
        else
            Hash.new
        end

    working_directory = File.dirname(git_dir(package, true))
    package.run(:import, Autobuild.tool(:git), *args,
                Hash[resolved_env: Hash.new,
                     working_directory: working_directory].merge(options))
end

.run_git_bare(package, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

(see Git#run_git_bare)



578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/autobuild/import/git.rb', line 578

def self.run_git_bare(package, *args)
    options =
        if args.last.kind_of?(Hash)
            args.pop
        else
            Hash.new
        end

    package.run(:import, Autobuild.tool(:git),
                '--git-dir', git_dir(package, false),
                *args, Hash[resolved_env: Hash.new].merge(options))
end

.shallow?Boolean

Whether shallow clones is enabled globally

Returns:

  • (Boolean)


22
23
24
# File 'lib/autobuild/import/git.rb', line 22

def shallow?
    !!@shallow
end

.single_branch?Boolean

Whether single_branch is enabled globally

Returns:

  • (Boolean)


14
15
16
# File 'lib/autobuild/import/git.rb', line 14

def single_branch?
    !!@single_branch
end

.validate_git_dir(package, require_working_copy, _dir, style) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates the return value of resolve_git_dir

Parameters:

  • package (Package)

    the package we are working on

  • require_working_copy (Boolean)

    if false, a bare repository will be considered as valid, otherwise not

  • dir (String, nil)

    the path to the repository’s git directory, or nil if the target is not a valid repository (see the documentation of resolve_git_dir

  • style (Symbol, nil)

    either :normal for a git checkout with working copy, :bare for a bare repository or nil if resolve_git_dir did not detect a git repository

Raises:

  • ConfigException if dir/style are nil, or if require_working_copy is true and style is :bare



481
482
483
484
485
486
487
488
489
490
491
# File 'lib/autobuild/import/git.rb', line 481

def self.validate_git_dir(package, require_working_copy, _dir, style)
    if !style
        raise ConfigException.new(package, 'import', retry: false),
              "while importing #{package.name}, #{package.importdir} "\
              "does not point to a git repository"
    elsif require_working_copy && (style == :bare)
        raise ConfigException.new(package, 'import', retry: false),
              "while importing #{package.name}, #{package.importdir} "\
              "points to a bare git repository but a working copy was required"
    end
end

.vcs_definition_for(path, remote_name = 'autobuild') ⇒ Object

Returns a hash that represents the configuration of a git importer based on the information contained in the git configuration

Raises:

  • (ArgumentError)

    if the path does not point to a git repository



1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
# File 'lib/autobuild/import/git.rb', line 1427

def self.vcs_definition_for(path, remote_name = 'autobuild')
    unless can_handle?(path)
        raise ArgumentError, "#{path} is neither a git repository, "\
            "nor a bare git repository"
    end

    Dir.chdir(path) do
        vars = `#{Autobuild.tool(:git)} config -l`.
            split("\n").
            each_with_object(Hash.new) do |line, h|
                k, v = line.strip.split('=', 2)
                h[k] = v
            end
        url = vars["remote.#{remote_name}.url"] || vars['remote.origin.url']
        if url
            return Hash[type: :git, url: url]
        else
            return Hash[type: :git]
        end
    end
end

.versionString

Returns the git version as a string

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
91
# File 'lib/autobuild/import/git.rb', line 82

def self.version
    version = Subprocess.run('git', 'setup', Autobuild.tool(:git), '--version').
        first
    if version =~ /^git version (\d[\d.]+)/
        $1.split(".").map { |i| Integer(i) }
    else
        raise ArgumentError, "cannot parse git version string #{version}, "\
            "was expecting something looking like 'git version 2.1.0'"
    end
end

Instance Method Details

#checkout(package, _options = Hash.new) ⇒ Object



1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
# File 'lib/autobuild/import/git.rb', line 1331

def checkout(package, _options = Hash.new)
    shallow_clone = validate_shallow(package)

    base_dir = File.expand_path('..', package.importdir)
    FileUtils.mkdir_p(base_dir) unless File.directory?(base_dir)

    clone_options = Array.new
    if with_submodules?
        clone_options << '--recurse-submodules'
        clone_options << '--shallow-submodules' if shallow_clone
    end

    clone_options << '--depth' << '1' if shallow_clone

    if single_branch?
        if tag
            if tag.start_with?("refs/")
                raise ArgumentError, "you cannot provide a full ref for"\
                    " the tag while cloning a single branch"
            end
            clone_options << "--branch=#{tag}"
        elsif remote_branch
            if remote_branch.start_with?("refs/")
                raise ArgumentError, "you cannot provide a full ref for"\
                    " the remote branch while cloning a single branch"
            end
            clone_options << "--branch=#{remote_branch}"
        end
        clone_options << "--single-branch"
    elsif shallow_clone
        clone_options << "--no-single-branch"
    end

    each_alternate_path(package) do |path|
        clone_options << '--reference' << path
    end
    self.class.default_config.each do |key, value|
        clone_options << "--config=#{key}=#{value}"
    end
    package.run(:import,
                Autobuild.tool('git'), 'clone', '-o', remote_name, *clone_options,
                repository, package.importdir, retry: true)

    update_remotes_configuration(package, only_local: false)
    update(package, only_local: !remote_branch.start_with?("refs/"),
                    reset: :force)
    if with_submodules?
        run_git(package, "submodule", "update", '--init', '--recursive')
    end
end

#commit_present_in?(package, rev, reference) ⇒ Boolean

Tests whether a commit is already present in a given history

Parameters:

  • the (Package)

    package we are working on

  • rev (String)

    what we want to verify the presence of

  • reference (String)

    the reference commit. The method tests that ‘commit’ is present in the history of ‘reference’

Returns:

  • (Boolean)


951
952
953
954
955
956
957
958
959
960
961
# File 'lib/autobuild/import/git.rb', line 951

def commit_present_in?(package, rev, reference)
    commit = rev_parse(package, rev)
    begin
        merge_base = run_git_bare(package, 'merge-base', commit, reference).first
        merge_base == commit
    rescue Exception
        raise PackageException.new(package, 'import'), "failed to find "\
            "the merge-base between #{rev} and #{reference}. "\
            "Are you sure these commits exist ?"
    end
end

#current_branch(package) ⇒ String?

Returns the branch HEAD is pointing to

Returns:

  • (String, nil)

    the full ref HEAD is pointing to (i.e. refs/heads/master), or nil if HEAD is detached



857
858
859
860
861
# File 'lib/autobuild/import/git.rb', line 857

def current_branch(package)
    run_git_bare(package, 'symbolic-ref', 'HEAD', '-q').first.strip
rescue SubcommandFailed => e
    raise if e.status != 1
end

#current_remote_commit(package, options = Hash.new) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the commit ID of what we should consider being the remote commit

Parameters:

Returns:

  • (String)

    the commit ID as a string



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/autobuild/import/git.rb', line 746

def current_remote_commit(package, options = Hash.new)
    options = Hash[only_local: options] unless options.kind_of?(Hash)
    only_local = options.delete(:only_local)

    if only_local
        unless remote_branch.start_with?("refs/")
            refspec =
                options[:refspec] ||
                ("refs/tags/#{tag}" if tag) ||
                "refs/remotes/#{remote_name}/#{remote_branch}"
        end
        unless (refspec = Array(refspec).first)
            raise ArgumentError, "cannot use only_local with no tag,"\
              " and an absolute remote ref"
        end

        begin
            run_git_bare(package, 'show-ref', '-s', refspec).first.strip
        rescue SubcommandFailed
            raise PackageException.new(package, "import"),
                  "cannot resolve #{refspec}"
        end
    else
        refspec =
            options[:refspec] ||
            ("refs/tags/#{tag}" if tag) ||
            remote_branch_to_ref(remote_branch)
        begin fetch_remote(package, refspec: refspec)
        rescue Exception => e
            fallback(e, package, :status, package, only_local)
        end
    end
end

#declare_alternate_repository(name, repository, options = Hash.new) ⇒ Object



1449
1450
1451
1452
1453
1454
1455
# File 'lib/autobuild/import/git.rb', line 1449

def declare_alternate_repository(name, repository, options = Hash.new)
    unless name
        raise ArgumentError, "cannot declare alternate repository "\
            "#{repository} without a name"
    end
    additional_remotes << [name, repository, options[:push_to] || repository]
end

#delta_between_tags(package, from_tag, to_tag) ⇒ Object

Computes the merge status for this package between two existing tags

Parameters:

  • package (Package)
  • from_tag (String)

    the source tag

  • to_tag (String)

    the target tag

Raises:

  • (ArgumentError)

    if one of the tags is unknown



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/autobuild/import/git.rb', line 499

def delta_between_tags(package, from_tag, to_tag)
    pkg_tags = tags(package)
    unless pkg_tags.key?(from_tag)
        raise ArgumentError, "tag '#{from_tag}' is unknown to #{package.name} "\
            "-- known tags are: #{pkg_tags.keys}"
    end
    unless pkg_tags.key?(to_tag)
        raise ArgumentError, "tag '#{to_tag}' is unknown to #{package.name} "\
            "-- known tags are: #{pkg_tags.keys}"
    end

    from_commit = pkg_tags[from_tag]
    to_commit = pkg_tags[to_tag]

    merge_status(package, to_commit, from_commit)
end

#describe_commit_on_remote(package, rev = 'HEAD', options = Hash.new) ⇒ String

Finds a remote reference that contains a commit

It will favor the configured #remote_branch if it matches

Parameters:

  • package (Autobuild::Package)

    the package we are working on

  • commit_id (String)

    the commit ID (can be a rev)

Returns:

  • (String)

    a remote ref

Raises:



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/autobuild/import/git.rb', line 971

def describe_commit_on_remote(package, rev = 'HEAD', options = Hash.new)
    rev = rev.to_str
    options = Kernel.validate_options options,
                                      tags: true

    commit_id = rev_parse(package, rev)

    remote_refs = Hash[*each_remote_ref(package).to_a.flatten]
    remote_branch_ref = remote_branch_to_ref(remote_branch)
    remote_branch_id = remote_refs.delete(remote_branch_ref)
    begin
        if commit_present_in?(package, commit_id, remote_branch_id)
            return remote_branch
        end
    rescue PackageException # We have to fetch. Fetch all branches at once
        fetch_remote(package, refspec: [remote_branch_ref, *remote_refs.keys])
        if commit_present_in?(package, commit_id, remote_branch_id)
            return remote_branch
        end
    end

    remote_refs.delete_if { |r| r =~ %r{^refs/tags/} } unless options[:tags]

    # Prefer tags, then heads, then the rest (e.g. github pull requests)
    remote_refs = remote_refs.sort_by do |rev_name, _rev_id|
        case rev_name
        when %r{^refs/tags/} then 0
        when %r{^refs/heads/} then 1
        else 2
        end
    end

    remote_refs.delete_if do |rev_name, rev_id|
            if commit_present_in?(package, commit_id, rev_id)
                return rev_name
            else
                true
            end
    rescue PackageException
            false
    end

    unless remote_refs.empty?
        fetch_remote(package, refspec: remote_refs.map(&:first))
        remote_refs.each do |rev_name, rev_id|
            return rev_name if commit_present_in?(package, commit_id, rev_id)
        end
    end

    raise PackageException.new(package), "current HEAD (#{commit_id}) does not "\
        "seem to be present on the remote"
end

#describe_rev(package, rev) ⇒ Object

Resolve a commit ref to a tag or commit ID



649
650
651
652
653
654
655
656
# File 'lib/autobuild/import/git.rb', line 649

def describe_rev(package, rev)
    tag = run_git_bare(package, 'describe',
                       '--tags', '--exact-match', rev).first.strip
    [true, tag.encode('UTF-8')]
rescue Autobuild::SubcommandFailed
    commit = rev_parse(package, rev)
    [false, commit.encode('UTF-8')]
end

#detached_head?(package) ⇒ Boolean

Returns:

  • (Boolean)


838
839
840
# File 'lib/autobuild/import/git.rb', line 838

def detached_head?(package)
    current_branch(package).nil?
end

#determine_target_state(package, only_local: false) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
# File 'lib/autobuild/import/git.rb', line 1168

def determine_target_state(package, only_local: false)
    pinned_state =
        if commit then commit
        elsif tag then "refs/tags/#{tag}"
        end

    if pinned_state
        unless has_commit?(package, pinned_state)
            fetch_commit = current_remote_commit(
                package,
                only_local: only_local,
                refspec: [remote_branch_to_ref(remote_branch), tag])
        end
        target_commit = pinned_state = rev_parse(package, pinned_state)
    else
        target_commit = fetch_commit =
            current_remote_commit(package, only_local: only_local)
    end

    [pinned_state, target_commit, fetch_commit]
end

#each_alternate_path(package) ⇒ Object



1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/autobuild/import/git.rb', line 1280

def each_alternate_path(package)
    return enum_for(__method__, package) unless block_given?

    alternates.each do |path|
        path = format(path, package.name)
        yield(path) if File.directory?(path)
    end
    nil
end

#each_configured_remote {|remote_name, url, push_to| ... } ⇒ Object

Enumerates the remotes that this importer would set up on the repository

Yield Parameters:

  • remote_name (String)

    the remote name

  • url (String)

    the remote URL

  • push_to (String)

    the remote push-to URL



625
626
627
628
629
# File 'lib/autobuild/import/git.rb', line 625

def each_configured_remote
    ([['autobuild', repository, push_to]] + additional_remotes).each do |args|
        yield(args[0], args[1], args[2] || args[1])
    end
end

#each_remote_ref(package) {|ref_name, commit_id| ... } ⇒ Object

Enumerates the ref that are present on the remote

Yield Parameters:

  • ref_name (String)

    the ref name

  • commit_id (String)

    the ref’s commit ID



662
663
664
665
666
667
668
669
# File 'lib/autobuild/import/git.rb', line 662

def each_remote_ref(package)
    return enum_for(__method__, package) unless block_given?

    run_git_bare(package, 'ls-remote', repository).each do |line|
        commit_id, ref_name = line.split(/\s+/)
        yield(ref_name, commit_id) if ref_name !~ /\^/
    end
end

#fetch_remote(package, options = Hash.new) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetches updates from the remote repository. Returns the remote commit ID on success, nil on failure. Expects the current directory to be the package’s source directory.



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/autobuild/import/git.rb', line 676

def fetch_remote(package, options = Hash.new)
    validate_importdir(package)
    unless options[:refspec]
        raise ArgumentError, "required argument 'refspec' not given"
    end

    git_dir = git_dir(package, false)

    # If we are checking out a specific commit, we don't know which
    # branch to refer to in git fetch. So, we have to set up the
    # remotes and call git fetch directly (so that all branches get
    # fetch)
    #
    # Otherwise, do git fetch now
    #
    # Doing it now is better as it makes sure that we replace the
    # configuration parameters only if the repository and branch are
    # OK (i.e. we keep old working configuration instead)
    refspec = Array(options[:refspec])
    tag_arg = ['--tags'] if tag
    run_git_bare(package, 'fetch', repository, *tag_arg, *refspec, retry: true)

    update_remotes_configuration(package)

    # Now get the actual commit ID from the FETCH_HEAD file, and
    # return it
    if File.readable?(File.join(git_dir, 'FETCH_HEAD'))
        fetched_commits = File.readlines(File.join(git_dir, 'FETCH_HEAD')).
            find_all { |l| l !~ /not-for-merge/ }.
            map { |line| line.split(/\s+/).first }
        refspec.zip(fetched_commits).each do |spec, commit_id|
            if spec =~ %r{^refs/heads/(.*)$}
                run_git_bare(package, 'update-ref', "-m", "updated by autobuild",
                             "refs/remotes/#{remote_name}/#{$1}", commit_id)
            end
        end

        fetched_commits.first
    end
end

#git_dir(package, require_working_copy) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns either the package’s working copy or git directory

Parameters:

  • package (Package)

    the package to resolve

  • require_working_copy (Boolean)

    whether a working copy is required

Raises:

  • if the package’s Package#importdir is not a git repository, or if it is a bare repository and require_working_copy is true



443
444
445
446
447
448
449
450
451
452
453
# File 'lib/autobuild/import/git.rb', line 443

def git_dir(package, require_working_copy)
    if @git_dir_cache[0] == package.importdir
        dir, style = *@git_dir_cache[1, 2]
    else
        dir, style = Git.resolve_git_dir(package.importdir)
    end

    @git_dir_cache = [package.importdir, dir, style]
    self.class.validate_git_dir(package, require_working_copy, dir, style)
    dir
end

#has_all_branches?Boolean

Whether both the local and remote branches are known

See documentation of #resolve_all_branches

Returns:

  • (Boolean)


386
387
388
# File 'lib/autobuild/import/git.rb', line 386

def has_all_branches?
    remote_branch && local_branch
end

#has_branch?(package, branch_name) ⇒ Boolean

Returns:

  • (Boolean)


822
823
824
825
826
827
828
829
830
831
832
# File 'lib/autobuild/import/git.rb', line 822

def has_branch?(package, branch_name)
    run_git_bare(package, 'show-ref', '-q', '--verify',
                 remote_branch_to_ref(branch_name))
    true
rescue SubcommandFailed => e
    if e.status == 1
        false
    else
        raise
    end
end

#has_commit?(package, commit_id) ⇒ Boolean

Returns:

  • (Boolean)


811
812
813
814
815
816
817
818
819
820
# File 'lib/autobuild/import/git.rb', line 811

def has_commit?(package, commit_id)
    run_git_bare(package, 'rev-parse', '-q', '--verify', "#{commit_id}^{commit}")
    true
rescue SubcommandFailed => e
    if e.status == 1
        false
    else
        raise
    end
end

#has_local_branch?(package) ⇒ Boolean

Returns:

  • (Boolean)


834
835
836
# File 'lib/autobuild/import/git.rb', line 834

def has_local_branch?(package)
    has_branch?(package, local_branch) if local_branch
end

#merge?Boolean

True if it is allowed to merge remote updates automatically. If false (the default), the import will fail if the updates do not resolve as a fast-forward

Returns:

  • (Boolean)


312
313
314
# File 'lib/autobuild/import/git.rb', line 312

def merge?
    @merge
end

#merge_if_simple(package, target_commit) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
# File 'lib/autobuild/import/git.rb', line 1253

def merge_if_simple(package, target_commit)
    status = merge_status(package, target_commit)
    if status.needs_update?
        if !merge? && status.status == Status::NEEDS_MERGE
            raise PackageException.new(package, 'import'), "the local branch "\
                "'#{local_branch}' and the remote branch #{branch} of "\
                "#{package.name} have diverged, and I therefore refuse "\
                "to update automatically. Go into #{package.importdir} "\
                "and either reset the local branch or merge the remote changes"
        end
        run_git(package, 'merge', target_commit)
        return true
    end
    false
end

#merge_status(package, fetch_commit, reference_commit = "HEAD") ⇒ Object

Computes the update status to update a branch whose tip is at reference_commit (which can be a symbolic reference) using the fetch_commit commit

I.e. this compute what happens if one would do

git checkout reference_commit
git merge fetch_commit


1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/autobuild/import/git.rb', line 1033

def merge_status(package, fetch_commit, reference_commit = "HEAD")
    begin
        common_commit = run_git_bare(package, 'merge-base',
                                     reference_commit, fetch_commit).first.strip
    rescue Exception
        raise PackageException.new(package, 'import'), "failed to find "\
            "the merge-base between #{reference_commit} and #{fetch_commit}. "\
            "Are you sure these commits exist ?"
    end
    remote_commit = rev_parse(package, fetch_commit)
    head_commit   = rev_parse(package, reference_commit)

    status = if common_commit != remote_commit
                 if common_commit == head_commit
                     Status::SIMPLE_UPDATE
                 else
                     Status::NEEDS_MERGE
                 end
             elsif common_commit == head_commit
                 Status::UP_TO_DATE
             else
                 Status::ADVANCED
             end

    Status.new(package, status, fetch_commit, head_commit, common_commit)
end

#on_local_branch?(package) ⇒ Boolean

Checks if the current branch is the target branch. Expects that the current directory is the package’s directory

Returns:

  • (Boolean)


865
866
867
868
869
# File 'lib/autobuild/import/git.rb', line 865

def on_local_branch?(package)
    if (current_branch = self.current_branch(package))
        current_branch == "refs/heads/#{local_branch}"
    end
end

#on_target_branch?(package) ⇒ Boolean

Deprecated.

use on_local_branch? instead

Returns:

  • (Boolean)


872
873
874
# File 'lib/autobuild/import/git.rb', line 872

def on_target_branch?(package)
    on_local_branch?(package)
end

#relocate(repository, options = Hash.new) ⇒ Object

Changes the repository this importer is pointing to



1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
# File 'lib/autobuild/import/git.rb', line 1383

def relocate(repository, options = Hash.new)
    options = options.transform_keys(&:to_sym)

    local_branch  =
        options[:local_branch] || options[:branch] ||
        self.local_branch || options[:default_branch] || nil
    remote_branch =
        options[:remote_branch] || options[:branch] ||
        self.remote_branch || options[:default_branch] || nil
    if local_branch&.start_with?("refs/")
        raise ArgumentError, "you cannot provide a full ref for"\
            " the local branch, only for the remote branch"
    end

    @push_to = options[:push_to] || @push_to
    @branch = @local_branch = @remote_branch = nil
    if local_branch == remote_branch
        @branch = local_branch
    else
        @local_branch = local_branch
        @remote_branch = remote_branch
    end
    @tag    = options.fetch(:tag, @tag)
    @commit = options.fetch(:commit, @commit)

    @repository = repository.to_str
    @repository_id =
        options[:repository_id] ||
        "git:#{@repository}"
    @source_id =
        options[:source_id] ||
        "#{@repository_id} branch=#{remote_branch} tag=#{tag} commit=#{commit}"
end

#reset_head_to_commit(package, target_commit, fetch_commit, options = Hash.new) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Safely resets the current branch to a given commit

This method safely resets the current branch to a given commit, not requiring a clean working copy (i.e. it can handle local changes).

It verifies that the current HEAD will not be lost by the operation, either because it is included in the target commit or because it is present remotely

Parameters:

  • package (Package)

    the package we handle

  • target_commit (String)

    the commit we want to reset HEAD to

  • fetch_commit (String)

    the state of the remote branch. This is used to avoid losing commits if HEAD is not included in target_commit

  • options (Hash) (defaults to: Hash.new)

    a customizable set of options

Options Hash (options):

  • force (Boolean) — default: false

    bypasses checks that verify that some commits won’t be lost by resetting



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/autobuild/import/git.rb', line 1120

def reset_head_to_commit(package, target_commit, fetch_commit, options = Hash.new)
    current_head     = rev_parse(package, 'HEAD')
    head_to_target   = merge_status(package, target_commit, current_head)
    status_to_target = head_to_target.status

    if status_to_target == Status::UP_TO_DATE
        return false
    elsif status_to_target == Status::SIMPLE_UPDATE
        run_git(package, 'merge', target_commit)
    elsif !options[:force]
        # Check whether the current HEAD is present on the remote
        # repository. We'll refuse resetting if there are uncommitted
        # changes
        unless commit_present_in?(package, current_head, fetch_commit)
            raise ImporterCannotReset.new(package, 'import'),
                  "branch #{local_branch} of #{package.name} contains"\
                  " commits that do not seem to be present on the branch"\
                  " #{remote_branch} of the remote repository. I can't"\
                  " go on as it could make you lose some stuff. Update"\
                  " the remote branch in your overrides, push your"\
                  " changes or reset to the remote commit manually"\
                  " before trying again"
        end
    end

    package.message format("  %%s: resetting branch %<branch>s to %<commit>s",
                           branch: local_branch, commit: target_commit)
    # I don't use a reset --hard here as it would add even more
    # restrictions on when we can do the operation (as we would refuse
    # doing it if there are local changes). The checkout creates a
    # detached HEAD, but makes sure that applying uncommitted changes is
    # fine (it would abort otherwise). The rest then updates HEAD and
    # the local_branch ref to match the required target commit
    resolved_target_commit = rev_parse(package, "#{target_commit}^{commit}")
    begin
        run_git(package, 'checkout', target_commit)
        run_git(package, 'update-ref', "refs/heads/#{local_branch}",
                resolved_target_commit)
        run_git(package, 'symbolic-ref', "HEAD", "refs/heads/#{local_branch}")
    rescue ::Exception
        run_git(package, 'symbolic-ref', "HEAD", target_commit)
        run_git(package, 'update-ref', "refs/heads/#{local_branch}", current_head)
        run_git(package, 'checkout', "refs/heads/#{local_branch}")
        raise
    end
    true
end

#resolve_all_branches(package, only_local: false) ⇒ Object

Resolve branches based on the remote’s HEAD

Since GitHub (and others) decided to change the name of the “default” branch, we can’t assume that master is … well … master.

For this reason, a Git importer does not have a built-in default. If the branch(es) are not provided explicitly, the importer will call this method to guess the name of the default branch instead.

Call #has_all_branches? to determine whether it is necessary



400
401
402
403
404
405
406
407
408
409
# File 'lib/autobuild/import/git.rb', line 400

def resolve_all_branches(package, only_local: false)
    default_branch = resolve_remote_head(package, only_local: only_local)
    unless default_branch
        raise NetworkAccessNeeded,
              "determining the remote branch would require access to "\
              "the network, and only_local is true"
    end

    relocate(repository, default_branch: default_branch)
end

#resolve_remote_head(package, only_local: false) ⇒ Object

Return default local branch if exists, if not return the default remote branch

Parameters:



378
379
380
381
# File 'lib/autobuild/import/git.rb', line 378

def resolve_remote_head(package, only_local: false)
    try_resolve_remote_head_from_local(package) ||
        (!only_local && try_resolve_remote_head_from_server(package))
end

#rev_parse(package, name, object_type = "commit") ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves a revision into a commit ID

Parameters:

  • package (Package)
  • name (String)

    the revspec that is to be resolved

  • objecT_type (String)

    the type of git object we want to resolve to

Returns:

  • (String)

    the commit ID

Raises:



921
922
923
924
925
926
927
928
# File 'lib/autobuild/import/git.rb', line 921

def rev_parse(package, name, object_type = "commit")
    name = "#{name}^{#{object_type}}" if object_type
    run_git_bare(package, 'rev-parse', '-q', '--verify', name).first
rescue Autobuild::SubcommandFailed
    raise PackageException.new(package, 'import'),
          "failed to resolve #{name}. "\
          "Are you sure this commit, branch or tag exists ?"
end

#run_git(package, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run a git command that require a working copy

Parameters:

  • package (Package)
  • args (Array)

    the git arguments, excluding the git command itself. The last argument can be a hash, in which case it is passed as an option hash to Package#run



545
546
547
# File 'lib/autobuild/import/git.rb', line 545

def run_git(package, *args)
    self.class.run_git(package, *args)
end

#run_git_bare(package, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run a git command that only need a git directory

Parameters:

  • package (Package)
  • args (Array)

    the git arguments, excluding the git command itself. The last argument can be a hash, in which case it is passed as an option hash to Package#run



571
572
573
# File 'lib/autobuild/import/git.rb', line 571

def run_git_bare(package, *args)
    self.class.run_git_bare(package, *args)
end

#setup_remote(package, remote_name, repository, push_to = repository, only_local: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set a remote up in the repositorie’s configuration



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/autobuild/import/git.rb', line 594

def setup_remote(
    package, remote_name, repository, push_to = repository,
    only_local: true
)
    resolve_all_branches(package, only_local: only_local) unless has_all_branches?

    run_git_bare(package, 'config', '--replace-all',
                 "remote.#{remote_name}.url", repository)
    run_git_bare(package, 'config', '--replace-all',
                 "remote.#{remote_name}.pushurl", push_to || repository)
    run_git_bare(package, 'config', '--replace-all',
                 "remote.#{remote_name}.fetch",
                 "+refs/heads/*:refs/remotes/#{remote_name}/*")

    if remote_branch && local_branch
        remote_ref = remote_branch_to_ref(remote_branch)
        run_git_bare(package, 'config', '--replace-all',
                     "remote.#{remote_name}.push",
                     "refs/heads/#{local_branch}:#{remote_ref}")
    else
        run_git_bare(package, 'config', '--replace-all',
                     "remote.#{remote_name}.push", "refs/heads/*:refs/heads/*")
    end
end

#shallow?Boolean

Whether clones should be shallow

Returns:

  • (Boolean)


331
332
333
# File 'lib/autobuild/import/git.rb', line 331

def shallow?
    @shallow
end

#show(package, commit, path) ⇒ String

Returns the file’s conents at a certain commit

Parameters:

  • package (Package)
  • commit (String)
  • path (String)

Returns:

  • (String)


936
937
938
939
940
941
# File 'lib/autobuild/import/git.rb', line 936

def show(package, commit, path)
    run_git_bare(package, 'show', "#{commit}:#{path}").join("\n")
rescue Autobuild::SubcommandFailed
    raise PackageException.new(package, 'import'),
          "failed to either resolve commit #{commit} or file #{path}"
end

#single_branch?Boolean

Whether ‘clone’ should fetch only the remote branch, or all the branches

Returns:

  • (Boolean)


326
327
328
# File 'lib/autobuild/import/git.rb', line 326

def single_branch?
    @single_branch
end

#status(package, options = Hash.new) ⇒ Object

Returns a Status object that represents the status of this package w.r.t. the expected remote repository and branch



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/autobuild/import/git.rb', line 782

def status(package, options = Hash.new)
    only_local =
        if options.kind_of?(Hash)
            options.fetch(:only_local, false)
        else
            options
        end

    validate_importdir(package)
    resolve_all_branches(package, only_local: only_local) unless has_all_branches?

    _pinned_state, target_commit, = determine_target_state(
        package, only_local: only_local)

    status = merge_status(package, target_commit)
    status.uncommitted_code = self.class.has_uncommitted_changes?(package)
    if (current_branch = self.current_branch(package))
        if current_branch != "refs/heads/#{local_branch}"
            status.unexpected_working_copy_state <<
                "working copy is on branch #{current_branch}, "\
                "the autoproj configuration expected it to be on #{local_branch}"
        end
    else
        status.unexpected_working_copy_state <<
            "working copy is on a detached HEAD"
    end
    status
end

#tags(package, options = Hash.new) ⇒ Hash<String,String>

The tags of this packages

Parameters:

  • package (Package)
  • options (Hash) (defaults to: Hash.new)

    a customizable set of options

Options Hash (options):

  • only_local (Boolean) — default: false

    whether the tags should be fetch from the remote first, or if one should only list tags that are already known locally

Returns:

  • (Hash<String,String>)

    a mapping from a tag name to its commit ID



524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/autobuild/import/git.rb', line 524

def tags(package, options = Hash.new)
    unless options.fetch(:only_local, false)
        run_git_bare(package, 'fetch', '--tags')
    end
    tag_list = run_git_bare(package, 'show-ref', '--tags').map(&:strip)
    tags = Hash.new
    tag_list.each do |entry|
        commit_to_tag = entry.split(" ")
        tags[commit_to_tag[1].sub("refs/tags/", "")] = commit_to_tag[0]
    end
    tags
end

#try_resolve_remote_head_from_local(package) ⇒ Object

Return the remote head branch from local copy if exists, if not return nil

Parameters:



352
353
354
355
356
357
358
359
360
# File 'lib/autobuild/import/git.rb', line 352

def try_resolve_remote_head_from_local(package)
    ls_local_string = run_git_bare(
        package, 'symbolic-ref',
        "refs/remotes/#{@remote_name}/HEAD"
    ).first.strip
    local_remote_head = ls_local_string.match("refs/remotes/#{@remote_name}/(.*)")
    local_remote_head ? local_remote_head[1] : nil
rescue Autobuild::SubcommandFailed # rubocop:disable Lint/SuppressedException
end

#try_resolve_remote_head_from_server(package) ⇒ Object

Return the remote head branch from server if exists, if not return ‘master’

Parameters:



365
366
367
368
369
370
371
372
373
# File 'lib/autobuild/import/git.rb', line 365

def try_resolve_remote_head_from_server(package)
    ls_remote_string = package.run(
        :import,
        Autobuild.tool('git'), 'ls-remote', '--symref', repository
    ).first.strip
    server_remote_head =
        ls_remote_string.match("ref:[^A-z]refs/heads/(.*)[^A-z]HEAD")
    server_remote_head ? server_remote_head[1] : 'master'
end

#update(package, options = Hash.new) ⇒ Object



1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
# File 'lib/autobuild/import/git.rb', line 1191

def update(package, options = Hash.new)
    validate_importdir(package)

    resolve_all_branches(package) unless has_all_branches?

    only_local = options.fetch(:only_local, false)
    reset = options.fetch(:reset, false)

    # This is really really a hack to workaround how broken the
    # importdir thing is
    update_alternates(package) if package.importdir == package.srcdir

    pinned_state, target_commit, fetch_commit =
        determine_target_state(package, only_local: only_local)

    did_change_branch = ensure_on_local_branch(package, target_commit)

    # Check whether we are already at the requested state
    if pinned_state
        pin_is_uptodate, pin_did_merge =
            handle_pinned_state(package, pinned_state, reset: reset)
    end

    unless pin_is_uptodate
        fetch_commit ||= current_remote_commit(
            package, only_local: only_local,
                     refspec: [remote_branch_to_ref(remote_branch), tag]
        )
        did_update =
            if reset
                reset_head_to_commit(package, target_commit, fetch_commit,
                                     force: (reset == :force))
            else
                merge_if_simple(package, target_commit)
            end
    end

    if !only_local && with_submodules?
        run_git(package, "submodule", "update", '--init', '--recursive')
        did_update = true
    end

    did_update || pin_did_merge || did_change_branch
end

#update_alternates(package) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Updates the git alternates file in the already checked out package to match #alternates

Parameters:

  • package (Package)

    the already checked-out package



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/autobuild/import/git.rb', line 1067

def update_alternates(package)
    alternates_path = File.join(git_dir(package, false),
                                'objects', 'info', 'alternates')
    current_alternates =
        if File.file?(alternates_path)
            File.readlines(alternates_path)
                .map(&:strip)
                .find_all { |l| !l.empty? }
        else
            Array.new
        end

    alternates = each_alternate_path(package).map do |path|
        File.join(path, 'objects')
    end

    unless (current_alternates.sort - alternates.sort).empty?
        # Warn that something is fishy, but assume that the user knows
        # what he is doing
        package.warn "%s: the list of git alternates listed in the repository "\
            "differs from the one set up in autobuild."
        package.warn "%s: I will update, but that is dangerous"
        package.warn "%s: using git alternates is for advanced users only, "\
            "who know git very well."
        package.warn "%s: Don't complain if something breaks"
    end
    if alternates.empty?
        FileUtils.rm_f alternates_path
    else
        File.open(alternates_path, 'w') do |io|
            io.write alternates.join("\n")
        end
    end
end

#update_remotes_configuration(package, only_local: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates the git repository’s configuration for the target remote



634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/autobuild/import/git.rb', line 634

def update_remotes_configuration(package, only_local: true)
    each_configured_remote do |*args|
        setup_remote(package, *args, only_local: only_local)
    end

    if local_branch
        run_git_bare(package, 'config', '--replace-all',
                     "branch.#{local_branch}.remote", remote_name)
        run_git_bare(package, 'config', '--replace-all',
                     "branch.#{local_branch}.merge",
                     remote_branch_to_ref(local_branch))
    end
end

#uses_lfs?(package) ⇒ Boolean

Returns:

  • (Boolean)


1290
1291
1292
1293
1294
1295
1296
1297
1298
# File 'lib/autobuild/import/git.rb', line 1290

def uses_lfs?(package)
    git_files = run_git(package, 'ls-files').join("\n")
    git_attrs = run_git(
        package, 'check-attr', '--all', '--stdin',
        input_streams: [StringIO.new(git_files)]
    ).join("\n")

    /(.*): filter: lfs/.match(git_attrs)
end

#validate_importdir(package) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Verifies that the package’s Package#importdir points to a git repository



345
346
347
# File 'lib/autobuild/import/git.rb', line 345

def validate_importdir(package)
    git_dir(package, true)
end

#validate_shallow(package) ⇒ Object



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/autobuild/import/git.rb', line 1307

def validate_shallow(package)
    return false unless shallow?

    if commit
        Autoproj.warn "#{package.name}: "\
                      "Cannot pin a commit while doing a shallow clone"
        return false
    end
    if tag && !single_branch?
        Autoproj.warn(
            "#{package.name}: "\
            "Cannot pin a tag while doing a shallow clone " \
            "if single_branch is not set"
        )
        return false
    end
    if @remote_branch
        Autoproj.warn "#{package.name}: "\
                      "Cannot use remote_branch while doing a shallow clone"
        return false
    end
    true
end

#vcs_fingerprint(package) ⇒ Object



220
221
222
# File 'lib/autobuild/import/git.rb', line 220

def vcs_fingerprint(package)
    rev_parse(package, "HEAD", @fingerprint_mode)
end

#with_submodules?Boolean

Whether the git checkout should be done with submodules

Returns:

  • (Boolean)


320
321
322
# File 'lib/autobuild/import/git.rb', line 320

def with_submodules?
    @with_submodules
end