Class: GithubBackup::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/github_backup/git.rb

Instance Method Summary collapse

Instance Method Details

#commits_ahead(remote, lbranch, rbranch) ⇒ Object



25
26
27
# File 'lib/github_backup/git.rb', line 25

def commits_ahead(remote, lbranch, rbranch)
  `git rev-list --count refs/remotes/#{remote}/#{rbranch}..refs/heads/#{lbranch}`.chomp.to_i
end

#commits_behind(remote, lbranch, rbranch) ⇒ Object



21
22
23
# File 'lib/github_backup/git.rb', line 21

def commits_behind(remote, lbranch, rbranch)
  `git rev-list --count refs/heads/#{lbranch}..refs/remotes/#{remote}/#{rbranch}`.chomp.to_i
end

#current_branchObject



10
11
12
# File 'lib/github_backup/git.rb', line 10

def current_branch
  `git rev-parse --abbrev-ref HEAD`.chomp
end

#remotes(_remotes = nil) ⇒ Object



3
4
5
6
7
8
# File 'lib/github_backup/git.rb', line 3

def remotes(_remotes = nil)
  unless _remotes
    _remotes = `git remote`.split(/\s+/)
  end
  _remotes
end

#tracked_branches(remote) ⇒ Object



14
15
16
17
18
19
# File 'lib/github_backup/git.rb', line 14

def tracked_branches(remote)
  info = `git remote show #{remote}`
    .lines
    .select { |l| l =~ /merges with remote/i }
    .map{ |l| l.strip.split(/\s+merges with remote\s+/i) }
end

#updateObject

updater =



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
# File 'lib/github_backup/git.rb', line 32

def update
  remotes.each do |remote|
    puts `git remote update #{remote}`
    tracked_branches(remote).each do |branches|
      lbranch, rbranch = branches
      behind = commits_behind(remote, lbranch, rbranch)
      ahead = commits_ahead(remote, lbranch, rbranch)
      if behind > 0
        if ahead > 0
          puts "branch #{lbranch} is #{behind} commit(s) behind and " \
               "#{ahead} commit(s) ahead of #{remote}/#{rbranch}. " \
               "could not be fast-forwarded"
        elsif lbranch == current_branch
          puts "branch #{lbranch} was #{behind} commit(s) behind of " \
               "#{remote}/#{rbranch}. fast-forward merge"
          puts "git merge -q refs/remotes/#{remote}/#{rbranch}"
        else
          puts "branch #{lbranch} was #{behind} commit(s) behind of " \
               "#{remote}/#{rbranch}. resetting local branch to remote"
          puts "git branch -f #{lbranch} -t refs/remotes/#{remote}/#{rbranch} >/dev/null"
        end
      end
    end
  end
end