Class: Migrator

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/migrator.rb

Defined Under Namespace

Classes: MigrationNeededError, MigratorDifferentTapsError, MigratorNoOldnameError, MigratorNoOldpathError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formula, force: ARGV.force?) ⇒ Migrator

Returns a new instance of Migrator



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
# File 'brew/Library/Homebrew/migrator.rb', line 111

def initialize(formula, force: ARGV.force?)
  @oldname = formula.oldname
  @newname = formula.name
  raise MigratorNoOldnameError, formula unless oldname

  @formula = formula
  @old_cellar = HOMEBREW_CELLAR/formula.oldname
  raise MigratorNoOldpathError, formula unless old_cellar.exist?

  @old_tabs = old_cellar.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
  @old_tap = old_tabs.first.tap

  raise MigratorDifferentTapsError.new(formula, old_tap) if !force && !from_same_tap_user?

  @new_cellar = HOMEBREW_CELLAR/formula.name
  @new_cellar_existed = @new_cellar.exist?

  if @old_linked_keg = linked_old_linked_keg
    @old_linked_keg_record = old_linked_keg.linked_keg_record if old_linked_keg.linked?
    @old_opt_record = old_linked_keg.opt_record if old_linked_keg.optlinked?
    @new_linked_keg_record = HOMEBREW_CELLAR/"#{newname}/#{File.basename(old_linked_keg)}"
  end

  @old_pin_record = HOMEBREW_PINNED_KEGS/oldname
  @new_pin_record = HOMEBREW_PINNED_KEGS/newname
  @pinned = old_pin_record.symlink?
  @old_pin_link_record = old_pin_record.readlink if @pinned
end

Instance Attribute Details

#formulaObject (readonly)

instance of new name formula



45
46
47
# File 'brew/Library/Homebrew/migrator.rb', line 45

def formula
  @formula
end

#new_cellarObject (readonly)

path to newname cellar according to new name



78
79
80
# File 'brew/Library/Homebrew/migrator.rb', line 78

def new_cellar
  @new_cellar
end

#new_cellar_existedObject (readonly)

true if new cellar existed at initialization time



81
82
83
# File 'brew/Library/Homebrew/migrator.rb', line 81

def new_cellar_existed
  @new_cellar_existed
end

#new_linked_keg_recordObject (readonly)

path to newname keg that will be linked if old_linked_keg isn’t nil



87
88
89
# File 'brew/Library/Homebrew/migrator.rb', line 87

def new_linked_keg_record
  @new_linked_keg_record
end

#new_pin_recordObject (readonly)

path to newname pin



84
85
86
# File 'brew/Library/Homebrew/migrator.rb', line 84

def new_pin_record
  @new_pin_record
end

#newnameObject (readonly)

new name of the formula



75
76
77
# File 'brew/Library/Homebrew/migrator.rb', line 75

def newname
  @newname
end

#old_cellarObject (readonly)

path to oldname’s cellar



51
52
53
# File 'brew/Library/Homebrew/migrator.rb', line 51

def old_cellar
  @old_cellar
end

#old_linked_kegObject (readonly)

oldname linked keg



60
61
62
# File 'brew/Library/Homebrew/migrator.rb', line 60

def old_linked_keg
  @old_linked_keg
end

#old_linked_keg_recordObject (readonly)

path to oldname’s linked keg



63
64
65
# File 'brew/Library/Homebrew/migrator.rb', line 63

def old_linked_keg_record
  @old_linked_keg_record
end

#old_opt_recordObject (readonly)

path to oldname opt



57
58
59
# File 'brew/Library/Homebrew/migrator.rb', line 57

def old_opt_record
  @old_opt_record
end

resolved path to oldname pin



72
73
74
# File 'brew/Library/Homebrew/migrator.rb', line 72

def old_pin_link_record
  @old_pin_link_record
end

#old_pin_recordObject (readonly)

path to oldname pin



54
55
56
# File 'brew/Library/Homebrew/migrator.rb', line 54

def old_pin_record
  @old_pin_record
end

