forked from Nginlion/git-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
104 lines (84 loc) · 2.5 KB
/
command.py
File metadata and controls
104 lines (84 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
Created on Jun 9, 2014
@author: lzrak47
'''
import os
from termcolor import colored
from branch import Branch
from constants import GIT_DIR
from repository import Repository
from utils import get_all_files_in_dir, filter_by_gitignore, less_str
class Command(object):
'''
handle all commands
'''
@staticmethod
def cmd_init(workspace):
Repository.create_repository(workspace)
@staticmethod
def cmd_add(file):
if file == '.':
Repository().stage(filter_by_gitignore(get_all_files_in_dir('.', GIT_DIR)))
else:
Repository().stage([file])
@staticmethod
def cmd_rm(file, cached=False):
Repository().delete(file)
if not cached:
os.remove(file)
@staticmethod
def cmd_commit(msg):
Repository().commit(msg)
@staticmethod
def cmd_log(num, use_less=True):
res = Repository().show_log(num)
if use_less:
less_str(res)
else:
print res
@staticmethod
def cmd_status():
Repository().show_status()
@staticmethod
def cmd_branch(name, is_deleted=False):
b = Branch()
if not name:
for branch in b.get_all_branches():
print '* %s' % colored(branch, 'green') if branch == b.head_name else ' %s' % branch
elif is_deleted:
b.delete_branch(name)
else :
b.add_branch(name)
@staticmethod
def cmd_reset(commit_sha1, is_soft=False, is_hard=False):
repo = Repository()
pre_entries = dict(repo.index.entries)
repo.update_head_commit(commit_sha1)
if not is_soft:
repo.rebuild_index_from_commit(commit_sha1)
if is_hard:
repo.rebuild_working_tree(pre_entries)
@staticmethod
def cmd_checkout(branch):
b = Branch()
b.switch_branch(branch)
repo = Repository()
pre_entries = dict(repo.index.entries)
repo.rebuild_index_from_commit(repo.branch.head_commit)
repo.rebuild_working_tree(pre_entries)
@staticmethod
def cmd_diff(cached=False, use_less=True):
if cached:
res = Repository().diff_between_index_and_head_tree()
else:
res = Repository().diff_between_working_tree_and_index()
if use_less:
less_str(res)
else:
print res
@staticmethod
def cmd_push():
pass
@staticmethod
def cmd_clone():
pass