Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
only pull if necessary
  • Loading branch information
mruzicka committed May 4, 2012
1 parent 1403361 commit c71af21
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
61 changes: 61 additions & 0 deletions modules/federated_wiki/files/git_need_pull.rb
@@ -0,0 +1,61 @@
def get_git_command_output(*command)
command.insert(0, %q{git})
IO.popen('-') do |io|
if io.nil?
exec(*command)
else
io.read()
end
end.chomp()
end

def get_config_value(key)
value = get_git_command_output(%q{config}, key)
return nil unless $?.exitstatus() == 0
value
end

def get_commit_id(ref_spec)
commit_id = get_git_command_output(%q{rev-parse}, ref_spec)
return nil unless $?.exitstatus() == 0
commit_id
end

def get_branch_name(branch_ref)
return nil unless branch_ref.start_with?('refs/heads/')
branch_ref.slice!(0..10)
branch_ref.chomp!()
branch_ref
end

# find out the local branch
local_ref = get_git_command_output(*%w{symbolic-ref --quiet HEAD})
exit(1) unless $?.exitstatus() == 0
local_branch = get_branch_name(local_ref)
exit(1) if local_branch.nil?

# find out the remote branch
remote = get_config_value("branch.#{local_branch}.remote")
exit(1) if remote.nil?

remote_branch_ref = get_config_value("branch.#{local_branch}.merge")
exit(1) if remote_branch_ref.nil?
remote_branch = get_branch_name(remote_branch_ref)
exit(1) if remote_branch.nil?

# update the remote references
exit(1) unless system(*%w{git fetch --quiet} << remote)

remote_branch.insert(0, remote + '/')

# get the commit IDs of the local and remote branches
local_commit_id = get_commit_id(local_branch)
exit(1) if local_commit_id.nil?
remote_commit_id = get_commit_id(remote_branch)
exit(1) if remote_commit_id.nil?

# get the number of commits in the remote branch not present in the local branch
counts = get_git_command_output(*%w{rev-list --left-right --count} << "#{local_commit_id}...#{remote_commit_id}")
exit(1) unless $?.exitstatus() == 0

exit(counts.split(' ')[1].to_i == 0 ? 1 : 0)
13 changes: 11 additions & 2 deletions modules/federated_wiki/manifests/init.pp
Expand Up @@ -13,6 +13,8 @@
$patch_dir = "${install_dir}/patch"
$memcached_patch = "${patch_dir}/memcached.patch"

$need_pull_util = '/usr/local/lib/git_need_pull.rb'

package { [$build_dependencies]:
ensure => installed,
}
Expand All @@ -36,6 +38,13 @@
ensure => installed,
}

file { $need_pull_util:
source => 'puppet:///modules/federated_wiki/git_need_pull.rb',
owner => root,
group => root,
mode => 0644,
}

exec { 'git-clone':
unless => "test -d \"${install_dir}\"",
command => "git clone \"${source_git_repository}\" \"${install_dir}\"",
Expand All @@ -44,7 +53,7 @@
}

exec { 'git-pull':
onlyif => "test -d \"${install_dir}\"",
onlyif => "test -d \"${install_dir}\" && ruby \"${need_pull_util}\"",
command => 'git pull',
cwd => $install_dir,
path => ['/usr/local/bin', '/bin', '/usr/bin'],
Expand All @@ -63,7 +72,7 @@
owner => root,
group => root,
mode => 0644,
require => File[$patch_dir],
require => File[$patch_dir],
}

exec { 'memcached-patch':
Expand Down

0 comments on commit c71af21

Please sign in to comment.