Skip to content

Commit

Permalink
Merge pull request #457 from JrGoodle/f-strings
Browse files Browse the repository at this point in the history
Use f strings
  • Loading branch information
JrGoodle committed May 1, 2020
2 parents 9a2e406 + 4b49697 commit 9e1adce
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 63 deletions.
9 changes: 4 additions & 5 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/clowder/clowder_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def print_status(self, fetch: bool = False) -> None:

real_path = os.path.realpath(clowder_symlink)
symlink_output = fmt.get_path('clowder.yaml')
clowder_path = fmt.remove_prefix(real_path + '/', ROOT_DIR)
clowder_path = fmt.remove_prefix(f'{real_path}/', ROOT_DIR)
path_output = fmt.get_path(clowder_path[1:-1])
print(project_output + ' ' + current_ref_output)
print(symlink_output + ' -> ' + path_output + '\n')
print(f'{project_output} {current_ref_output}')
print(f'{symlink_output} -> {path_output}\n')

def pull(self) -> None:
"""Pull clowder repo upstream changes"""
Expand Down
34 changes: 17 additions & 17 deletions src/clowder/git/project_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def herd_branch(self, url: str, branch: str, depth: int = 0, rebase: bool = Fals
return

branch_output = fmt.ref_string(branch)
branch_ref = 'refs/heads/' + branch
branch_ref = f'refs/heads/{branch}'
if self.existing_local_branch(branch):
self._herd_branch_existing_local(branch, depth=depth, rebase=rebase, fork_remote=fork_remote)
return
Expand All @@ -133,15 +133,15 @@ def herd_branch(self, url: str, branch: str, depth: int = 0, rebase: bool = Fals
return

remote_output = fmt.remote_string(self.remote)
self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output)
self._print(f' - No existing remote branch {remote_output} {branch_output}')
if fork_remote:
self.fetch(fork_remote, depth=depth, ref=branch_ref)
if self.existing_remote_branch(branch, fork_remote):
self._herd(fork_remote, branch_ref, depth=depth, fetch=False, rebase=rebase)
return

remote_output = fmt.remote_string(fork_remote)
self._print(' - No existing remote branch ' + remote_output + ' ' + branch_output)
self._print(f' - No existing remote branch {remote_output} {branch_output}')

fetch = depth != 0
self.herd(url, depth=depth, fetch=fetch, rebase=rebase)
Expand All @@ -166,7 +166,7 @@ def herd_tag(self, url: str, tag: str, depth: int = 0, rebase: bool = False) ->
return

try:
self.fetch(self.remote, ref='refs/tags/' + tag, depth=depth)
self.fetch(self.remote, ref=f'refs/tags/{tag}', depth=depth)
self._checkout_tag(tag)
except ClowderGitError:
fetch = depth != 0
Expand Down Expand Up @@ -200,14 +200,14 @@ def prune_branch_local(self, branch: str, force: bool) -> None:

branch_output = fmt.ref_string(branch)
if branch not in self.repo.heads:
self._print(' - Local branch ' + branch_output + " doesn't exist")
self._print(f" - Local branch {branch_output} doesn't exist")
return

prune_branch = self.repo.heads[branch]
if self.repo.head.ref == prune_branch:
ref_output = fmt.ref_string(truncate_ref(self.default_ref))
try:
self._print(' - Checkout ref ' + ref_output)
self._print(f' - Checkout ref {ref_output}')
self.repo.git.checkout(truncate_ref(self.default_ref))
except GitError as err:
message = colored(' - Failed to checkout ref', 'red') + ref_output
Expand All @@ -218,7 +218,7 @@ def prune_branch_local(self, branch: str, force: bool) -> None:
self._exit()

try:
self._print(' - Delete local branch ' + branch_output)
self._print(f' - Delete local branch {branch_output}')
self.repo.delete_head(branch, force=force)
except GitError as err:
message = colored(' - Failed to delete local branch ', 'red') + branch_output
Expand All @@ -237,11 +237,11 @@ def prune_branch_remote(self, branch: str, remote: str) -> None:

branch_output = fmt.ref_string(branch)
if not self.existing_remote_branch(branch, remote):
self._print(' - Remote branch ' + branch_output + " doesn't exist")
self._print(f" - Remote branch {branch_output} doesn't exist")
return

try:
self._print(' - Delete remote branch ' + branch_output)
self._print(f' - Delete remote branch {branch_output}')
self.repo.git.push(remote, '--delete', branch)
except GitError as err:
message = colored(' - Failed to delete remote branch ', 'red') + branch_output
Expand Down Expand Up @@ -277,13 +277,13 @@ def reset(self, depth: int = 0) -> None:
branch_output = fmt.ref_string(branch)
remote_output = fmt.remote_string(self.remote)
if not self.existing_remote_branch(branch, self.remote):
message = colored(' - No existing remote branch ', 'red') + remote_output + ' ' + branch_output
message = colored(' - No existing remote branch ', 'red') + f'{remote_output} {branch_output}'
self._print(message)
self._exit(message)

self.fetch(self.remote, ref=self.default_ref, depth=depth)
self._print(' - Reset branch ' + branch_output + ' to ' + remote_output + ' ' + branch_output)
remote_branch = self.remote + '/' + branch
self._print(f' - Reset branch {branch_output} to {remote_output} {branch_output}')
remote_branch = f'{self.remote}/{branch}'
self._reset_head(branch=remote_branch)

def reset_timestamp(self, timestamp: str, author: str, ref: str) -> None:
Expand Down Expand Up @@ -324,7 +324,7 @@ def start(self, remote: str, branch: str, depth: int, tracking: bool) -> None:
except ClowderGitError:
self._exit()
else:
print(' - ' + fmt.ref_string(branch) + ' already exists')
print(f' - {fmt.ref_string(branch)} already exists')
if self._is_branch_checked_out(branch):
print(' - On correct branch')
else:
Expand Down Expand Up @@ -356,12 +356,12 @@ def sync(self, fork_remote: str, rebase: bool = False) -> None:
else:
self._pull(self.remote, truncate_ref(self.default_ref))

self._print(' - Push to ' + fork_remote_output + ' ' + branch_output)
self._print(f' - Push to {fork_remote_output} {branch_output}')
command = ['git', 'push', fork_remote, truncate_ref(self.default_ref)]
try:
execute_command(command, self.repo_path, print_output=self._print_output)
except ClowderError:
message = colored(' - Failed to push to ', 'red') + fork_remote_output + ' ' + branch_output
message = colored(' - Failed to push to ', 'red') + f'{fork_remote_output} {branch_output}'
self._print(message)
self._print(fmt.command_failed_error(command))
self._exit(message)
Expand Down Expand Up @@ -421,7 +421,7 @@ def _herd_branch_existing_local(self, branch: str, depth: int = 0, rebase: bool

self._checkout_branch(branch)

branch_ref = 'refs/heads/' + branch
branch_ref = f'refs/heads/{branch}'
self.fetch(self.remote, depth=depth, ref=branch_ref)
if self.existing_remote_branch(branch, self.remote):
self._herd_remote_branch(self.remote, branch, depth=depth, rebase=rebase)
Expand All @@ -445,7 +445,7 @@ def _herd_branch_initial(self, url: str, branch: str, depth: int = 0) -> None:
self.fetch(self.remote, depth=depth, ref=branch)
if not self.existing_remote_branch(branch, self.remote):
remote_output = fmt.remote_string(self.remote)
self._print(' - No existing remote branch ' + remote_output + ' ' + fmt.ref_string(branch))
self._print(f' - No existing remote branch {remote_output} {fmt.ref_string(branch)}')
self._herd_initial(url, depth=depth)
return
self._create_branch_local_tracking(branch, self.remote, depth=depth, fetch=False, remove_dir=True)
Expand Down
46 changes: 23 additions & 23 deletions src/clowder/git/project_repo_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _checkout_branch_local(self, branch: str, remove_dir: bool = False) -> None:

branch_output = fmt.ref_string(branch)
try:
self._print(' - Checkout branch ' + branch_output)
self._print(f' - Checkout branch {branch_output}')
default_branch = self.repo.heads[branch]
default_branch.checkout()
except GitError as err:
Expand Down Expand Up @@ -97,7 +97,7 @@ def _checkout_new_repo_branch(self, branch: str, depth: int) -> None:

if not self.existing_remote_branch(branch, self.remote):
remove_directory(self.repo_path)
message = colored(' - No existing remote branch ', 'red') + remote_output + ' ' + branch_output
message = colored(' - No existing remote branch ', 'red') + f'{remote_output} {branch_output}'
self._print(message)
self._exit(fmt.parallel_exception_error(self.repo_path, message))

Expand Down Expand Up @@ -168,7 +168,7 @@ def _checkout_sha(self, sha: str) -> None:
if self.repo.head.commit.hexsha == sha:
self._print(' - On correct commit')
return
self._print(' - Checkout commit ' + commit_output)
self._print(f' - Checkout commit {commit_output}')
self.repo.git.checkout(sha)
except GitError as err:
message = colored(' - Failed to checkout commit ', 'red')
Expand All @@ -186,16 +186,16 @@ def _checkout_tag(self, tag: str) -> None:

tag_output = fmt.ref_string(tag)
if tag not in self.repo.tags:
raise ClowderGitError(msg=' - No existing tag ' + tag_output)
raise ClowderGitError(msg=f' - No existing tag {tag_output}')

try:
same_commit = self.repo.head.commit == self.repo.tags[tag].commit
is_detached = self.repo.head.is_detached
if same_commit and is_detached:
self._print(' - On correct commit for tag')
return
self._print(' - Checkout tag ' + tag_output)
self.repo.git.checkout('refs/tags/' + tag)
self._print(f' - Checkout tag {tag_output}')
self.repo.git.checkout(f'refs/tags/{tag}')
except (GitError, ValueError) as err:
message = colored(' - Failed to checkout tag ', 'red')
self._print(message + tag_output)
Expand Down Expand Up @@ -228,7 +228,7 @@ def _create_branch_local(self, branch: str) -> None:

branch_output = fmt.ref_string(branch)
try:
self._print(' - Create branch ' + branch_output)
self._print(f' - Create branch {branch_output}')
self.repo.create_head(branch)
except GitError as err:
message = colored(' - Failed to create branch ', 'red')
Expand All @@ -255,7 +255,7 @@ def _create_branch_local_tracking(self, branch: str, remote: str, depth: int,
self.fetch(remote, depth=depth, ref=branch, remove_dir=remove_dir)

try:
self._print(' - Create branch ' + branch_output)
self._print(f' - Create branch {branch_output}')
self.repo.create_head(branch, origin.refs[branch])
except (GitError, IndexError) as err:
message = colored(' - Failed to create branch ', 'red') + branch_output
Expand Down Expand Up @@ -288,7 +288,7 @@ def _create_branch_remote_tracking(self, branch: str, remote: str, depth: int) -
return

try:
self._print(' - Push remote branch ' + branch_output)
self._print(f' - Push remote branch {branch_output}')
self.repo.git.push(remote, branch)
self._set_tracking_branch(remote, branch)
except GitError as err:
Expand All @@ -313,7 +313,7 @@ def _create_remote(self, remote: str, url: str, remove_dir: bool = False) -> Non

remote_output = fmt.remote_string(remote)
try:
self._print(' - Create remote ' + remote_output)
self._print(f' - Create remote {remote_output}')
self.repo.create_remote(remote, url)
return
except GitError as err:
Expand Down Expand Up @@ -381,7 +381,7 @@ def _get_remote_tag(self, tag: str, remote: str, depth: int = 0,

tag_output = fmt.ref_string(tag)
self._remote(remote, remove_dir=remove_dir)
self.fetch(remote, depth=depth, ref='refs/tags/' + tag, remove_dir=remove_dir)
self.fetch(remote, depth=depth, ref=f'refs/tags/{tag}', remove_dir=remove_dir)

try:
return self.repo.tags[tag]
Expand Down Expand Up @@ -409,7 +409,7 @@ def _init_repo(self) -> None:
return

try:
self._print(' - Initialize repo at ' + fmt.get_path(self.repo_path))
self._print(f' - Initialize repo at {fmt.get_path(self.repo_path)}')
if not os.path.isdir(self.repo_path):
try:
os.makedirs(self.repo_path)
Expand Down Expand Up @@ -482,7 +482,7 @@ def _print_existing_remote_branch_message(self, branch: str) -> None:
except (KeyboardInterrupt, SystemExit):
self._exit()
else:
self._print(' - Tracking branch ' + branch_output + ' already exists')
self._print(f' - Tracking branch {branch_output} already exists')

@not_detached
def _pull(self, remote: str, branch: str) -> None:
Expand All @@ -494,13 +494,13 @@ def _pull(self, remote: str, branch: str) -> None:

branch_output = fmt.ref_string(branch)
remote_output = fmt.remote_string(remote)
self._print(' - Pull from ' + remote_output + ' ' + branch_output)
self._print(f' - Pull from {remote_output} {branch_output}')
command = ['git pull', remote, branch]

try:
execute_command(command, self.repo_path, print_output=self._print_output)
except ClowderError:
message = colored(' - Failed to pull from ', 'red') + remote_output + ' ' + branch_output
message = colored(' - Failed to pull from ', 'red') + f'{remote_output} {branch_output}'
self._print(message)
self._exit(message)

Expand All @@ -514,13 +514,13 @@ def _rebase_remote_branch(self, remote: str, branch: str) -> None:

branch_output = fmt.ref_string(branch)
remote_output = fmt.remote_string(remote)
self._print(' - Rebase onto ' + remote_output + ' ' + branch_output)
self._print(f' - Rebase onto {remote_output} {branch_output}')
command = ['git pull --rebase', remote, branch]

try:
execute_command(command, self.repo_path, print_output=self._print_output)
except ClowderError:
message = colored(' - Failed to rebase onto ', 'red') + remote_output + ' ' + branch_output
message = colored(' - Failed to rebase onto ', 'red') + f'{remote_output} {branch_output}'
self._print(message)
self._print(fmt.command_failed_error(command))
self._exit(message)
Expand Down Expand Up @@ -566,11 +566,11 @@ def _rename_remote(self, remote_from: str, remote_to: str) -> None:

remote_output_f = fmt.remote_string(remote_from)
remote_output_t = fmt.remote_string(remote_to)
self._print(' - Rename remote ' + remote_output_f + ' to ' + remote_output_t)
self._print(f' - Rename remote {remote_output_f} to {remote_output_t}')
try:
self.repo.git.remote('rename', remote_from, remote_to)
except GitError as err:
message = colored(' - Failed to rename remote from ', 'red') + remote_output_f + ' to ' + remote_output_t
message = colored(' - Failed to rename remote from ', 'red') + f'{remote_output_f} to {remote_output_t}'
self._print(message)
self._print(fmt.error(err))
self._exit(message)
Expand All @@ -591,7 +591,7 @@ def _set_tracking_branch(self, remote: str, branch: str, remove_dir: bool = Fals
try:
local_branch = self.repo.heads[branch]
remote_branch = origin.refs[branch]
self._print(' - Set tracking branch ' + branch_output + ' -> ' + remote_output + ' ' + branch_output)
self._print(f' - Set tracking branch {branch_output} -> {remote_output} {branch_output}')
local_branch.set_tracking_branch(remote_branch)
except GitError as err:
message = colored(' - Failed to set tracking branch ', 'red') + branch_output
Expand All @@ -618,12 +618,12 @@ def _set_tracking_branch_commit(self, branch: str, remote: str, depth: int) -> N
self.fetch(remote, depth=depth, ref=branch)

if not self.existing_local_branch(branch):
message = colored(' - No local branch ', 'red') + branch_output + '\n'
message = colored(' - No local branch ', 'red') + f'{branch_output}\n'
self._print(message)
self._exit(message)

if not self.existing_remote_branch(branch, remote):
message = colored(' - No remote branch ', 'red') + branch_output + '\n'
message = colored(' - No remote branch ', 'red') + f'{branch_output}\n'
self._print(message)
self._exit(message)

Expand All @@ -634,6 +634,6 @@ def _set_tracking_branch_commit(self, branch: str, remote: str, depth: int) -> N
message_2 = colored(' on different commit', 'red')
message = message_1 + branch_output + message_2 + '\n'
self._print(message)
self._exit(message_1)
self._exit(message)

self._set_tracking_branch(remote, branch)

0 comments on commit 9e1adce

Please sign in to comment.