diff --git a/lib/git_worktree.rb b/lib/git_worktree.rb index 5d772c55328..71bdeacec6d 100644 --- a/lib/git_worktree.rb +++ b/lib/git_worktree.rb @@ -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) @@ -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 @@ -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]} @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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| diff --git a/spec/fixtures/git_repos/no_master.git/HEAD b/spec/fixtures/git_repos/no_master.git/HEAD new file mode 100644 index 00000000000..1d57c9ea7fc --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/branch2 diff --git a/spec/fixtures/git_repos/no_master.git/config b/spec/fixtures/git_repos/no_master.git/config new file mode 100644 index 00000000000..4f7d452b3cf --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + ignorecase = true + precomposeunicode = false diff --git a/spec/fixtures/git_repos/no_master.git/objects/43/0ff1dcee43d2e8a12c3ba36f5ff20e46136152 b/spec/fixtures/git_repos/no_master.git/objects/43/0ff1dcee43d2e8a12c3ba36f5ff20e46136152 new file mode 100644 index 00000000000..20d209978f7 Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/43/0ff1dcee43d2e8a12c3ba36f5ff20e46136152 differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/44/f84e202a8eb9d8d35fc183ae8637db3b003aa8 b/spec/fixtures/git_repos/no_master.git/objects/44/f84e202a8eb9d8d35fc183ae8637db3b003aa8 new file mode 100644 index 00000000000..134a5a3b1fb --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/objects/44/f84e202a8eb9d8d35fc183ae8637db3b003aa8 @@ -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� \ No newline at end of file diff --git a/spec/fixtures/git_repos/no_master.git/objects/78/3878cb3ed5964c75767141176c80f7d528abb5 b/spec/fixtures/git_repos/no_master.git/objects/78/3878cb3ed5964c75767141176c80f7d528abb5 new file mode 100644 index 00000000000..c3a5445fde3 Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/78/3878cb3ed5964c75767141176c80f7d528abb5 differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/95/99fdeb034597d90d72d2f58396dee096885b79 b/spec/fixtures/git_repos/no_master.git/objects/95/99fdeb034597d90d72d2f58396dee096885b79 new file mode 100644 index 00000000000..d5d5dce7727 Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/95/99fdeb034597d90d72d2f58396dee096885b79 differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/ae/fde3a01f6e10d72fd4899ce14c8b2654d3eb45 b/spec/fixtures/git_repos/no_master.git/objects/ae/fde3a01f6e10d72fd4899ce14c8b2654d3eb45 new file mode 100644 index 00000000000..7d41955fe6e Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/ae/fde3a01f6e10d72fd4899ce14c8b2654d3eb45 differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/e3/16f7d7548ea8f874c872ff3be04f4e9849833b b/spec/fixtures/git_repos/no_master.git/objects/e3/16f7d7548ea8f874c872ff3be04f4e9849833b new file mode 100644 index 00000000000..c8e759a2a0e Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/e3/16f7d7548ea8f874c872ff3be04f4e9849833b differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/spec/fixtures/git_repos/no_master.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 new file mode 100644 index 00000000000..71122389437 Binary files /dev/null and b/spec/fixtures/git_repos/no_master.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 differ diff --git a/spec/fixtures/git_repos/no_master.git/objects/fd/b4bc8c22f4b3ca8279c2e1eb92670893473d05 b/spec/fixtures/git_repos/no_master.git/objects/fd/b4bc8c22f4b3ca8279c2e1eb92670893473d05 new file mode 100644 index 00000000000..d71bc55f4cd --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/objects/fd/b4bc8c22f4b3ca8279c2e1eb92670893473d05 @@ -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ן����"+� \ No newline at end of file diff --git a/spec/fixtures/git_repos/no_master.git/packed-refs b/spec/fixtures/git_repos/no_master.git/packed-refs new file mode 100644 index 00000000000..6eaf4028ff0 --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/packed-refs @@ -0,0 +1,3 @@ +# pack-refs with: peeled fully-peeled +783878cb3ed5964c75767141176c80f7d528abb5 refs/heads/branch1 +e316f7d7548ea8f874c872ff3be04f4e9849833b refs/heads/branch2 diff --git a/spec/fixtures/git_repos/no_master.git/refs/.gitkeep b/spec/fixtures/git_repos/no_master.git/refs/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/spec/fixtures/git_repos/no_master.git/refs/tags/tag1 b/spec/fixtures/git_repos/no_master.git/refs/tags/tag1 new file mode 100644 index 00000000000..d22d8fbced6 --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/refs/tags/tag1 @@ -0,0 +1 @@ +fdb4bc8c22f4b3ca8279c2e1eb92670893473d05 diff --git a/spec/fixtures/git_repos/no_master.git/refs/tags/tag2 b/spec/fixtures/git_repos/no_master.git/refs/tags/tag2 new file mode 100644 index 00000000000..fd1e2c81436 --- /dev/null +++ b/spec/fixtures/git_repos/no_master.git/refs/tags/tag2 @@ -0,0 +1 @@ +e316f7d7548ea8f874c872ff3be04f4e9849833b diff --git a/spec/lib/git_worktree_spec.rb b/spec/lib/git_worktree_spec.rb index af0de70cb64..d90331afdcc 100644 --- a/spec/lib/git_worktree_spec.rb +++ b/spec/lib/git_worktree_spec.rb @@ -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) }