Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move git folder on path change #99

Merged
merged 4 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion shallow_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,30 @@ def get_default_config():
# CLI
#######

def move_git_folder_to_path(source_path, new_path):
"""
Moves git folder and .gitignore to the new backup directory.
"""
git_dir = os.path.join(source_path, '.git')
git_ignore_file = os.path.join(source_path, '.gitignore')

try:
shutil.move(git_dir, new_path)
shutil.move(git_ignore_file, new_path)
print(Fore.BLUE + Style.BRIGHT + "Moving git repo to new destination" + Style.RESET_ALL)
except FileNotFoundError:
pass


def prompt_for_path_update(config):
"""
Ask user if they'd like to update the backup path or not.
If yes, update. If no... don't.
"""
current_path = config["backup_path"]
print(
Fore.BLUE + Style.BRIGHT + "Current shallow-backup path -> " + Style.NORMAL + "{}".format(
config["backup_path"]) + Style.RESET_ALL)
current_path) + Style.RESET_ALL)
alichtman marked this conversation as resolved.
Show resolved Hide resolved

if prompt_yes_no("Would you like to update this?", Fore.GREEN):
print(Fore.GREEN + Style.BRIGHT +
Expand All @@ -642,6 +657,9 @@ def prompt_for_path_update(config):
abs_path) + Style.RESET_ALL)
config["backup_path"] = abs_path
write_config(config)
make_dir_warn_overwrite(abs_path)
move_git_folder_to_path(current_path, abs_path)



# custom help options
Expand Down
11 changes: 6 additions & 5 deletions tests/test_copies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from shallow_backup import _copy_file, _copy_dir
from constants import Constants

DIR_TO_BACKUP = 'shallow-backup-test-dir'
BACKUP_DIR = 'shallow-backup-test-backup-dir'
DIR_TO_BACKUP = 'shallow-backup-test-copy-dir'
BACKUP_DIR = 'shallow-backup-test-copy-backup-dir'
TEST_TEXT_FILE = 'test-file.txt'
DIRS = [DIR_TO_BACKUP, BACKUP_DIR]

Expand All @@ -26,7 +26,8 @@ def setup_method(self):
f.close()

def teardown_method(self):
shutil.rmtree(DIR_TO_BACKUP)
for directory in DIRS:
shutil.rmtree(directory)
os.remove(TEST_TEXT_FILE)

def test_copy_file(self):
Expand All @@ -37,14 +38,14 @@ def test_copy_file(self):
process =_copy_file(TEST_TEXT_FILE, BACKUP_DIR)
assert process.returncode == 0
assert os.path.isfile(TEST_TEXT_FILE)
assert os.path.isfile(os.path.join(BACKUP_DIR, TEST_TEXT_FILE)
assert os.path.isfile(os.path.join(BACKUP_DIR, TEST_TEXT_FILE))

def test_copy_dir(self):
"""
Test that copying a directory works as expected
"""
# TODO: Test that all subfiles and folders are copied.
test_dir = 'test'
test_dir = 'test/'
test_path = os.path.join(DIR_TO_BACKUP, test_dir)
os.mkdir(test_path)
process = _copy_dir(test_path, BACKUP_DIR)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_git_folder_moving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import shutil
from shallow_backup import move_git_folder_to_path, git_init_if_needed, create_gitignore_if_needed

OLD_BACKUP_DIR = 'shallow-backup-test-git-old-backup-dir'
NEW_BACKUP_DIR = 'shallow-backup-test-git-new-backup-backup-dir'
DIRS = [OLD_BACKUP_DIR, NEW_BACKUP_DIR]


class TestGitFolderCopying:
"""
Test the functionality of .git copying
"""

def setup_method(self):
for directory in DIRS:
try:
os.mkdir(directory)
except FileExistsError:
shutil.rmtree(directory)
os.mkdir(directory)

def teardown_method(self):
for directory in DIRS:
shutil.rmtree(directory)

def test_copy_git_folder(self):
"""
pyasi marked this conversation as resolved.
Show resolved Hide resolved
Test copying the .git folder and .gitignore from an old directory to a new one
"""
git_init_if_needed(OLD_BACKUP_DIR)
create_gitignore_if_needed(OLD_BACKUP_DIR)
move_git_folder_to_path(OLD_BACKUP_DIR, NEW_BACKUP_DIR, False)
assert os.path.isdir(os.path.join(NEW_BACKUP_DIR, '.git/'))
assert os.path.isfile(os.path.join(NEW_BACKUP_DIR, '.gitignore'))
assert not os.path.isdir(os.path.join(OLD_BACKUP_DIR, '.git/'))
assert not os.path.isfile(os.path.join(OLD_BACKUP_DIR, '.gitignore'))