Class: TestIds::Git::Rollback

Inherits:
Object
  • Object
show all
Includes:
Origen::Utility::InputCapture
Defined in:
lib/test_ids/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Rollback

Returns a new instance of Rollback.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/test_ids/git.rb', line 20

def initialize(id)
  repos = Dir.glob("#{Origen.app.imports_dir}/test_ids/*.git")
  if repos.size == 0
    puts 'No TestIds repositories were found in this application'
  elsif repos.size > 1
    puts
    puts 'Multiple TestIDs repositories found, select the one to rollback:'
    puts
    repos.each_with_index do |repo, i|
      puts "  #{i} - #{Pathname.new(repo).basename}"
    end
    accept = repos.map.with_index { |r, i| i }
    puts
    selection = repos.size + 1
    until repos[selection]
      selection = get_text(single: true, accept: accept).to_i
    end
  else
    selection = 0
  end
  name = Pathname.new(repos[selection]).basename.to_s
  repo = ::Git.open(repos[selection])
  begin
    commit = repo.object(id)
  rescue ::Git::GitExecuteError
    puts 'The given commit ID cannot be found in that repository'
    exit
  end
  day = 24 * 60 * 60
  if commit.date < Time.now - (7 * day)
    puts "Sorry, that commit is more than a week old and I'm too scared to rollback that far."
    puts 'You will need to do that manually if you must.'
    exit
  end
  puts
  puts "About to rollback the TestIds repository #{name} to commit #{id}."
  puts
  puts 'This will permanently delete any IDs assigned by anyone, anywhere, since that commit.'
  puts
  puts 'ARE YOU SURE YOU KNOW WHAT YOU ARE DOING?'
  puts
  get_text(confirm: true, default: 'no')
  repo.reset_hard(id)
  repo.push('origin', 'master', force: true)
  puts 'As you wish, rolled back successfully!'
end