+ def with_foo_repository
+ Dir.chdir("#{Rails.configuration.Git.Repositories}/#{repositories(:foo).uuid}") do
+ must_pipe("git checkout master 2>&1")
+ yield
+ end
+ end
+
+ test 'tag_in_internal_repository, new non-tip sha1 in local repo' do
+ tag = "tag#{rand(10**10)}"
+ sha1 = nil
+ with_foo_repository do
+ must_pipe("git checkout -b branch-#{rand(10**10)} 2>&1")
+ must_pipe("echo -n #{tag.shellescape} >bar")
+ must_pipe("git add bar")
+ must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+ sha1 = must_pipe("git log -n1 --format=%H").strip
+ must_pipe("git rm bar")
+ must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+ end
+ CommitsHelper::tag_in_internal_repository 'active/foo', sha1, tag
+ gitint = "git --git-dir #{Rails.configuration.Containers.JobsAPI.GitInternalDir.shellescape}"
+ assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}"))
+ assert $?.success?
+ end
+
+ test 'tag_in_internal_repository, new unreferenced sha1 in local repo' do
+ tag = "tag#{rand(10**10)}"
+ sha1 = nil
+ with_foo_repository do
+ must_pipe("echo -n #{tag.shellescape} >bar")
+ must_pipe("git add bar")
+ must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+ sha1 = must_pipe("git log -n1 --format=%H").strip
+ must_pipe("git reset --hard HEAD^")
+ end
+ CommitsHelper::tag_in_internal_repository 'active/foo', sha1, tag
+ gitint = "git --git-dir #{Rails.configuration.Containers.JobsAPI.GitInternalDir.shellescape}"
+ assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}"))
+ assert $?.success?
+ end
+
+ # In active/shabranchnames, "7387838c69a21827834586cc42b467ff6c63293b" is
+ # both a commit hash, and the name of a branch that begins from that same
+ # commit.
+ COMMIT_BRANCH_NAME = "7387838c69a21827834586cc42b467ff6c63293b"
+ # A commit that appears in the branch after 7387838c.
+ COMMIT_BRANCH_COMMIT_2 = "abec49829bf1758413509b7ffcab32a771b71e81"
+ # "738783" is another branch that starts from the above commit.
+ SHORT_COMMIT_BRANCH_NAME = COMMIT_BRANCH_NAME[0, 6]
+ # A commit that appears in branch 738783 after 7387838c.
+ SHORT_BRANCH_COMMIT_2 = "77e1a93093663705a63bb4d505698047e109dedd"
+
+ test "find_commit_range min_version prefers commits over branch names" do
+ assert_equal([COMMIT_BRANCH_NAME],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ COMMIT_BRANCH_NAME, nil, nil))
+ end
+
+ test "find_commit_range max_version prefers commits over branch names" do
+ assert_equal([COMMIT_BRANCH_NAME],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ nil, COMMIT_BRANCH_NAME, nil))
+ end
+
+ test "find_commit_range min_version with short branch name" do
+ assert_equal([SHORT_BRANCH_COMMIT_2],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ SHORT_COMMIT_BRANCH_NAME, nil, nil))
+ end
+
+ test "find_commit_range max_version with short branch name" do
+ assert_equal([SHORT_BRANCH_COMMIT_2],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ nil, SHORT_COMMIT_BRANCH_NAME, nil))
+ end
+
+ test "find_commit_range min_version with disambiguated branch name" do
+ assert_equal([COMMIT_BRANCH_COMMIT_2],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ "heads/#{COMMIT_BRANCH_NAME}",
+ nil, nil))
+ end
+
+ test "find_commit_range max_version with disambiguated branch name" do
+ assert_equal([COMMIT_BRANCH_COMMIT_2],
+ CommitsHelper::find_commit_range("active/shabranchnames", nil,
+ "heads/#{COMMIT_BRANCH_NAME}", nil))
+ end
+
+ test "find_commit_range min_version with unambiguous short name" do
+ assert_equal([COMMIT_BRANCH_NAME],
+ CommitsHelper::find_commit_range("active/shabranchnames",
+ COMMIT_BRANCH_NAME[0..-2], nil, nil))
+ end
+
+ test "find_commit_range max_version with unambiguous short name" do
+ assert_equal([COMMIT_BRANCH_NAME],
+ CommitsHelper::find_commit_range("active/shabranchnames", nil,
+ COMMIT_BRANCH_NAME[0..-2], nil))
+ end
+