Class: Origen::RevisionControl::DesignSync

Inherits:
Base show all
Defined in:
lib/origen/revision_control/design_sync.rb

Instance Attribute Summary

Attributes inherited from Base

#local, #remote

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dssc?, #git?, #initialize, #svn?

Constructor Details

This class inherits a constructor from Origen::RevisionControl::Base

Class Method Details

.remote_check_in(dir, options = {}) ⇒ Object

Check in the contents of the given directory to a remote vault location, meaning a vault location that is not necessarily associated with the current workspace that the given files are in.

Anything found in the given directory will be checked in, even files which are not currently under revision control.

No attempt will be made to merge the current vault contents with the local data, the local data will always be checked in as latest.

A tag can be optionally supplied and if present will be applied to the files post check in.

Examples:


Origen::RevisionControl::DesignSync.remote_check_in("#{Origen.root}/output/j750", vault: "sync://sync-15088:15088/Projects/common_tester_blocks/origen_training/j750", tag: Origen.app.version)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/origen/revision_control/design_sync.rb', line 18

def self.remote_check_in(dir, options = {})
  options = {
    force: true
  }.merge(options)

  dir = Pathname.new(dir)
  fail "Directory does not exist: #{dir}" unless dir.exist?
  fail "Only directories are supported by remote_check_in, this is not a directory: #{dir}" unless dir.directory?
  fail 'No vault option supplied to remote_check_in!' unless options[:vault]
  scratch = Pathname.new("#{Origen.app.workspace_manager.imports_directory}/design_sync/scratch")
  FileUtils.rm_rf(scratch) if scratch.exist?
  FileUtils.mkdir_p(scratch)
  FileUtils.cp_r("#{dir}/.", scratch)
  remove_dot_syncs!(scratch)
  ds = new(remote: options[:vault], local: scratch)
  ds.checkin(options)
  ds.tag(options[:tag], options) if options[:tag]
  FileUtils.rm_rf(scratch)
end

.remove_dot_syncs!(dir, options = {}) ⇒ Object

Recursively remove all .SYNC directories from the given directory



39
40
41
42
43
44
45
46
# File 'lib/origen/revision_control/design_sync.rb', line 39

def self.remove_dot_syncs!(dir, options = {})
  dir = Pathname.new(dir)
  fail "Directory does not exist: #{dir}" unless dir.exist?
  fail "Only directories are supported by remove_dot_syncs, this is not a directory: #{dir}" unless dir.directory?
  Dir.glob("#{dir}/**/.SYNC").sort.each do |dot_sync|
    FileUtils.rm_rf(dot_sync)
  end
end

Instance Method Details

#build(options = {}) ⇒ Object



48
49
50
# File 'lib/origen/revision_control/design_sync.rb', line 48

def build(options = {})
  checkout(options)
end

#changes(dir = nil, options = {}) ⇒ Object



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
# File 'lib/origen/revision_control/design_sync.rb', line 88

def changes(dir = nil, options = {})
  paths, options = clean_path(dir, options)
  options = {
    verbose: false
  }.merge(options)

  cmd = 'compare -rec -path -report silent'
  if options[:version]
    cmd += " -selector #{prefix_tag(options[:version])}"
  end
  cmd += " #{paths.first}"

  objects = {
    added: [], removed: [], changed: []
  }
  dssc(cmd, options).each do |line|
    # We need to parse the following data from the output, ignore everything else
    # which will mostly refer to un-managed files.
    #
    # Added since previous version...
    # 1.12                                          First only         source_setup
    # Removed since previous version...
    #                        1.13                   Second only        lib/history
    # Modified since previous version...
    # 1.32                   1.31                   Different versions lib/origen/application.rb
    # Modified since previous version including a local edit...
    # 1.7 (Locally Modified) 1.7                    Different states   lib/origen/commands/rc.rb
    unless line =~ /Unmanaged/
      # http://www.rubular.com/r/GoNYB75upB
      if line =~ /\s*(\S+)\s+First only\s+(\S+)\s*/
        objects[:added] << Regexp.last_match[2]
      # http://www.rubular.com/r/Xvh32Lm4hS
      elsif line =~ /\s*(\S+)\s+Second only\s+(\S+)\s*/
        objects[:removed] << Regexp.last_match[2]
      # http://www.rubular.com/r/tvTHod9Mye
      elsif line =~ /\s*\S+\s+(\(Locally Modified\))?\s*(\S+)\s+Different (versions|states)\s+(\S+)\s*/
        objects[:changed] << Regexp.last_match[4]
      end
    end
  end
  objects[:present] = !objects[:added].empty? || !objects[:removed].empty? || !objects[:changed].empty?
  objects[:added].map! { |i| "#{paths.first}/" + i }
  objects[:removed].map! { |i| "#{paths.first}/" + i }
  objects[:changed].map! { |i| "#{paths.first}/" + i }
  objects
end

#checkin(path = nil, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/origen/revision_control/design_sync.rb', line 69

