Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
pass REPO_URL and REPO_NAME as a prelaunch env variables. update git …
Browse files Browse the repository at this point in the history
…clone command to use REPO_NAME

Summary: previously, the git clone command uses the basename of the source path (repo_path) as the repo name when determining the cache. This is fragile - repo_path defaults to ./source/.

Test Plan: unit tests

Reviewers: anupc

Reviewed By: anupc

Subscribers: changesbot, kylec, wwu

Differential Revision: https://tails.corp.dropbox.com/D233858
  • Loading branch information
Naphat Sanguansin committed Oct 5, 2016
1 parent bc8d599 commit 4656893
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
7 changes: 7 additions & 0 deletions changes/models/jobplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ def get_build_step_for_job(cls, job_id):
# TODO(anupc): Does it make sense to expose this in project config?
bazel_debug_config = current_app.config['BAZEL_DEBUG_CONFIG']

if 'prelaunch_env' not in bazel_debug_config:
bazel_debug_config['prelaunch_env'] = {}

vcs = job.project.repository.get_vcs()

bazel_debug_config['prelaunch_env']['REPO_URL'] = job.project.repository.url
bazel_debug_config['prelaunch_env']['REPO_NAME'] = vcs.get_repository_name(job.project.repository.url)

implementation = LXCBuildStep(
cluster=current_app.config['DEFAULT_CLUSTER'],
commands=[
Expand Down
15 changes: 15 additions & 0 deletions changes/vcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ def _execute_subproccess(self, proc, *args, **kwargs):
raise CommandError(args[0], proc.returncode, stdout, stderr)
return stdout

@classmethod
def get_repository_name(cls, repository_url):
"""
Given a repository URL, get the repository name.
Examples:
example.com:test.git -> test.git
example.com:test -> test
example.com:prefix/test.git -> test.git
example.com:test.git -> test
"""
# type: (str) -> str
path = repository_url.split(':')[-1].strip('/')
return os.path.basename(path)

@staticmethod
def get_clone_command(remote_url, path, revision, clean=True, cache_dir=None):
# type: (str, str, str, bool, Optional[str]) -> str
Expand Down
14 changes: 11 additions & 3 deletions changes/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
LOCAL_PATH=%(local_path)s
REVISION=%(revision)s
BASE_NAME=$(basename "$LOCAL_PATH")
CACHE_PATH="%(cache_dir)s/$BASE_NAME.git"
CACHE_PATH="%(cache_dir)s/%(cache_base_name)s"
if [ -d "$CACHE_PATH" ]; then
echo "Using local repository cache."
Expand Down Expand Up @@ -315,6 +314,14 @@ def is_child_parent(self, child_in_question, parent_in_question):
else:
raise

@classmethod
def get_repository_name(cls, repository_url):
# type: (str) -> str
name = super(GitVcs, cls).get_repository_name(repository_url)
if not name.endswith('.git'):
name += '.git'
return name

@staticmethod
def get_clone_command(remote_url, path, revision, clean=True, cache_dir=None):
# type: (str, str, str, bool, Optional[str]) -> str
Expand All @@ -324,7 +331,8 @@ def get_clone_command(remote_url, path, revision, clean=True, cache_dir=None):
revision=revision,
cache_dir=cache_dir or "/dev/null",
clean_command='git clean -fdx' if clean else '',
pre_reset_command='git checkout -q master'
pre_reset_command='git checkout -q master',
cache_base_name=GitVcs.get_repository_name(remote_url),
)

def get_buildstep_clone(self, source, workspace, clean=True, cache_dir=None):
Expand Down
14 changes: 14 additions & 0 deletions tests/changes/vcs/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,17 @@ def test_no_newline_both(self):
patch = self.PATCH_TEMPLATE_NO_NEWLINE_BOTH.format(path=path)
content = self.vcs._selectively_apply_diff(path, 'FOO', diff=patch)
assert content == 'blah'


class GetRepositoryTestCase(TestCase):

def test_correct(self):
for (url, expected_name) in [
('example.com:test.git', 'test.git'),
('example.com:test', 'test'),
('ssh@example.com:test', 'test'),
('example.com:prefix/test', 'test'),
('example.com:test-with-hyphen', 'test-with-hyphen'),
('example.com:some-prefix/test-with-hyphen', 'test-with-hyphen'),
]:
assert Vcs.get_repository_name(url) == expected_name
16 changes: 16 additions & 0 deletions tests/changes/vcs/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,19 @@ def test_get_patch_hash(self):
patch_hash = vcs.get_patch_hash('HEAD')

assert isinstance(patch_hash, str) and len(patch_hash) == 40


class GetRepositoryTestCase(TestCase):

def test_correct(self):
for (url, expected_name) in [
('example.com:test.git', 'test.git'),
('example.com:test', 'test.git'),
('ssh@example.com:test.git', 'test.git'),
('ssh@example.com:test', 'test.git'),
('example.com:prefix/test.git', 'test.git'),
('example.com:prefix/test', 'test.git'),
('example.com:test-with-hyphen', 'test-with-hyphen.git'),
('example.com:some-prefix/test-with-hyphen', 'test-with-hyphen.git'),
]:
assert GitVcs.get_repository_name(url) == expected_name

0 comments on commit 4656893

Please sign in to comment.