-
Notifications
You must be signed in to change notification settings - Fork 206
/
docopt-git.py
80 lines (63 loc) · 1.3 KB
/
docopt-git.py
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
"""
Git 命令行
前置条件:
1. pip install gitpython
2. 安装了 git
Usage:
git status
git add [<pathspec>...]
git commit -m msg
git push
Options:
-h --help Show help.
-m --message msg Commit with message.
"""
import os
from docopt import docopt
from git.cmd import Git
def cli():
"""
git 命名程序入口
"""
args = docopt(__doc__)
git = Git(os.getcwd())
if args['status']:
handle_status(git)
elif args['add']:
handle_add(git, args['<pathspec>'])
elif args['commit']:
handle_commit(git, args['--message'])
elif args['push']:
handle_push(git)
else:
print(__doc__)
def handle_status(git):
"""
处理 status 命令
"""
cmd = ['git', 'status']
output = git.execute(cmd)
print(output)
def handle_add(git, pathspec):
"""
处理 add 命令
"""
cmd = ['git', 'add'] + pathspec
output = git.execute(cmd)
print(output)
def handle_commit(git, msg):
"""
处理 -m <msg> 命令
"""
cmd = ['git', 'commit', '-m', msg]
output = git.execute(cmd)
print(output)
def handle_push(git):
"""
处理 push 命令
"""
cmd = ['git', 'push']
output = git.execute(cmd)
print(output)
if __name__ == '__main__':
cli()