|
| 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 | + |
0 commit comments