#old_tabsObject (readonly)

tabs from oldname kegs



66
67
68
# File 'brew/Library/Homebrew/migrator.rb', line 66

def old_tabs
  @old_tabs
end

#old_tapObject (readonly)

tap of the old name



69
70
71
# File 'brew/Library/Homebrew/migrator.rb', line 69

def old_tap
  @old_tap
end

#oldnameObject (readonly)

old name of the formula



48
49
50
# File 'brew/Library/Homebrew/migrator.rb', line 48

def oldname
  @oldname
end

Class Method Details

.migrate_if_needed(formula) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'brew/Library/Homebrew/migrator.rb', line 100

def self.migrate_if_needed(formula)
  return unless Migrator.needs_migration?(formula)

  begin
    migrator = Migrator.new(formula)
    migrator.migrate
  rescue => e
    onoe e
  end
end

.needs_migration?(formula) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
# File 'brew/Library/Homebrew/migrator.rb', line 89

def self.needs_migration?(formula)
  oldname = formula.oldname
  return false unless oldname

  oldname_rack = HOMEBREW_CELLAR/oldname
  return false if oldname_rack.symlink?
  return false unless oldname_rack.directory?

  true
end

Instance Method Details

#backup_old_tabsObject



416
417
418
# File 'brew/Library/Homebrew/migrator.rb', line 416

def backup_old_tabs
  old_tabs.each(&:write)
end

#backup_oldnameObject

Backup everything if errors occurred while migrating.



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'brew/Library/Homebrew/migrator.rb', line 372

def backup_oldname
  unlink_oldname_opt
  unlink_oldname_cellar
  backup_oldname_cellar
  backup_old_tabs

  if pinned? && !old_pin_record.symlink?
    src_oldname = (old_pin_record.dirname/old_pin_link_record).expand_path
    old_pin_record.make_relative_symlink(src_oldname)
    new_pin_record.delete
  end

  if new_cellar.exist?
    new_cellar.subdirs.each do |d|
      newname_keg = Keg.new(d)
      newname_keg.unlink
      newname_keg.uninstall if new_cellar_existed
    end
  end

  return if old_linked_keg.nil?

  # The keg used to be linked and when we backup everything we restore
  # Cellar/oldname, the target also gets restored, so we are able to
  # create a keg using its old path
  if old_linked_keg_record
    begin
      old_linked_keg.link
    rescue Keg::LinkError
      old_linked_keg.unlink
      raise
    rescue Keg::AlreadyLinkedError
      old_linked_keg.unlink
      retry
    end
  else
    old_linked_keg.optlink
  end
end

#backup_oldname_cellarObject



412
413
414
# File 'brew/Library/Homebrew/migrator.rb', line 412

def backup_oldname_cellar
  FileUtils.mv(new_cellar, old_cellar) unless old_cellar.exist?
end

#fix_tabsObject

Fix INSTALL_RECEIPTs for tap-migrated formula.



141
142
143
144
145
146
# File 'brew/Library/Homebrew/migrator.rb', line 141

def fix_tabs
  old_tabs.each do |tab|
    tab.tap = formula.tap
    tab.write
  end
end

#from_same_tap_user?Boolean

Returns:

  • (Boolean)


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
# File 'brew/Library/Homebrew/migrator.rb', line 148

def from_same_tap_user?
  formula_tap_user = formula.tap.user if formula.tap
  old_tap_user = nil

  new_tap = if old_tap
    old_tap_user, = old_tap.user
    if migrate_tap = old_tap.tap_migrations[formula.oldname]
      new_tap_user, new_tap_repo = migrate_tap.split("/")
      "#{new_tap_user}/#{new_tap_repo}"
    end
  end

  if formula_tap_user == old_tap_user
    true
  # Homebrew didn't use to update tabs while performing tap-migrations,
  # so there can be INSTALL_RECEIPTs containing wrong information about tap,
  # so we check if there is an entry about oldname migrated to tap and if
  # newname's tap is the same as tap to which oldname migrated, then we
  # can perform migrations and the taps for oldname and newname are the same.
  elsif formula.tap && old_tap && formula.tap == new_tap
    fix_tabs
    true
  else
    false
  end
