Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Automate Git repos without master branch #16690

Merged
merged 2 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions lib/git_worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class GitWorktree
ENTRY_KEYS = [:path, :dev, :ino, :mode, :gid, :uid, :ctime, :mtime]
DEFAULT_FILE_MODE = 0100644
LOCK_REFERENCE = 'refs/locks'
MASTER_REF = 'refs/heads/master'

def initialize(options = {})
raise ArgumentError, "Must specify path" unless options.key?(:path)
Expand Down Expand Up @@ -74,7 +73,7 @@ def add(path, data, default_entry_keys = {})
current_index.add(entry)
end

def remove(path )
def remove(path)
current_index.remove(path)
end

Expand Down Expand Up @@ -126,7 +125,7 @@ def save_changes(message, owner = :local)
def file_attributes(fname)
walker = Rugged::Walker.new(@repo)
walker.sorting(Rugged::SORT_DATE)
walker.push(@repo.ref(MASTER_REF).target)
walker.push(@repo.ref(local_ref).target)
commit = walker.find { |c| c.diff(:paths => [fname]).size > 0 }
return {} unless commit
{:updated_on => commit.time.gmtime, :updated_by => commit.author[:name]}
Expand Down Expand Up @@ -169,9 +168,21 @@ def mv_dir(old_dir, new_dir)

private

def current_branch
@repo.head_unborn? ? 'master' : @repo.head.name.sub(/^refs\/heads\//, '')
end

def upstream_ref
"refs/remotes/#{@remote_name}/#{current_branch}"
end

def local_ref
"refs/heads/#{current_branch}"
end

def fetch_and_merge
fetch
commit = @repo.ref("refs/remotes/#{@remote_name}/master").target
commit = @repo.ref(upstream_ref).target
merge(commit)
end

Expand All @@ -187,21 +198,21 @@ def pull
def merge_and_push(commit)
rebase = false
push_lock do
@saved_cid = @repo.ref(MASTER_REF).target.oid
@saved_cid = @repo.ref(local_ref).target.oid
merge(commit, rebase)
rebase = true
@repo.push(@remote_name, [MASTER_REF], :credentials => @cred)
@repo.push(@remote_name, [local_ref], :credentials => @cred)
end
end

def merge(commit, rebase = false)
master_branch = @repo.ref(MASTER_REF)
merge_index = master_branch ? @repo.merge_commits(master_branch.target, commit) : nil
current_branch = @repo.ref(local_ref)
merge_index = current_branch ? @repo.merge_commits(current_branch.target, commit) : nil
if merge_index && merge_index.conflicts?
result = differences_with_master(commit)
result = differences_with_current(commit)
raise GitWorktreeException::GitConflicts, result
end
commit = rebase(commit, merge_index, master_branch.try(:target)) if rebase
commit = rebase(commit, merge_index, current_branch.try(:target)) if rebase
@repo.reset(commit, :soft)
end

Expand All @@ -217,7 +228,7 @@ def rebase(commit, merge_index, parent)

def commit(message)
tree = @current_index.write_tree(@repo)
parents = @repo.empty? ? [] : [@repo.ref(MASTER_REF).target].compact
parents = @repo.empty? ? [] : [@repo.ref(local_ref).target].compact
create_commit(message, tree, parents)
end

Expand Down Expand Up @@ -268,7 +279,7 @@ def get_tree(path)
end

def lookup_commit_tree
return nil unless @repo.branches['master']
return nil if !@commit_sha && !@repo.branches['master']
ct = @commit_sha ? @repo.lookup(@commit_sha) : @repo.branches['master'].target
ct.tree if ct
end
Expand Down Expand Up @@ -306,7 +317,7 @@ def create_commit(message, tree, parents)
end

def lock
@repo.references.create(LOCK_REFERENCE, MASTER_REF)
@repo.references.create(LOCK_REFERENCE, local_ref)
yield
rescue Rugged::ReferenceError
sleep 0.1
Expand All @@ -316,7 +327,7 @@ def lock
end

def push_lock
@repo.references.create(LOCK_REFERENCE, MASTER_REF)
@repo.references.create(LOCK_REFERENCE, local_ref)
begin
yield
rescue Rugged::ReferenceError => err
Expand All @@ -332,9 +343,9 @@ def push_lock
end
end

def differences_with_master(commit)
def differences_with_current(commit)
differences = {}
diffs = @repo.diff(commit, @repo.ref(MASTER_REF).target)
diffs = @repo.diff(commit, @repo.ref(local_ref).target)
diffs.deltas.each do |delta|
result = []
delta.diff.each_line do |line|
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/git_repos/no_master.git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/branch2
6 changes: 6 additions & 0 deletions spec/fixtures/git_repos/no_master.git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = false
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x��A! E]s�^@L�@b�Wah�LI��_^��y��/��C�<��,�e��� ���+cL���7y����}@{�W�{��}�Y/��8��0��l�Z3�|S��3����t5�
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x-�M
1 @a�=E.�4�i2 "ޤΨ�TJ6�ފ���{g@'��5+�8a���BS��G��޸�QbJd����ۺ���x������a}ƭ��\z-K��g@OL^&r��d�1ן����"+�
3 changes: 3 additions & 0 deletions spec/fixtures/git_repos/no_master.git/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pack-refs with: peeled fully-peeled
783878cb3ed5964c75767141176c80f7d528abb5 refs/heads/branch1
e316f7d7548ea8f874c872ff3be04f4e9849833b refs/heads/branch2
Empty file.
1 change: 1 addition & 0 deletions spec/fixtures/git_repos/no_master.git/refs/tags/tag1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fdb4bc8c22f4b3ca8279c2e1eb92670893473d05
1 change: 1 addition & 0 deletions spec/fixtures/git_repos/no_master.git/refs/tags/tag2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e316f7d7548ea8f874c872ff3be04f4e9849833b
19 changes: 19 additions & 0 deletions spec/lib/git_worktree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,25 @@ def open_existing_repo
end
end

describe "git branches with no master" do
let(:git_repo_path) { Rails.root.join("spec/fixtures/git_repos/no_master.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }

describe "#branches" do
it "all branches" do
expect(test_repo.branches).to match_array(%w(branch1 branch2))
end
end

describe "#file_list" do
it "get list of files in a branch" do
test_repo.branch = 'branch2'

expect(test_repo.file_list).to match_array(%w(file1 file2 file3 file4))
end
end
end

describe 'git tags' do
let(:git_repo_path) { Rails.root.join("spec/fixtures/git_repos/branch_and_tag.git") }
let(:test_repo) { GitWorktree.new(:path => git_repo_path.to_s) }
Expand Down