Skip to content

Fix missing fetch #113

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

Merged
merged 10 commits into from
May 12, 2022
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
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## Fixed Issues

* [#106](https://github.com/pmd/pmd-regression-tester/issues/106): git clone/checkout fails when using commit sha1 as tag

## External Contributions

# 1.5.0 / 2022-05-06
Expand Down
32 changes: 15 additions & 17 deletions lib/pmdtester/builders/project_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ def clone_projects
logger.info "Start cloning #{project.name} repository"
path = project.clone_root_path

raise "Unsupported project type '#{project.type}' - only git is supported" unless project.type == 'git'

if File.exist?(path)
logger.warn "Skipping clone, project path #{path} already exists"
else
raise "Unsupported project type '#{project.type}' - only git is supported" unless project.type == 'git'

# git:
# Don't download whole history
# Note we don't use --single-branch, because the repo is downloaded
# once but may be used with several tags.
clone_cmd = "git clone --no-single-branch --depth 1 #{project.connection} #{path}"

# Don't download whole history. This just fetches HEAD, the correct ref is fetched below.
clone_cmd = "git clone --single-branch --depth 1 #{project.connection} #{path}"
Cmd.execute_successfully(clone_cmd)
end

Dir.chdir(path) do
execute_reset_cmd(project.type, project.tag)
# this works with tags, branch names and (full-length) hashes
# first move to a different (temporary) branch, in case we are already on the branch that we want to update
Cmd.execute_successfully('git checkout -b fetched/temp')
# fetches any new commits. Could also be a tag.
Cmd.execute_successfully("git fetch --depth 1 origin #{project.tag}")
# update the branch to work on based on the new fetch. Creates a new branch if it doesn't exist already
Cmd.execute_successfully("git branch --force fetched/#{project.tag} FETCH_HEAD")
# checkout the updated branch
Cmd.execute_successfully("git checkout fetched/#{project.tag}")
# remove the temporary branch
Cmd.execute_successfully('git branch -D fetched/temp')
end
logger.info "Cloning #{project.name} completed"
end
Expand Down Expand Up @@ -93,13 +99,5 @@ def run_as_script(path, command)
end
stdout
end

def execute_reset_cmd(type, tag)
raise "Unsupported project type '#{type}' - only git is supported" unless type == 'git'

reset_cmd = "git checkout #{tag}; git reset --hard #{tag}"

Cmd.execute_successfully(reset_cmd)
end
end
end
35 changes: 35 additions & 0 deletions test/integration_test_project_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'test_helper'
require 'etc'

# Integration test for PmdTester::ProjectBuilder
class IntegrationTestProjectBuilder < Test::Unit::TestCase
include PmdTester
include TestUtils

def setup
`rake clean`
end

def test_clone_with_commit_sha1
projects = PmdTester::ProjectsParser.new.parse('test/resources/integration_test_project_builder/' \
'project-list_commit_sha1.xml')
project_builder = PmdTester::ProjectBuilder.new(projects)
project_builder.clone_projects

assert_path_exist('target/repositories/Schedul-o-matic-9000/.git/HEAD')
assert_file_content_equals('ref: refs/heads/fetched/6b1229ba43b38931fbbab5924bc9b9611d19a786',
'target/repositories/Schedul-o-matic-9000/.git/HEAD')
assert_file_content_equals('6b1229ba43b38931fbbab5924bc9b9611d19a786',
'target/repositories/Schedul-o-matic-9000/.git/refs/heads/fetched/' \
'6b1229ba43b38931fbbab5924bc9b9611d19a786')

assert_path_exist('target/repositories/fflib-apex-common/.git/HEAD')
assert_file_content_equals('ref: refs/heads/fetched/7e0891efb86d23de62811af56d87d0959082a322',
'target/repositories/fflib-apex-common/.git/HEAD')
assert_file_content_equals('7e0891efb86d23de62811af56d87d0959082a322',
'target/repositories/fflib-apex-common/.git/refs/heads/fetched/' \
'7e0891efb86d23de62811af56d87d0959082a322')
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>

<projectlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="projectlist_1_2_0.xsd">
<description>Standard Projects</description>

<project>
<name>Schedul-o-matic-9000</name>
<type>git</type>
<connection>https://github.com/SalesforceLabs/Schedul-o-matic-9000</connection>
<tag>6b1229ba43b38931fbbab5924bc9b9611d19a786</tag>
</project>

<project>
<name>fflib-apex-common</name>
<type>git</type>
<connection>https://github.com/apex-enterprise-patterns/fflib-apex-common</connection>
<tag>7e0891efb86d23de62811af56d87d0959082a322</tag>
</project>

</projectlist>
21 changes: 21 additions & 0 deletions test/resources/project_builder/project-list_commit_sha1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>

<projectlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="projectlist_1_2_0.xsd">
<description>Standard Projects</description>

<project>
<name>Schedul-o-matic-9000</name>
<type>git</type>
<connection>https://github.com/SalesforceLabs/Schedul-o-matic-9000</connection>
<tag>6b1229ba43b38931fbbab5924bc9b9611d19a786</tag>
</project>

<project>
<name>fflib-apex-common</name>
<type>git</type>
<connection>https://github.com/apex-enterprise-patterns/fflib-apex-common</connection>
<tag>7e0891efb86d23de62811af56d87d0959082a322</tag>
</project>

</projectlist>
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def assert_file_equals(expected_path, actual_path)
assert_equal(expected_file, actual_file, actual_path)
end

def assert_file_content_equals(expected_content, actual_path)
actual_content = normalize_text(File.open(actual_path).read)
assert_equal(expected_content, actual_content, actual_path)
end

def assert_file_exists(path)
assert File.exist?(path)
end
Expand Down
19 changes: 17 additions & 2 deletions test/test_project_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def test_clone
project_builder.clone_projects
end

def test_clone_with_commit_sha1
expect_git_clone('Schedul-o-matic-9000', 'https://github.com/SalesforceLabs/Schedul-o-matic-9000',
'6b1229ba43b38931fbbab5924bc9b9611d19a786')
expect_git_clone('fflib-apex-common', 'https://github.com/apex-enterprise-patterns/fflib-apex-common',
'7e0891efb86d23de62811af56d87d0959082a322')

projects = PmdTester::ProjectsParser.new.parse('test/resources/project_builder/project-list_commit_sha1.xml')
project_builder = PmdTester::ProjectBuilder.new(projects)
project_builder.clone_projects
end

def test_build
Dir.stubs(:getwd)
.returns('target/repositories/checkstyle')
Expand All @@ -38,10 +49,14 @@ def test_build

def expect_git_clone(name, url, revision)
File.stubs(:exist?).with("target/repositories/#{name}").returns(false).once
PmdTester::Cmd.stubs(:execute_successfully).with('git clone --no-single-branch --depth 1' \
PmdTester::Cmd.stubs(:execute_successfully).with('git clone --single-branch --depth 1' \
" #{url} target/repositories/#{name}").once
Dir.stubs(:chdir).with("target/repositories/#{name}").yields.once
PmdTester::Cmd.stubs(:execute_successfully).with("git checkout #{revision}; git reset --hard #{revision}").once
PmdTester::Cmd.stubs(:execute_successfully).with('git checkout -b fetched/temp').once
PmdTester::Cmd.stubs(:execute_successfully).with("git fetch --depth 1 origin #{revision}").once
PmdTester::Cmd.stubs(:execute_successfully).with("git branch --force fetched/#{revision} FETCH_HEAD").once
PmdTester::Cmd.stubs(:execute_successfully).with("git checkout fetched/#{revision}").once
PmdTester::Cmd.stubs(:execute_successfully).with('git branch -D fetched/temp').once
end

def expect_build(name, build_cmd = nil, auxclasspath_cmd = nil)
Expand Down