Class: Six::Updater::GitRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/six/updater/gitrepo.rb

Instance Method Summary collapse

Constructor Details

#initialize(repositories, path, depth = 1) ⇒ GitRepo

Returns a new instance of GitRepo.



6
7
8
9
10
11
# File 'lib/six/updater/gitrepo.rb', line 6

def initialize(repositories, path, depth = 1)
  @repositories = repositories
  @path = path
  @depth = depth
  Six::Updater.setup_git
end

Instance Method Details

#check_status(type, array, msg) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/six/updater/gitrepo.rb', line 93

def check_status(type, array, msg)
  str = []
  if array.size > 0
    str << "#{type}:"
    array.each { |item| str << item[0] }
    msg << str.join("\n")
  end
end

#clone(opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/six/updater/gitrepo.rb', line 29

def clone(opts = {})
  repositories = @repositories.clone
  done = false
  opts[:depth] = @depth
  while repositories.size > 0 && !done
    rep = repositories.sample
    repositories -= [rep]
    log.info "Trying #{rep}"

    begin
      Git.clone(rep, @path, opts)
      Git.open(@path).config("core.autocrlf", "false")
      done = true
    rescue => e
      log.debug "WARNING: Failed, trying other mirrors if available"
      log.debug "ERROR: #{e.class} #{e.message} #{e.backtrace.join("\n")}"
    end
  end
  log.warn "WARNING: Failed, was unable to clone!" unless done
  done
end

#logObject



21
22
23
# File 'lib/six/updater/gitrepo.rb', line 21

def log
  Six::Updater.log
end

#openObject



13
14
15
# File 'lib/six/updater/gitrepo.rb', line 13

def open
  repository
end

#repositoryObject



17
18
19
# File 'lib/six/updater/gitrepo.rb', line 17

def repository
  @repository || Git.open(@path)
end

#reset(opts = {}) ⇒ Object



25
26
27
# File 'lib/six/updater/gitrepo.rb', line 25

def reset(opts = {})
  repository.reset(nil, opts)
end

#status(force = false) ⇒ Object



86
87
88
89
90
91
# File 'lib/six/updater/gitrepo.rb', line 86

def status(force = false)
  if force || !@status
    @status = status_update
  end
  @status
end

#status_updateObject



102
103
104
105
106
107
108
109
# File 'lib/six/updater/gitrepo.rb', line 102

def status_update
  msg = []
  [
    ['Added', repository.status.added], ['Changed', repository.status.changed],
    ['Deleted', repository.status.deleted], ['Untracked', repository.status.untracked]
  ].each { |entry| check_status(entry[0], entry[1], msg) }
  msg
end

#update(opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/six/updater/gitrepo.rb', line 55

def update(opts = {})
  done = false
  repositories = @repositories.clone
  while repositories.size > 0 && !done
    cfg = nil
    File.open(File.join(@path, '.git/config')) { |file| cfg = file.readlines }
    rep = ''
    cfg.map! do |entry|
      if entry[/(\t*)url \=/]
        rep = repositories.sample
        repositories -= [rep]
        "#{$1}url = #{rep}"
      else
        entry
      end
    end
    log.info "Trying #{rep}"
    File.open(File.join(@path, '.git/config'), 'w') { |file| file.puts cfg }
    begin
      repository.config("core.autocrlf", "false")
      repository.pull_legacy
      done = true
    rescue => e
      log.debug "WARNING: Failed, trying other mirrors if available"
      log.debug "ERROR: #{e.class} #{e.message} #{e.backtrace.join("\n")}"
    end
  end
  log.warn "WARNING: Failed, was unable to update!" unless done
  done
end

#versionObject



51
52
53
# File 'lib/six/updater/gitrepo.rb', line 51

def version
  repository.log.first.to_s
end