Class: Autobuild::SVN

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

Instance Attribute Summary collapse

Attributes inherited from Importer

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

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(svnroot, options = {}) ⇒ SVN

Creates an importer which gets the source for the Subversion URL source. The following options are allowed:

:svnup

options to give to ‘svn up’

:svnco

options to give to ‘svn co’

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

Autobuild.programs['svn'] = 'my_svn_tool'


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/autobuild/import/svn.rb', line 15

def initialize(svnroot, options = {})
    svnroot = [*svnroot].join("/")
    svnopts, common = Kernel.filter_options(
        options,
        :svnup => [], :svnco => [], :revision => nil,
        :repository_id => "svn:#{svnroot}"
    )
    common[:repository_id] = svnopts.delete(:repository_id)
    relocate(svnroot, svnopts)
    super(common.merge(repository_id: svnopts[:repository_id]))
end

Instance Attribute Details

#revisionObject (readonly)

Returns the value of attribute revision.



46
47
48
# File 'lib/autobuild/import/svn.rb', line 46

def revision
  @revision
end

#svnrootString (readonly)

Returns the SVN root

Returns:

  • (String)


37
38
39
# File 'lib/autobuild/import/svn.rb', line 37

def svnroot
  @svnroot
end

Instance Method Details

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

:nodoc:



249
250
251
252
253
# File 'lib/autobuild/import/svn.rb', line 249

def checkout(package, _options = Hash.new) # :nodoc:
    run_svn(package, 'co', "--non-interactive", *@options_co,
            svnroot, package.importdir,
            working_directory: nil)
end

#has_local_modifications?(package, with_untracked_files = false) ⇒ Boolean

Returns true if the SVN working copy at package.importdir has local modifications

Parameters:

  • package (Package)

    the package we want to test against

  • with_untracked_files (Boolean) (defaults to: false)

    if true, the presence of files neither ignored nor under version control will count has local modification.

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/autobuild/import/svn.rb', line 114

def has_local_modifications?(package, with_untracked_files = false)
    status = run_svn(package, 'status', '--xml')

    not_modified = %w[external ignored none normal]
    not_modified << "unversioned" unless with_untracked_files

    REXML::Document.new(status.join("")).
        elements.enum_for(:each, '//wc-status').
        any? do |status_item|
            !not_modified.include?(status_item.attributes['item'].to_s)
        end
end

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



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

def relocate(root, options = Hash.new)
    @svnroot = [*root].join("/")
    @options_up = [*options[:svnup]]
    @options_co = [*options[:svnco]]
    @revision = options[:revision]
    if revision
        @options_co << '--revision' << revision
        # We do not add it to @options_up as the behaviour depends on
        # the parameters given to {update} and to the state of the
        # working copy
    end
end

#repositoryObject

Alias for #svnroot

For consistency with the other importers



42
43
44
# File 'lib/autobuild/import/svn.rb', line 42

def repository
    svnroot
end

#run_svn(package, *args, &block) ⇒ Object

Helper method to run a SVN command on a package’s working copy



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/autobuild/import/svn.rb', line 164

def run_svn(package, *args, &block)
    options =
        if args.last.kind_of?(Hash)
            args.pop
        else
            Hash.new
        end

    options, other_options = Kernel.filter_options(
        options, working_directory: package.importdir, retry: true
    )
    options = options.merge(other_options)
    package.run(:import, Autobuild.tool(:svn), *args, options, &block)
end

#sourceString

Deprecated.

use #svnroot instead

Returns:

  • (String)


30
31
32
# File 'lib/autobuild/import/svn.rb', line 30

def source
    svnroot
end

#status(package, only_local = false) ⇒ Package::Status

Returns status information for package

Given that subversion is not a distributed VCS, the only status returned are either Importer::Status::UP_TO_DATE or Importer::Status::SIMPLE_UPDATE. Moreover, if the status is local-only, Package::Status#remote_commits will not be filled (querying the log requires accessing the SVN server)

Returns:

  • (Package::Status)


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