end


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'brew/Library/Homebrew/migrator.rb', line 282

def link_newname
  oh1 "Relinking #{Formatter.identifier(newname)}"
  new_keg = Keg.new(new_linked_keg_record)

  # If old_keg wasn't linked then we just optlink a keg.
  # If old keg wasn't optlinked and linked, we don't call this method at all.
  # If formula is keg-only we also optlink it.
  if formula.keg_only? || !old_linked_keg_record
    begin
      new_keg.optlink
    rescue Keg::LinkError => e
      onoe "Failed to create #{formula.opt_prefix}"
      raise
    end
    return
  end

  new_keg.remove_linked_keg_record if new_keg.linked?

  begin
    mode = OpenStruct.new(overwrite: true)
    new_keg.link(mode)
  rescue Keg::ConflictError => e
    onoe "Error while executing `brew link` step on #{newname}"
    puts e
    puts
    puts "Possible conflicting files are:"
    mode = OpenStruct.new(dry_run: true, overwrite: true)
    new_keg.link(mode)
    raise
  rescue Keg::LinkError => e
    onoe "Error while linking"
    puts e
    puts
    puts "You can try again using:"
    puts "  brew link #{formula.name}"
  rescue Exception => e # rubocop:disable Lint/RescueException
    onoe "An unexpected error occurred during linking"
    puts e
    puts e.backtrace if ARGV.debug?
    ignore_interrupts { new_keg.unlink }
    raise
  end
end

Remove old_cellar if it exists



358
359
360
361
# File 'brew/Library/Homebrew/migrator.rb', line 358

def link_oldname_cellar
  old_cellar.delete if old_cellar.symlink? || old_cellar.exist?
  old_cellar.make_relative_symlink(formula.rack)
end

Link keg to opt if it was linked before migrating.



328
329
330
331
332
333
# File 'brew/Library/Homebrew/migrator.rb', line 328

def link_oldname_opt
  return unless old_opt_record

  old_opt_record.delete if old_opt_record.symlink?
  old_opt_record.make_relative_symlink(new_linked_keg_record)
end

#linked_old_linked_kegObject



175
176
177
178
179
180
181
# File 'brew/Library/Homebrew/migrator.rb', line 175

def linked_old_linked_keg
  keg_dirs = []
  keg_dirs += new_cellar.subdirs if new_cellar.exist?
  keg_dirs += old_cellar.subdirs
  kegs = keg_dirs.map { |d| Keg.new(d) }
  kegs.find(&:linked?) || kegs.find(&:optlinked?)
end

#lockObject



420
421
422
423
424
425
# File 'brew/Library/Homebrew/migrator.rb', line 420

def lock
  @newname_lock = FormulaLock.new newname
  @oldname_lock = FormulaLock.new oldname
  @newname_lock.lock
  @oldname_lock.lock
end

#migrateObject



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
# File 'brew/Library/Homebrew/migrator.rb', line 187

def migrate
  oh1 "Processing #{Formatter.identifier(oldname)} formula rename to #{Formatter.identifier(newname)}"
  lock
  unlink_oldname
  unlink_newname if new_cellar.exist?
  repin
  move_to_new_directory
  link_oldname_cellar
  link_oldname_opt
  link_newname unless old_linked_keg.nil?
  update_tabs
  return unless formula.outdated?

  opoo <<~EOS
    #{Formatter.identifier(newname)} is outdated!
    To avoid broken installations, as soon as possible please run:
      brew upgrade
    Or, if you're OK with a less reliable fix:
      brew upgrade #{newname}
  EOS
rescue Interrupt
  ignore_interrupts { backup_oldname }
rescue Exception => e # rubocop:disable Lint/RescueException
  onoe "Error occurred while migrating."
  puts e
  puts e.backtrace if ARGV.debug?
  puts "Backing up..."
  ignore_interrupts { backup_oldname }
ensure
  unlock
end

