StatusMonitor.git

in www/status/monitors/git.rb [18:138]


def StatusMonitor.git(previous_status)
  logdir = File.expand_path('../../../logs', __FILE__)
  log = File.join(logdir, 'git-pull')

  archive = File.join(logdir, 'archive')
  FileUtils.mkdir(archive) unless File.directory?(archive)

  
  if __FILE__ == $0 
    fdata = DATA.read
  else
    fdata = File.open(log, 'r:UTF-8') do |file|
      file.flock(File::LOCK_EX)
      file.read
    end
  end

  updates = fdata.split(%r{\n(?:/\w+)*/srv/git/})[1..-1] || [] 

  status = {}
  seen_level = {}

  
  updates.each do |update|
    show 'update', update
    level = 'success'
    title = nil
    data = revision = update[/^(Already up-to-date.|Updating [0-9a-f]+\.\.[0-9a-f]+|HEAD is now at [0-9a-f]+.+)$/]
    title = update[SUMMARY_RE]
    show 'data', data

    lines = update.split("\n")
    repository = lines.shift.to_sym
    show 'repository', repository

    start_ignores = [
      'Already ',
      'Your branch is up-to-date with',
      'Your branch is up to date with',
      'Your branch is behind',
      '  (use "git pull" ',
      'Fast-forward',
      'Updating ',
      ' create mode ',
      ' delete mode ',
      ' rename ',
      ' mode change ',
      'HEAD is now at ',
      
      'From git://',
      ' * [new branch]',
      ' * [new tag]',
      'From https://',
      'Auto packing the repository',
      'See "git help gc" for manual housekeeping',
    ]

    lines.reject! do |line|
      line.start_with?(*start_ignores) or
      line =~ SUMMARY_RE or
      line =~  /^ +[0-9a-f]+\.\.[0-9a-f]+ +\S+ +-> \S+$/ or 
      line =~  /^ +\+ [0-9a-f]+\.\.\.[0-9a-f]+ +\S+ +-> \S+ +\(forced update\)$/ 
    end

    unless lines.empty?
      level = 'info'
      data = lines.dup
    end

    
    lines.reject! {|line|
      
      line =~  /^ \S+ +\| +\d+/ or
      
      line =~  /^ \S+ => \S+ +\| +\d+/ or
      
      line =~  /^ \S+ => \S+ +\| Bin/ or
      
      line =~  /^ \S+ +\| Bin \d+ -> \d+ bytes/
    }

    show 'lines', lines
    if lines.empty?
      if not data
        title = 'partial response'
        level = 'warning'
        seen_level[level] = true
      elsif data.is_a? String
        title = 'No files updated'
      end

      data << revision if revision and data.instance_of? Array
    else
      level = 'danger'
      data = lines.dup
      title = nil
      seen_level[level] = true
    end

    status[repository] = {level: level, data: data, href: '../logs/git-pull'}
    status[repository][:title] = title if title
  end

  
  %w{danger warning}.each do |lvl|
    if seen_level[lvl]
      
      file = File.basename(log)
      if __FILE__ == $0 
        puts 'Would copy log to ' + File.join(archive, file + '.' + lvl)
      else
        FileUtils.copy log, File.join(archive, file + '.' + lvl), preserve: true
      end
      break
    end
  end

  status[:empty] = {level: 'success', data: updates} if status.empty? 
  {data: status}
end