Skip to content

Commit 22c96bc

Browse files
committed
编写git命令行封装
1 parent a5c44da commit 22c96bc

File tree

2 files changed

+137
-5
lines changed

2 files changed

+137
-5
lines changed

git_cmd.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# coding: utf-8
2+
3+
import subprocess
4+
import os
5+
6+
7+
class git_log():
8+
"""git log"""
9+
10+
11+
class git_cmd():
12+
"""git command"""
13+
def __init__(self, path, repository_url):
14+
# 初始化数据
15+
self.url = repository_url
16+
self.output = ''
17+
self.error = ''
18+
self.command = ''
19+
self.remotes = []
20+
self.branches = {}
21+
try:
22+
os.chdir(path)
23+
except Exception, error:
24+
print error
25+
26+
# cwd 参数只有windows系统下才生效
27+
self.cmd = subprocess.Popen(args="git --version",
28+
shell=True,
29+
stdin=subprocess.PIPE,
30+
stdout=subprocess.PIPE,
31+
stderr=subprocess.PIPE,
32+
cwd=path)
33+
self._check_result()
34+
35+
def _check_result(self):
36+
"""check last cmd"""
37+
# 检查上一条命令是否结束
38+
self.output, self.error = self.cmd.communicate(input=None)
39+
40+
def _run_cmd(self):
41+
"""run cmd and return output and error"""
42+
# 运行git命令并返回输出和错误码
43+
self.cmd.communicate(stdin=self.command)
44+
self._check_result()
45+
return self.output, self.error
46+
47+
def clone(self):
48+
"""git clone """
49+
# 拉取远端仓代码到本地
50+
# git clone [--template=<template_directory>]
51+
# [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
52+
# [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
53+
# [--dissociate] [--separate-git-dir <git dir>]
54+
# [--depth <depth>] [--[no-]single-branch]
55+
# [--recursive | --recurse-submodules] [--] <repository>
56+
# [<directory>]
57+
self.command = "git clone " + self.url
58+
return self._run_cmd()
59+
60+
def branch_update(self):
61+
"""git branch """
62+
# 更新分支字典
63+
self.command = "git branch -r"
64+
self._run_cmd()
65+
self.remote_update()
66+
# 按照'\n'分割字符串
67+
self.output.split('\n')
68+
for remote in self.remotes:
69+
# 给一个远端添加一个key,其数据为一个列表
70+
self.branches[remote] = []
71+
for branch in self.output[0, -1]:
72+
if remote in branch:
73+
# 在返回的分支字符串中删除远端和/
74+
branch.split(remote + '/')
75+
# 添加分支到列表
76+
self.branches[remote].append(branch)
77+
return self.output, self.error
78+
79+
def remote_update(self):
80+
"""get remote """
81+
# 获取远端列表
82+
self.command = "git remote "
83+
self._run_cmd()
84+
remotes = self.output.split('\n')
85+
# 清空原有列表
86+
for index in range(len(self.remotes)):
87+
self.remotes.pop()
88+
for remote in remotes:
89+
self.remotes.append(remote)
90+
return self.output, self.error
91+
92+
def add_remote(self, name, remote_url):
93+
"""add remote """
94+
# 添加远端
95+
self.command = "git remote add " + name + remote_url
96+
return self._run_cmd()
97+
98+
def pull_current_branch(self, remote):
99+
"""git pull """
100+
# 从指定远端拉取当前分支
101+
# git pull [options] [<repository> [<refspec>...]]
102+
self.command = "git pull " + remote
103+
return self._run_cmd()
104+
105+
def pull_all_branch(self, remote):
106+
"""pull all branch"""
107+
# 从指定远端拉取所有分支代码
108+
# 0. 记录当前分支
109+
# 1. 更新所有远端和分支
110+
# 2. 循环切换分支和拉取分支
111+
# 3. 切回原分支
112+
# 待编辑
113+
114+
return self._run_cmd()
115+
116+
117+
118+
119+

subprocess_git.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,39 @@
2323
branch = "master"
2424

2525
# 切换路径
26-
dir = "/home/wmm/studydata"
26+
dir = "/home/wmm/studydata/python_base_subprocess_test_repository"
2727
os.chdir(dir)
28+
os.getcwdu()
2829

2930
# 端口自测试
3031
# subprocess._demo_posix()
3132

3233
active = "clone "
3334
para = "--bare"
3435
url = "git@github.com:PythonObject/python_base_subprocess_test_repository.git"
35-
command = format("" + bash + ' ' + active +' ' + '' + url)
36+
37+
# command = format("" + bash + ' ' + active +' ' + '' + url)
38+
command = "git log"
3639
pro = subprocess.Popen(
3740
args=command,
3841
shell=True,
3942
stdin=None,
4043
stderr=subprocess.PIPE,
4144
stdout=subprocess.PIPE
4245
)
46+
print pro.pid
47+
48+
49+
50+
# pro.returncode
51+
#   该属性表示子进程的返回状态,returncode可能有多重情况:
52+
# None —— 子进程尚未结束;
53+
# ==0 —— 子进程正常退出;
54+
# > 0—— 子进程异常退出,returncode对应于出错码;
55+
# < 0—— 子进程被信号杀掉了。
4356

4457
# poll 用来检查新创建的进程是否结束
45-
while pro.poll() == None:
58+
while pro.poll() is None:
4659
print pro.stdout.readline()
4760

4861
print "return code:"
@@ -51,8 +64,8 @@
5164
print pro.stderr.readline()
5265
print "output:"
5366
print pro.stdout.readline()
54-
55-
# pro.kill()
67+
print pro.returncode
68+
# pro.terminate()
5669

5770

5871

0 commit comments

Comments
 (0)