diff --git a/README.md b/README.md index 830631a..274839e 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,27 @@ присылайте пулл реквесты с решением для SVN или с более элегантным подходом. См. также: [пост про домашние задания](http://clubs.ya.ru/4611686018427468886/replies.xml?item_no=450). + +----------------------------------------------- +МОЕ РЕШЕНИЕ +============ + +rm_untracked_files +=================== +Cкрипт, перемещающий в корзину все untracked файлы для cvs git / subversion + +Help: rm_untracked_files -h + +rm_git _untracked_files.py +=========================== +That's what we would do without git-ls ;) + +Скрипт, который удаляет все git untracked файлы репозитория, демонстрируя использование pygit-биндингов на python к библиотеке [libgit2](https://github.com/libgit2) + +Для использования следовать инструкциям: + + https://github.com/libgit2/pygit2 + +Использование: + + $ ./rm_git_untracked_files.py [-h] [-p REPO_PATH] [-t TRASH_PATH] \ No newline at end of file diff --git a/rm_git_untracked_files.py b/rm_git_untracked_files.py new file mode 100755 index 0000000..2e1394e --- /dev/null +++ b/rm_git_untracked_files.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + +import os, re, shutil, argparse, getpass +from pygit2 import Repository + +user = getpass.getuser() +parser = argparse.ArgumentParser(description='Print your git repository path.') +parser.add_argument('-p', dest='repo_path', default='.', help='your repository path') +parser.add_argument('-t', dest='trash_path', default='/home/' + user + '/.local/share/Trash/files/', help='Trash path') +args = parser.parse_args() + +user = getpass.getuser() + +repo_path = args.repo_path +trash_path = args.trash_path + +def remove_untracked_files(tracked_files): + files_list = [] + templ = re.compile(".git") + for root, dirs, files in os.walk(repo_path): + for f in files: + if not re.search(templ, root): + file_ = os.path.join(root, f) + if file_ not in tracked_files: + new_name = trash_path + f + shutil.move(file_, new_name) + +repo_git_path = repo_path + '.git' +repo = Repository(repo_git_path) + +index = repo.index +index.read() + +tracked_files = [] +for entry in index: + tracked_files.append(repo_path + entry.path) + +remove_untracked_files(tracked_files) diff --git a/rm_untracked_files b/rm_untracked_files new file mode 100755 index 0000000..9edcb6e --- /dev/null +++ b/rm_untracked_files @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +print_help() { + cat << EOF + Usage: rm_untracked_files [OPTIONS] [ARGUMENTS] + + OPTIONS: + -r repository path + -d destination path + -s version control system type + -h print help message + + #without arg -d script move untracked files to Trash + + EXAMPLES: + rm_untracked_files -p /repo -t /repo_ -s git + rm_untracked_files -p /repo -t /repo_ -s svn + rm_untracked_files -p /repo -t /.local/share/Trash/files/ -s git + rm_untracked_files -p /repo -s git + rm_untracked_files -p /repo -s svn +EOF +} + +DEST=/.local/share/Trash/files/ + +while getopts "r: d: s: :h" OPTION +do + case $OPTION in + h) + print_help + exit 1 + ;; + r) + REP=$OPTARG + ;; + d) + DEST=$OPTARG + ;; + s) + CVS_TYPE=$OPTARG + ;; + esac +done + +cd $REPO + +if [ $CVS_TYPE = "git" ]; then + git ls-files -o --exclude-standard --exclude-per-directory=.gitignore + --directory | xargs -I {} mv {} $DEST + +elif [ $CVS_TYPE = "svn" ]; then + svn st --no-ignore | grep '^?' | awk '{print $2}' | xargs mv $DEST +fi \ No newline at end of file