Skip to content

Commit 264536e

Browse files
authored
Merge pull request #113 from oowekyala/fix-missing-fetch
Fix missing fetch
2 parents 5640ec9 + 5d01c7b commit 264536e

File tree

7 files changed

+116
-19
lines changed

7 files changed

+116
-19
lines changed

History.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
## Fixed Issues
88

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

1113
# 1.5.0 / 2022-05-06

lib/pmdtester/builders/project_builder.rb

+15-17
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,28 @@ def clone_projects
1919
logger.info "Start cloning #{project.name} repository"
2020
path = project.clone_root_path
2121

22+
raise "Unsupported project type '#{project.type}' - only git is supported" unless project.type == 'git'
23+
2224
if File.exist?(path)
2325
logger.warn "Skipping clone, project path #{path} already exists"
2426
else
25-
raise "Unsupported project type '#{project.type}' - only git is supported" unless project.type == 'git'
26-
27-
# git:
28-
# Don't download whole history
29-
# Note we don't use --single-branch, because the repo is downloaded
30-
# once but may be used with several tags.
31-
clone_cmd = "git clone --no-single-branch --depth 1 #{project.connection} #{path}"
32-
27+
# Don't download whole history. This just fetches HEAD, the correct ref is fetched below.
28+
clone_cmd = "git clone --single-branch --depth 1 #{project.connection} #{path}"
3329
Cmd.execute_successfully(clone_cmd)
3430
end
3531

3632
Dir.chdir(path) do
37-
execute_reset_cmd(project.type, project.tag)
33+
# this works with tags, branch names and (full-length) hashes
34+
# first move to a different (temporary) branch, in case we are already on the branch that we want to update
35+
Cmd.execute_successfully('git checkout -b fetched/temp')
36+
# fetches any new commits. Could also be a tag.
37+
Cmd.execute_successfully("git fetch --depth 1 origin #{project.tag}")
38+
# update the branch to work on based on the new fetch. Creates a new branch if it doesn't exist already
39+
Cmd.execute_successfully("git branch --force fetched/#{project.tag} FETCH_HEAD")
40+
# checkout the updated branch
41+
Cmd.execute_successfully("git checkout fetched/#{project.tag}")
42+
# remove the temporary branch
43+
Cmd.execute_successfully('git branch -D fetched/temp')
3844
end
3945
logger.info "Cloning #{project.name} completed"
4046
end
@@ -93,13 +99,5 @@ def run_as_script(path, command)
9399
end
94100
stdout
95101
end
96-
97-
def execute_reset_cmd(type, tag)
98-
raise "Unsupported project type '#{type}' - only git is supported" unless type == 'git'
99-
100-
reset_cmd = "git checkout #{tag}; git reset --hard #{tag}"
101-
102-
Cmd.execute_successfully(reset_cmd)
103-
end
104102
end
105103
end
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require 'etc'
5+
6+
# Integration test for PmdTester::ProjectBuilder
7+
class IntegrationTestProjectBuilder < Test::Unit::TestCase
8+
include PmdTester
9+
include TestUtils
10+
11+
def setup
12+
`rake clean`
13+
end
14+
15+
def test_clone_with_commit_sha1
16+
projects = PmdTester::ProjectsParser.new.parse('test/resources/integration_test_project_builder/' \
17+
'project-list_commit_sha1.xml')
18+
project_builder = PmdTester::ProjectBuilder.new(projects)
19+
project_builder.clone_projects
20+
21+
assert_path_exist('target/repositories/Schedul-o-matic-9000/.git/HEAD')
22+
assert_file_content_equals('ref: refs/heads/fetched/6b1229ba43b38931fbbab5924bc9b9611d19a786',
23+
'target/repositories/Schedul-o-matic-9000/.git/HEAD')
24+
assert_file_content_equals('6b1229ba43b38931fbbab5924bc9b9611d19a786',
25+
'target/repositories/Schedul-o-matic-9000/.git/refs/heads/fetched/' \
26+
'6b1229ba43b38931fbbab5924bc9b9611d19a786')
27+
28+
assert_path_exist('target/repositories/fflib-apex-common/.git/HEAD')
29+
assert_file_content_equals('ref: refs/heads/fetched/7e0891efb86d23de62811af56d87d0959082a322',
30+
'target/repositories/fflib-apex-common/.git/HEAD')
31+
assert_file_content_equals('7e0891efb86d23de62811af56d87d0959082a322',
32+
'target/repositories/fflib-apex-common/.git/refs/heads/fetched/' \
33+
'7e0891efb86d23de62811af56d87d0959082a322')
34+
end
35+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
3+
<projectlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="projectlist_1_2_0.xsd">
5+
<description>Standard Projects</description>
6+
7+
<project>
8+
<name>Schedul-o-matic-9000</name>
9+
<type>git</type>
10+
<connection>https://github.com/SalesforceLabs/Schedul-o-matic-9000</connection>
11+
<tag>6b1229ba43b38931fbbab5924bc9b9611d19a786</tag>
12+
</project>
13+
14+
<project>
15+
<name>fflib-apex-common</name>
16+
<type>git</type>
17+
<connection>https://github.com/apex-enterprise-patterns/fflib-apex-common</connection>
18+
<tag>7e0891efb86d23de62811af56d87d0959082a322</tag>
19+
</project>
20+
21+
</projectlist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
3+
<projectlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="projectlist_1_2_0.xsd">
5+
<description>Standard Projects</description>
6+
7+
<project>
8+
<name>Schedul-o-matic-9000</name>
9+
<type>git</type>
10+
<connection>https://github.com/SalesforceLabs/Schedul-o-matic-9000</connection>
11+
<tag>6b1229ba43b38931fbbab5924bc9b9611d19a786</tag>
12+
</project>
13+
14+
<project>
15+
<name>fflib-apex-common</name>
16+
<type>git</type>
17+
<connection>https://github.com/apex-enterprise-patterns/fflib-apex-common</connection>
18+
<tag>7e0891efb86d23de62811af56d87d0959082a322</tag>
19+
</project>
20+
21+
</projectlist>

