Skip to content

Commit

Permalink
Add support for specific revisions of a git repo (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
kogi committed Feb 11, 2021
1 parent 2047762 commit 2d72dab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
12 changes: 8 additions & 4 deletions git_resource/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ git_resource(resource_name, path_or_repo, dockerfile='Dockerfile', namespace='de
* `resource_name` ( str ) – the name to use for this resource
* `path_or_repo` ( str ) – either the URL to the remote git repo, or the path to your local checkout.
If passing a repo url, a branch may be specified with a hash delimiter (i.e. `git@example.com/path/to/repo.git#myBranchNameHere`).
If no branch is specified, defaults to `master`
To use a tag, prefix the tag name with `tags/` (i.e. `tags/v1.0.01`)
To use a tag, prefix the tag name with `tags/` (i.e. `git@example.com/path/to/repo.git#tags/v1.0.01`)
To use a specific revision/sha, prefix the sha with `@` (i.e. `git@example.com/path/to/repo.git@myRevisionSha`).
If no branch or revision is specified, defaults to `master`
* `dockerfile` ( str ) – the path to your dockerfile, relative to your git repository's root
* `namespace` ( str ) – the namespace to deploy the built image to. This can be overridden within the `deployment_callback` function (see: [Custom Deployment YAML][2])
* `resource_deps` ( List [ str ] ) – a list of resources on which this resource depends
Expand Down Expand Up @@ -103,8 +104,11 @@ deploy_from_repository(resource_name, repository_url, dockerfile='Dockerfile', n
```

* `resource_name` ( str ) – the name to use for this resource
* `repository_url` ( str ) - the URL to the remote git repo. A branch may be specified with a hash delimiter (i.e. `git@example.com/path/to/repo.git#myBranchNameHere`).
If no branch is specified, defaults to `master`
* `repository_url` ( str ) - the URL to the remote git repo.
A branch may be specified with a hash delimiter (i.e. `git@example.com/path/to/repo.git#myBranchNameHere`).
To use a tag, prefix the tag name with `tags/` (i.e. `git@example.com/path/to/repo.git#tags/v1.0.01`)
To use a specific revision/sha, prefix the sha with `@` (i.e. `git@example.com/path/to/repo.git@myRevisionSha`).
If no branch or revision is specified, defaults to `master`
* `dockerfile` ( str ) - the path, relative to `directory` to the Dockerfile to be built
* `namespace` ( str ) – the namespace to deploy the built image to. This can be overridden within the `deployment_callback` function (see: [Custom Deployment YAML][2])
* `resource_deps` ( List [ str ] ) – a list of resources on which this resource depends
Expand Down
53 changes: 38 additions & 15 deletions git_resource/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,36 @@ def git_checkout(repository_url, checkout_dir=None, unsafe_mode=False):
if checkout_dir == '' or checkout_dir == None:
checkout_dir = _get_default_checkout_target(repository_url)

repository_url, _, branch = _parse_repository_url(repository_url) # split the branch name away from the repo url
repository_url, _, tree = _parse_repository_url(repository_url) # split the branch name away from the repo url

if config.tilt_subcommand == "up" or config.tilt_subcommand == "ci":
if not os.path.exists(checkout_dir): # needs clone
if branch.startswith('tags/'):
branch = branch[5:]
is_branch = tree.startswith('#')
is_revision = tree.startswith('@')
is_tag = tree.startswith('#tags/')
tree = tree[1:]

if is_tag:
tree = tree[5:]

if tree.startswith('origin/'):
tree = tree[7:]

local('git clone {repository_url} --recurse-submodules --shallow-submodules --depth 1 --branch {branch} {checkout_dir}'.format(repository_url=repository_url, branch=branch, checkout_dir=checkout_dir), quiet=True)
if not os.path.exists(checkout_dir): # needs clone
if is_branch or is_tag:
local('git clone {repository_url} --recurse-submodules --shallow-submodules --depth 1 --branch {branch} {checkout_dir}'.format(repository_url=repository_url, branch=tree, checkout_dir=checkout_dir), quiet=True)
else:
local('git clone {repository_url} --recurse-submodules --shallow-submodules {checkout_dir} && cd {checkout_dir} && git checkout {branch}'.format(repository_url=repository_url, branch=tree, checkout_dir=checkout_dir), quiet=True)
else: # dir already exists
if os.path.exists('%s/.git' % checkout_dir): # this is a git repo
has_local_changes = True # default to True for safety
if not unsafe_mode:
has_local_changes = int(str(local('cd %s && git status -sbuno | wc -l' % checkout_dir, quiet=True)).strip()) > 1 # result greater than 1 indicates local modifications are present

if unsafe_mode or not has_local_changes:
if not branch.startswith('tags/') and not branch.startswith('origin/'):
branch = 'origin/%s' % branch
if not is_tag and not is_revision:
tree = 'origin/%s' % tree

local('cd %s && git fetch -a origin && git checkout -f %s && git submodule update --init' % (checkout_dir, branch), quiet=True)
local('cd %s && git fetch -a origin && git checkout -f %s && git submodule update --init' % (checkout_dir, tree), quiet=True)
else:
fail('git_checkout() failed: local modifications present and safe_mode is enabled')

Expand Down Expand Up @@ -84,17 +95,29 @@ def deploy_from_repository(name, repository_url, dockerfile='Dockerfile', namesp


def _parse_repository_url(url):
parts = url.split('#')
if len(parts) == 1:
branch = 'master'
url = parts[0]
is_ssh = url.startswith('git@')
if is_ssh:
url = url[4:]

splitter = None
if '#' in url:
splitter = '#'
elif '@' in url:
splitter = '@'

if splitter == None:
tree = '#master'
else:
branch = parts[-1] # last item
url = "#".join(parts[:-1]) # everything else
parts = url.split(splitter)
tree = splitter + parts[-1] # last item
url = splitter.join(parts[:-1]) # everything else

if is_ssh:
url = 'git@' + url

repository_name = str(local('basename %s .git' % url, quiet=True)).strip()

return url, repository_name, branch
return url, repository_name, tree


def _get_default_checkout_target(repository_url):
Expand Down

0 comments on commit 2d72dab

Please sign in to comment.