Skip to content

Commit

Permalink
Exit if a git repository exists on the new backup path (#171)
Browse files Browse the repository at this point in the history
Fix #168 

Verifies a Git repository doesn't already exists on destination path before trying to move the existing backup directory.
  • Loading branch information
nunomdc authored and alichtman committed Nov 9, 2018
1 parent ea8584c commit 2cf63d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions shallow_backup/git_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import git
from shutil import move
from printing import *
Expand Down Expand Up @@ -88,16 +89,30 @@ def git_add_all_commit_push(repo, message):
print_yellow_bold("No changes to commit...")


def move_git_repo(source_path, new_path):
def move_git_repo(source_path, dest_path):
"""
Moves git folder and .gitignore to the new backup directory.
Exits if there is already a git repo in the directory.
"""
dest_git_dir = os.path.join(dest_path, '.git')
dest_git_ignore = os.path.join(dest_path, '.gitignore')
git_exists = os.path.exists(dest_git_dir)
gitignore_exists = os.path.exists(dest_git_ignore)
if git_exists or gitignore_exists:
print_red_bold("Evidence of a git repo has been detected.")
if git_exists:
print_path_red("A git repo already exists here:", dest_git_dir)
if gitignore_exists:
print_path_red("A gitignore file already exists here:", dest_git_ignore)
print_red_bold("Exiting to prevent accidental deletion of user data.")
sys.exit()

git_dir = os.path.join(source_path, '.git')
git_ignore_file = os.path.join(source_path, '.gitignore')

try:
move(git_dir, new_path)
move(git_ignore_file, new_path)
move(git_dir, dest_path)
move(git_ignore_file, dest_path)
print_blue_bold("Moving git repo to new location.")
except FileNotFoundError:
pass
4 changes: 2 additions & 2 deletions shallow_backup/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def prompt_for_path_update(config):
print_green_bold("Enter relative or absolute path:")
abs_path = expand_to_abs_path(input())
print_path_blue("\nUpdating shallow-backup path to:", abs_path)
config["backup_path"] = abs_path
write_config(config)
mkdir_warn_overwrite(abs_path)
move_git_repo(current_path, abs_path)
config["backup_path"] = abs_path
write_config(config)


def prompt_for_git_url(repo):
Expand Down

0 comments on commit 2cf63d8

Please sign in to comment.