test/test_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def assert_file_equals(expected_path, actual_path)
1717
assert_equal(expected_file, actual_file, actual_path)
1818
end
1919

20+
def assert_file_content_equals(expected_content, actual_path)
21+
actual_content = normalize_text(File.open(actual_path).read)
22+
assert_equal(expected_content, actual_content, actual_path)
23+
end
24+
2025
def assert_file_exists(path)
2126
assert File.exist?(path)
2227
end

test/test_project_builder.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_clone
1818
project_builder.clone_projects
1919
end
2020

21+
def test_clone_with_commit_sha1
22+
expect_git_clone('Schedul-o-matic-9000', 'https://github.com/SalesforceLabs/Schedul-o-matic-9000',
23+
'6b1229ba43b38931fbbab5924bc9b9611d19a786')
24+
expect_git_clone('fflib-apex-common', 'https://github.com/apex-enterprise-patterns/fflib-apex-common',
25+
'7e0891efb86d23de62811af56d87d0959082a322')
26+
27+
projects = PmdTester::ProjectsParser.new.parse('test/resources/project_builder/project-list_commit_sha1.xml')
28+
project_builder = PmdTester::ProjectBuilder.new(projects)
29+
project_builder.clone_projects
30+
end
31+
2132
def test_build
2233
Dir.stubs(:getwd)
2334
.returns('target/repositories/checkstyle')
@@ -38,10 +49,14 @@ def test_build
3849

3950
def expect_git_clone(name, url, revision)
4051
File.stubs(:exist?).with("target/repositories/#{name}").returns(false).once
41-
PmdTester::Cmd.stubs(:execute_successfully).with('git clone --no-single-branch --depth 1' \
52+
PmdTester::Cmd.stubs(:execute_successfully).with('git clone --single-branch --depth 1' \
4253
" #{url} target/repositories/#{name}").once
4354
Dir.stubs(:chdir).with("target/repositories/#{name}").yields.once
44-
PmdTester::Cmd.stubs(:execute_successfully).with("git checkout #{revision}; git reset --hard #{revision}").once
55+
PmdTester::Cmd.stubs(:execute_successfully).with('git checkout -b fetched/temp').once
56+
PmdTester::Cmd.stubs(:execute_successfully).with("git fetch --depth 1 origin #{revision}").once
57+
PmdTester::Cmd.stubs(:execute_successfully).with("git branch --force fetched/#{revision} FETCH_HEAD").once
58+
PmdTester::Cmd.stubs(:execute_successfully).with("git checkout fetched/#{revision}").once
59+
PmdTester::Cmd.stubs(:execute_successfully).with('git branch -D fetched/temp').once
4560
end
4661

4762
def expect_build(name, build_cmd = nil, auxclasspath_cmd = nil)

0 commit comments

Comments
 (0)