From d2e8c0e96460b34371bbb72f0ecf878321fbf8e3 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Mon, 7 Dec 2015 14:46:40 -0500 Subject: pull origin/master before pushing --- lib/oxidized/hook/githubrepo.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/oxidized/hook/githubrepo.rb b/lib/oxidized/hook/githubrepo.rb index bf33b89..83e1078 100644 --- a/lib/oxidized/hook/githubrepo.rb +++ b/lib/oxidized/hook/githubrepo.rb @@ -9,6 +9,24 @@ class GithubRepo < Oxidized::Hook log "Pushing local repository(#{repo.path})..." remote = repo.remotes['origin'] || repo.remotes.create('origin', cfg.remote_repo) log "to remote: #{remote.url}" - remote.push(['+refs/heads/master'], credentials: credentials) + + fetch_and_merge_remote(repo, credentials) + + remote.push([repo.head.name], credentials: credentials) + end + + def fetch_and_merge_remote(repo, credentials) + their_branch = repo.branches["origin/master"] or return + + repo.fetch('origin', [repo.head.name], credentials: credentials) + + merge_index = repo.merge_commits(repo.head.target_id, their_branch.target_id) + + Rugged::Commit.create(repo, { + parents: [repo.head.target, their_branch.target], + tree: merge_index.write_tree(repo), + message: "Merge remote-tracking branch '#{their_branch.name}'", + update_ref: "HEAD" + }) end end -- cgit v1.2.1 From 68dd88766604145a8c8d16b22a68a9850ce92564 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Mon, 7 Dec 2015 15:27:42 -0500 Subject: fix test --- spec/githubrepo_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/githubrepo_spec.rb b/spec/githubrepo_spec.rb index a193cd6..9b509e4 100644 --- a/spec/githubrepo_spec.rb +++ b/spec/githubrepo_spec.rb @@ -17,6 +17,10 @@ describe Oxidized::Node do remote.expects(:push).returns(true) repo.expects(:remotes).returns({'origin' => remote}) repo.expects(:path).returns('foo.git') + repo_head = mock() + repo_head.expects(:name).returns('origin/master') + repo.expects(:head).returns(repo_head) + repo.expects(:branches).returns({}) Rugged::Repository.expects(:new).with('foo.git').returns(repo) end -- cgit v1.2.1