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

pull request for dz-unix-cli #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
38 changes: 38 additions & 0 deletions rm_git_untracked_files.py
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions rm_untracked_files
Original file line number Diff line number Diff line change
@@ -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