def config(self, username, email): """ | ##@函数目的: 设置Git的user name和email | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: Git客户端配置 git config --global user.name "jhuang" git config --global user.email "*****@*****.**" ssh-keygen -t rsa -C "*****@*****.**" eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa_git cd vi /root/.ssh/id_rsa.pub """ cmd = 'git config --global user.name "%s"' % (username) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) cmd = 'git config --global user.email "%s"' % (email) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) ''' 如果需要交互可以参考: https://pexpect.readthedocs.io/en/stable/overview.html#find-the-end-of-line-cr-lf-conventions http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/ import pexpect child = pexpect.spawn ('ssh [email protected]') child.sendline ('yes') child.sendline ('thzk211!') logger.info child.before # logger.info the result of the ls command. child.interact() # Give control of the child to the user. ''' return ret
def commits(self, fileLists, comment): """ | ##@函数目的: 将所有改变提交 | ##@参数说明:git commit -a是把unstaged的文件变成staged(这里不包括新建(untracked)的文件),然后commit | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ arrays = self.status() for i in fileLists: file_path = i self.add(file_path) cmd = 'git commit -m "%s"' % (comment) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) cmd = 'git add --all' ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) cmd = 'git commit -a -m "%s"' % (comment) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) return arrays
def push(self, branch): """ | ##@函数目的: 单个文件提交 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git push --progress "origin" %s' % (branch) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) return ret
def add(self, file_path): """ | ##@函数目的: 添加到本地索引 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git add %s"' % (file_path) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) return ret
def clone(self, RemoteGitiPath): """ | ##@函数目的: 克隆库 | ##@参数说明:gitpath : [email protected]:his/hisdb.git | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ ret, rc = subprocess_to_log( 'git clone --progress -v "%s" "%s"' % (RemoteGitiPath, self.LocalRepoPath), self.gitLog) return ret
def pull(self, branch): """ | ##@函数目的: pull | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git pull --progress --no-rebase -v "origin" %s' % (branch) # cmd='git pull' ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) return ret
def commit(self, comment, file_path): """ | ##@函数目的: 单个文件提交 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ self.add(file_path) comment = str(comment).encode('gbk') cmd = 'git commit -m %s %s"' % (comment, file_path) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) return ret
def status(self): """ | ##@函数目的: 获取待提交的文件列表 | ##@参数说明: | ##@返回值:数组 | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git status' ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) rs = re.findall('\s+(\w+\.\w+)', ret, re.S) # logger.info ( rs) return rs
def checkout(self, branch): """ | ##@函数目的: 切换分支 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git checkout %s' % (branch) ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) if rc == '0': logger.debug('切换分支成功...%s' % (branch)) else: raise Exception('切换GIT分支%s失败,请稍等重试!' % (branch)) return ret
def branch(self): """ | ##@函数目的: 获得分支信息 | ##@参数说明: | ##@返回值:数组,每个元素在数组内 | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ cmd = 'git branch' ret, rc = subprocess_to_log(cmd, self.gitLog, self.LocalRepoPath) ret = ret.split('\n') # logger.info ( ret) array = [] for i in range(len(ret) - 1): e = str(ret[i]).replace(' ', '') array.append(e) return array