Skip to content

Commit

Permalink
add --read-only option to add git command
Browse files Browse the repository at this point in the history
  • Loading branch information
jfischer committed May 18, 2019
1 parent 8c5ef80 commit 34e2b15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions dataworkspaces/dws.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ def rclone(ctx, role, name, config, compute_hash, source, dest):
help="Short name for this resource")
@click.option('--branch', type=str, default='master',
help="Branch of the repo to use, defaults to master.")
@click.option('--read-only', '-r', is_flag=True, default=False,
help='If specified, treat the origin repository as read-only and never push to it.')
@click.argument('path', type=str)
@click.pass_context
def git(ctx, role, name, branch, path):
def git(ctx, role, name, branch, read_only, path):
"""Add a local git repository as a resource. Subcommand of ``add``"""
ns = ctx.obj
if role is None:
Expand All @@ -283,7 +285,7 @@ def git(ctx, role, name, branch, path):
else:
role = click.prompt("Please enter a role for this resource, one of [s]ource-data, [i]ntermediate-data, [c]ode, or [r]esults", type=ROLE_PARAM)
path = abspath(expanduser(path))
add_command('git', role, name, ns.workspace_dir, ns.batch, ns.verbose, path, branch)
add_command('git', role, name, ns.workspace_dir, ns.batch, ns.verbose, path, branch, read_only)

add.add_command(git)

Expand Down
22 changes: 16 additions & 6 deletions dataworkspaces/resources/git_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ def git_move_and_add(srcabspath, destabspath, git_root, verbose):

class GitRepoResource(Resource):
def __init__(self, name, role, workspace_dir, remote_origin_url,
local_path, branch, verbose=False):
local_path, branch, read_only, verbose=False):
super().__init__('git', name, role, workspace_dir)
self.local_path = local_path
self.remote_origin_url = remote_origin_url
self.branch = branch
self.read_only = read_only
self.verbose = verbose

def to_json(self):
d = super().to_json()
d['remote_origin_url'] = self.remote_origin_url
d['branch'] = self.branch
d['read_only'] = self.read_only
return d

def local_params_to_json(self):
Expand Down Expand Up @@ -199,6 +201,8 @@ def restore(self, hashval):


def push_prechecks(self):
if self.read_only:
return
if is_git_dirty(self.local_path):
raise ConfigurationError(
"Git repo at %s has uncommitted changes. Please commit your changes before pushing." %
Expand All @@ -215,6 +219,9 @@ def push_prechecks(self):

def push(self):
"""Push to remote origin, if any"""
if self.read_only:
click.echo("Skipping push of resource %s, as it is read-only" % self.name)
return
switch_git_branch_if_needed(self.local_path, self.branch, self.verbose)
call_subprocess([GIT_EXE_PATH, 'push', 'origin', self.branch],
cwd=self.local_path, verbose=self.verbose)
Expand Down Expand Up @@ -318,7 +325,7 @@ def convert(self, value, param, ctx):

class GitRepoFactory(ResourceFactory):
def from_command_line(self, role, name, workspace_dir, batch, verbose,
local_path, branch):
local_path, branch, read_only):
"""Instantiate a resource object from the add command's
arguments"""
lpr = realpath(local_path)
Expand All @@ -327,6 +334,8 @@ def from_command_line(self, role, name, workspace_dir, batch, verbose,
if lpr.startswith(wdr):
if branch!='master':
raise ConfigurationError("Only the branch 'master' is available for resources that are within the workspace's git repository")
elif read_only:
raise ConfigurationError("The --read-only parameter is only valid for separate git repositories, not subdirectories.")
return GitRepoSubdirFactory().from_command_line(role, name,
workspace_dir, batch, verbose,
local_path)
Expand All @@ -337,22 +346,23 @@ def from_command_line(self, role, name, workspace_dir, batch, verbose,
remote_origin = get_remote_origin(local_path, verbose=verbose)

return GitRepoResource(name, role, workspace_dir,
remote_origin, local_path, branch, verbose)
remote_origin, local_path, branch, read_only, verbose)

def from_json(self, json_data, local_params, workspace_dir, batch, verbose):
"""Instantiate a resource object from the parsed resources.json file"""
assert json_data['resource_type']=='git'
return GitRepoResource(json_data['name'], json_data['role'],
workspace_dir, json_data['remote_origin_url'],
local_params['local_path'], json_data['branch'],
verbose)
json_data.get('read_only'), verbose)

def from_json_remote(self, json_data, workspace_dir, batch, verbose):
assert json_data['resource_type']=='git'
rname = json_data['name']
remote_origin_url = json_data['remote_origin_url']
default_local_path = join(workspace_dir, rname)
branch = json_data['branch']
read_only = json_data.get('read_only')
if not batch:
# ask the user for a local path
local_path = \
Expand All @@ -371,9 +381,9 @@ def from_json_remote(self, json_data, workspace_dir, batch, verbose):
local_path = default_local_path
return GitRepoResource(rname, json_data['role'],
workspace_dir, remote_origin_url,
local_path, branch, verbose)
local_path, branch, read_only, verbose)

def suggest_name(self, local_path, branch):
def suggest_name(self, local_path, branch, read_only):
return basename(local_path)


Expand Down

0 comments on commit 34e2b15

Please sign in to comment.