def checkin(path = nil, options = {})
  paths, options = clean_path(path, options)
  cmd = 'ci'
  cmd += ' -rec -keep'
  cmd += ' -skip' if options[:force]
  cmd += ' -new' if options[:unmanaged] || options[:force]
  if options[:comment] && !options[:comment].strip.empty?
    cmd += " -com \"#{options[:comment].strip}\""
  else
    # cmd += ' -nocom' # DO NOT USE nocom option with DesignSync, doesn't always work
    cmd += ' -com None'
  end
  paths = paths.join(' ')
  dssc("#{cmd} #{paths}", options)
  # Make sure the file is still writable
  `chmod a+w -R #{paths}`
  paths
end

#checkout(path = nil, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/origen/revision_control/design_sync.rb', line 52

def checkout(path = nil, options = {})
  paths, options = clean_path(path, options)
  cmd = 'pop'
  cmd += ' -rec -get -uni'
  cmd += ' -force' if options[:force]
  if options[:version]
    cmd += " -version #{prefix_tag(options[:version])}"
  else
    cmd += ' -merge' unless options[:force]
  end
  paths = paths.join(' ')
  dssc("#{cmd} #{paths}", options)
  # Design sync can be funny and even with -get it can leave unwritable files, so let's fix that
  `chmod a+w -R #{paths}`
  paths
end

#current_branchObject



235
236
237
# File 'lib/origen/revision_control/design_sync.rb', line 235

def current_branch
  dssc("url selector #{local}", verbose: false).first
end

#diff_cmd(file, version) ⇒ Object



174
175
176
# File 'lib/origen/revision_control/design_sync.rb', line 174

def diff_cmd(file, version)
  "dssc diff -gui -ver #{prefix_tag(version)} #{file}"
end

#local_modifications(dir = nil, options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/origen/revision_control/design_sync.rb', line 135

def local_modifications(dir = nil, options = {})
  paths, options = clean_path(dir, options)
  options = {
    verbose: false
  }.merge(options)

  cmd = 'ls -rec -managed -path -report N -modified -format text'
  cmd += " #{paths.first}"

  files = dssc(cmd, options).reject do |item|
    item.strip.empty? ||
    item =~ /^(Name|Directory|---)/
  end
  files.map! do |file|
    file.strip!  # Strip off any whitespace from all objects
    file.sub!(/^#{full_path_prefix}/, '')
    file.sub('|', ':')
    file.sub!(/^/, "#{paths.first}/")
  end
end

#rootObject



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
# File 'lib/origen/revision_control/design_sync.rb', line 189

def root
  # This is an expensive operation the way it is currently implemented, so
  # cache the result for future calls
  Origen.app.session.dssc["root-#{local}"] ||= begin
    root = local
    # Create two arrays, one to store the parents and the other to corresponding vaults.
    temp_parent_array = Array.new(0)
    temp_parent_vault_array = Array.new(0)
    resolved = false
    vault = dssc("url vault #{root}").first
    until resolved || root.root?
      parent = root.parent
      # push the current parent into the parent array
      temp_parent_array.push(parent.to_s)
      if File.exist?("#{parent}/.SYNC")
        parent_vault = dssc("url vault #{parent}").first
        # push the current parent_vault into the vault array.
        temp_parent_vault_array.push(parent_vault.to_s)
        if vault.to_s =~ /^#{parent_vault}/ && vault.to_s != parent_vault.to_s
          root = parent
        else
          # Now, check if the parent array has unique values, it should if the DesignSync directory structure is correct.
          if temp_parent_vault_array.uniq.length == temp_parent_vault_array.length
            resolved = true
          else
            # To display the file/directory that the user needs to correct the conflict in, pick up the second last element from the parent array.
            fault_dir = temp_parent_array[-2]
            fault_dir_name = fault_dir.split('/')[-1]
            Origen.log.error 'DesignSync returns same vault locations for two directories.'
            Origen.log.error 'Please resolve the workspace conflicts before continuing'
            # Un-cache the result so that there is no error in future calls.
            Origen.app.session.dssc["root-#{local}"] = nil
            # Remove the .ref symlink from the local directory so that there are no issues in the future call
            scratch = Pathname.new("#{local}/.ref")
            FileUtils.rm_rf(scratch) if scratch.exist?
            abort
          end
        end
      else
        resolved = true
      end
    end
    root
  end
end

#tag(id, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/origen/revision_control/design_sync.rb', line 178

def tag(id, options = {})
  id = VersionString.new(id)
  id = id.prefixed if id.semantic?
  replace = options[:force] ? '-replace' : ''
  dssc "tag #{id} -rec #{replace} *"
  # Applying this rule recursively seems to cause havoc, so running on its own.
  # This hits any dot files in the root directory, any dot files in sub directories
  # already get hit by the above tag job.
  dssc "tag #{id} #{replace} .*"
end

#unmanaged(dir = nil, options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/origen/revision_control/design_sync.rb', line 156

def unmanaged(dir = nil, options = {})
  paths, options = clean_path(dir, options)
  options = {
    verbose: false
  }.merge(options)
  cmd = 'ls -rec -unmanaged -fullpath -report N -modified -format text'
  cmd += " #{paths.first}"
  files = dssc(cmd, options).reject do |item|
    # removes extraneous lines
    item =~ /^(Name|Directory|---)/ || item.strip.empty?
  end
  files.map! do |file|
    file.strip!  # Strip off any whitespace from all objects
    file.sub!(/^#{full_path_prefix}/, '')
    file.sub('|', ':')
  end
end