def status(package, only_local = false)
    status = Status.new
    status.uncommitted_code = has_local_modifications?(package)
    if only_local
        status.status = Status::UP_TO_DATE
    else
        log = run_svn(package, 'log', '-r', 'BASE:HEAD', '--xml', '.')
        log = REXML::Document.new(log.join("\n"))
        missing_revisions = log.elements.enum_for(:each, 'log/logentry').
            map do |l|
                rev = l.attributes['revision']
                date = l.elements['date'].first.to_s
                author = l.elements['author'].first.to_s
                msg = l.elements['msg'].first.to_s.split("\n").first
                "#{rev} #{DateTime.parse(date)} #{author} #{msg}"
            end
        status.remote_commits = missing_revisions[1..-1]
        status.status =
            if missing_revisions.empty?
                Status::UP_TO_DATE
            else
                Status::SIMPLE_UPDATE
            end
    end
    status
end

#svn_info(package) ⇒ Array<String>

Returns the result of the ‘svn info’ command

It automatically runs svn upgrade if needed

Parameters:

Returns:

  • (Array<String>)

    the lines returned by svn info, with the trailing newline removed



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/svn.rb', line 195

def svn_info(package)
    old_lang = ENV['LC_ALL']
    ENV['LC_ALL'] = 'C'
    begin
        svninfo = run_svn(package, 'info')
    rescue SubcommandFailed => e
        if e.output.find { |l| l =~ /svn upgrade/ }
            # Try svn upgrade and info again
            run_svn(package, 'upgrade', retry: false)
            svninfo = run_svn(package, 'info')
        else
            raise
        end
    end

    unless svninfo.grep(/is not a working copy/).empty?
        raise ConfigException.new(package, 'import'),
              "#{package.importdir} does not appear to be a "\
              "Subversion working copy"
    end
    svninfo
ensure
    ENV['LC_ALL'] = old_lang
end

#svn_revision(package) ⇒ Integer

Returns the SVN revision of the package

Parameters:

Returns:

  • (Integer)


67
68
69
70
71
72
73
74
75
76
# File 'lib/autobuild/import/svn.rb', line 67

def svn_revision(package)
    svninfo = svn_info(package)
    revision = svninfo.grep(/^Revision: /).first
    unless revision
        raise ConfigException.new(package, 'import'),
              "cannot get SVN information for #{package.importdir}"
    end
    revision =~ /Revision: (\d+)/
    Integer($1)
end

#svn_url(package) ⇒ String

Returns the URL of the remote SVN repository

Parameters:

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
# File 'lib/autobuild/import/svn.rb', line 95

def svn_url(package)
    svninfo = svn_info(package)
    url = svninfo.grep(/^URL: /).first
    unless url
        raise ConfigException.new(package, 'import'),
              "cannot get SVN information for #{package.importdir}"
    end
    url.chomp =~ /URL: (.+)/
    $1
end

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

:nodoc:



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

def update(package, options = Hash.new) # :nodoc:
    if options[:only_local]
        package.warn "%s: the svn importer does not support local updates, "\
            "skipping"
        return false
    end

    url = svn_url(package)
    if url != svnroot
        raise ConfigException.new(package, 'import'), "current checkout "\
            "found at #{package.importdir} is from #{url}, "\
            "was expecting #{svnroot}"
    end

    options_up = @options_up.dup
    if revision
        if options[:reset] || svn_revision(package) < revision
            options_up << '--revision' << revision
        elsif revision
            # Don't update if the current revision is greater-or-equal
            # than the target revision
            return false
        end
    end

    run_svn(package, 'up', "--non-interactive", *options_up)
    true
end

#validate_importdir(package) ⇒ Object



179
180
181
182
183
# File 'lib/autobuild/import/svn.rb', line 179

def validate_importdir(package)
    # This upgrades the local SVN filesystem if needed and checks that
    # it actually is a SVN repository in the first place
    svn_info(package)
end

#vcs_fingerprint(package) ⇒ String

fingerprint method returns an unique hash to identify this package, for SVN the revision and URL will be used

Parameters:

Returns:

  • (String)


83
84
85
86
87
# File 'lib/autobuild/import/svn.rb', line 83

def vcs_fingerprint(package)
    Digest::SHA1.hexdigest(
        svn_info(package).grep(/^(URL|Revision):/).sort.join("\n")
    )
end