#move_to_new_directoryObject

move everything from Cellar/oldname to Cellar/newname



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
# File 'brew/Library/Homebrew/migrator.rb', line 220

def move_to_new_directory
  return unless old_cellar.exist?

  if new_cellar.exist?
    conflicted = false
    old_cellar.each_child do |c|
      next unless (new_cellar/c.basename).exist?

      begin
        FileUtils.rm_rf c
      rescue Errno::EACCES
        conflicted = true
        onoe "#{new_cellar/c.basename} already exists."
      end
    end

    odie "Remove #{new_cellar} manually and run `brew migrate #{oldname}`." if conflicted
  end

  oh1 "Moving #{Formatter.identifier(oldname)} versions to #{new_cellar}"
  if new_cellar.exist?
    FileUtils.mv(old_cellar.children, new_cellar)
  else
    FileUtils.mv(old_cellar, new_cellar)
  end
end

#pinned?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'brew/Library/Homebrew/migrator.rb', line 183

def pinned?
  @pinned
end

#repinObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'brew/Library/Homebrew/migrator.rb', line 247

def repin
  return unless pinned?

  # old_pin_record is a relative symlink and when we try to to read it
  # from <dir> we actually try to find file
  # <dir>/../<...>/../Cellar/name/version.
  # To repin formula we need to update the link thus that it points to
  # the right directory.
  # NOTE: old_pin_record.realpath.sub(oldname, newname) is unacceptable
  # here, because it resolves every symlink for old_pin_record and then
  # substitutes oldname with newname. It breaks things like
  # Pathname#make_relative_symlink, where Pathname#relative_path_from
  # is used to find relative path from source to destination parent and
  # it assumes no symlinks.
  src_oldname = (old_pin_record.dirname/old_pin_link_record).expand_path
  new_pin_record.make_relative_symlink(src_oldname.sub(oldname, newname))
  old_pin_record.delete
end


274
275
276
277
278
279
280
# File 'brew/Library/Homebrew/migrator.rb', line 274

def unlink_newname
  oh1 "Temporarily unlinking #{Formatter.identifier(newname)}"
  new_cellar.subdirs.each do |d|
    keg = Keg.new(d)
    keg.unlink
  end
end


266
267
268
269
270
271
272
# File 'brew/Library/Homebrew/migrator.rb', line 266

def unlink_oldname
  oh1 "Unlinking #{Formatter.identifier(oldname)}"
  old_cellar.subdirs.each do |d|
    keg = Keg.new(d)
    keg.unlink
  end
end

Remove Cellar/oldname link if it belongs to newname.



364
365
366
367
368
369
# File 'brew/Library/Homebrew/migrator.rb', line 364

def unlink_oldname_cellar
  if (old_cellar.symlink? && !old_cellar.exist?) || (old_cellar.symlink? \
        && formula.rack.exist? && formula.rack.realpath == old_cellar.realpath)
    old_cellar.unlink
  end
end

Remove opt/oldname link if it belongs to newname.



346
347
348
349
350
351
352
353
354
355
# File 'brew/Library/Homebrew/migrator.rb', line 346

def unlink_oldname_opt
  return unless old_opt_record

  if old_opt_record.symlink? && old_opt_record.exist? \
      && new_linked_keg_record.exist? \
      && new_linked_keg_record.realpath == old_opt_record.realpath
    old_opt_record.unlink
    old_opt_record.parent.rmdir_if_possible
  end
end

#unlockObject



427
428
429
430
# File 'brew/Library/Homebrew/migrator.rb', line 427

def unlock
  @newname_lock.unlock
  @oldname_lock.unlock
end

#update_tabsObject

After migtaion every INSTALL_RECEIPT.json has wrong path to the formula so we must update INSTALL_RECEIPTs



337
338
339
340
341
342
343
# File 'brew/Library/Homebrew/migrator.rb', line 337

def update_tabs
  new_tabs = new_cellar.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
  new_tabs.each do |tab|
    tab.source["path"] = formula.path.to_s if tab.source["path"]
    tab.write
  end
end