Skip to content

Commit

Permalink
fix git command for dep which points to a tag (#481)
Browse files Browse the repository at this point in the history
* fix git command for dep which points to taga

* throw error if dbt can't check out the ish

* fix test
  • Loading branch information
drewbanin authored Jul 6, 2017
1 parent 852dc49 commit 82580dc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
30 changes: 25 additions & 5 deletions dbt/clients/git.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dbt.clients.system import run_cmd
from dbt.logger import GLOBAL_LOGGER as logger
import dbt.exceptions


def clone(repo, cwd, dirname=None):
Expand All @@ -11,17 +12,36 @@ def clone(repo, cwd, dirname=None):
return run_cmd(cwd, clone_cmd)


def checkout(cwd, branch=None):
def list_tags(cwd):
out, err = run_cmd(cwd, ['git', 'tag', '--list'])
tags = set(out.decode('utf-8').strip().split("\n"))
return tags


def checkout(cwd, repo, branch=None):
if branch is None:
branch = 'master'

remote_branch = 'origin/{}'.format(branch)

logger.info(' Checking out branch {}.'.format(branch))

run_cmd(cwd, ['git', 'remote', 'set-branches', 'origin', branch])
run_cmd(cwd, ['git', 'fetch', '--depth', '1', 'origin', branch])
run_cmd(cwd, ['git', 'reset', '--hard', remote_branch])
run_cmd(cwd, ['git', 'fetch', '--tags', '--depth', '1', 'origin', branch])

tags = list_tags(cwd)

# Prefer tags to branches if one exists
if branch in tags:
spec = 'tags/{}'.format(branch)
else:
spec = 'origin/{}'.format(branch)

out, err = run_cmd(cwd, ['git', 'reset', '--hard', spec])
stderr = err.decode('utf-8').strip()

if stderr.startswith('fatal:'):
dbt.exceptions.bad_package_spec(repo, branch, stderr)
else:
return out, err


def get_current_sha(cwd):
Expand Down
7 changes: 6 additions & 1 deletion dbt/clients/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ def run_cmd(cwd, cmd):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

return proc.communicate()
out, err = proc.communicate()

logger.debug('STDOUT: "{}"'.format(out))
logger.debug('STDERR: "{}"'.format(err))

return out, err
6 changes: 6 additions & 0 deletions dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ def missing_sql_where(model):
model,
"Model '{}' is materialized as 'incremental', but does not have a "
"sql_where defined in its config.".format(model.get('unique_id')))


def bad_package_spec(repo, spec, error_message):
raise RuntimeException(
"Error checking out spec='{}' for repo {}\n{}".format(
spec, repo, error_message))
2 changes: 1 addition & 1 deletion dbt/task/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __pull_repo(self, repo, branch=None):

dependency_path = os.path.join(modules_path, folder)
start_sha = dbt.clients.git.get_current_sha(dependency_path)
dbt.clients.git.checkout(dependency_path, branch)
dbt.clients.git.checkout(dependency_path, repo, branch)
end_sha = dbt.clients.git.get_current_sha(dependency_path)

if exists:
Expand Down
9 changes: 6 additions & 3 deletions test/integration/023_exit_codes_test/test_exit_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,12 @@ def project_config(self):

@attr(type='postgres')
def test_deps(self):
_, success = self.run_dbt_and_check(['deps'])
# TODO : https://github.com/fishtown-analytics/dbt/issues/463
# self.assertFalse(success)
# this should fail
try:
_, success = self.run_dbt_and_check(['deps'])
self.assertTrue(False)
except RuntimeError as e:
pass

class TestExitCodesSeed(DBTIntegrationTest):
@property
Expand Down

0 comments on commit 82580dc

Please sign in to comment.