def GitUpload(file, Name, Email, Password, Repository, Message="Some uploads"): origin_uris = Repository if not origin_uris.startswith(Name): origin_uris = Name + "/" + origin_uris if not origin_uris.startswith("https://github.com/"): origin_uris = "https://github.com/" + origin_uris if not origin_uris.endswith(".git"): origin_uris = origin_uris + ".git" path = os.getcwd() # Gittle.clone() 会将当前目录下的文件下载到本地,并初始化git工作路径,上传文件正常 # repo = Gittle.clone("https://github.com/fengfeng0918/gittle.git", path) # Gittle.init() 初始化git工作路径,并没有将远程仓库信息拉到本地,上传(push)文件时会将远程库清空 # repo = Gittle.init(path,origin_uri="https://github.com/fengfeng0918/gittle.git") # git init 以下流程正常!!! if not os.path.exists(".git"): local_repo = Gittle.init(path) # local_repo = Gittle.clone(origin_uris,path) bares = False #不会删除远端,重写本地 else: local_repo = Repo(path) bares = True # 不会删除远端,不重写本地 repo = Gittle(local_repo, origin_uris) repo.fetch(bare=bares) # Stage file if not isinstance(file, list): file = [file] repo.stage(file) # Commiting repo.commit(name=Name, email=Email, message=Message, files=file) # add remote repo.add_remote('origin', origin_uris) # Push repo.auth(username=Name, password=Password) # Auth for pushing repo.push()
from gittle import Gittle from config import repo_path, repo_url, key_file # Gittle repo g = Gittle(repo_path, origin_uri=repo_url) # Authentication g.auth(pkey=key_file) # Do push g.push()
class GitStat: ''' print " This class is for parsed git repository.\n\ Returns parsed dictionary filled by name of users/commiters which add/modify files.\n\ Every item has own directory which contains some counters: \ counter for adding files\n\ counter for deleting files\n\ counter for files which has been modify\n\ Next:\n \ all git commits in array\n\ directory which lines in which files was modify and how many times who\n" ''' def __init__(self,git_path,rskey= None,logger=dg): self.User = {} self.Commits = {} self.__logger = logger self.__tmp_repository = "/tmp/temporary_git_repository" if(os.path.exists(self.__tmp_repository)): self.__tmp_repository = self.__tmp_repository+"_"+datetime.datetime.now().isoformat() print git_path try: Gittle.clone(git_path,self.__tmp_repository) except: pass #self.__logger("Error could not clone repository.") #return self.__repository = Gittle(self.__tmp_repository) if rskey != None: key_file = open(rskey) self.__repository.auth(pkey=key_file) #print self.__tmp_repository self.fill_User() def parse(self,name,commit,time): #print "asd",name, commit file_pattern = re.compile("diff --git a/(.*) b/(.*)") line_pattern = re.compile("@@ (.*) (.*) @@") regex = file_pattern.search(commit) file1,file2 = regex.group(1),regex.group(2) regex = line_pattern.search(commit) line1,line2 = regex.group(1),regex.group(2) #self.__logger("Files : ",file1,file2,line1,line2) # print self.User[name].has_key(file1) if self.User[name].has_key(file1): #self.User[name][file1]['line_str'].append(line1+" "+line2) if self.User[name][file1].has_key(line1): self.User[name][file1][line1]["counter"] += 1 self.User[name][file1][line1]['time'].append(time) else: self.User[name][file1][line1] = {} self.User[name][file1][line1]["counter"] = 1 self.User[name][file1][line1]['time'] = [] self.User[name][file1][line1]['time'].append(time) self.User[name][file1]['modify'] += 1 self.User[name][file1]['time'].append(time) if os.path.isfile(self.__tmp_repository+'/'+file1): self.User[name][file1]['exist'] = True else: self.User[name][file1]['exist'] = False self.Commits[name][file1]['array_commits'].append(commit) self.Commits[name][file1]['time'].append(time) else: self.User[name][file1] = {} self.User[name][file1][line1] = {} self.User[name][file1][line1]["counter"] = 1 self.User[name][file1][line1]['time'] = [] self.User[name][file1][line1]['time'].append(time) self.User[name][file1]['modify'] = 1 self.User[name][file1]['time'] = [] self.User[name][file1]['time'].append(time) if os.path.isfile(self.__tmp_repository+'/'+file1): self.User[name][file1]['exist'] = True else: self.User[name][file1]['exist'] = False self.Commits[name][file1] = {} self.Commits[name][file1]['array_commits'] = [] self.Commits[name][file1]['array_commits'].append(commit) self.Commits[name][file1]['time'] = [] self.Commits[name][file1]['time'].append(time) return file1 def fill_User(self): for commit in self.__repository.commit_info(): sha = [commit['sha']] #print commit try: if not self.User.has_key(commit['committer']['name']): #print commit#['committer']['name'] #print self.__repository.diff(*sha)[0]['diff'] self.User[commit['committer']['name']] = {} self.Commits[commit['committer']['name']] = {} self.parse(commit['committer']['name'],self.__repository.diff(*sha)[0]['diff'],commit['time']) else: self.parse(commit['committer']['name'],self.__repository.diff(*sha)[0]['diff'],commit['time']) except: pass #return file1 def return_User(self): # print self.User return self.User def return_commits(self): return self.Commits
# /usr/bin/python # coding=utf-8 '''help me to git add changes ''' from gittle import Gittle import sys conf = [ ['/home/zhang/www/php', '[email protected]:tianyunchong/php.git'] ] for con in conf: repo = Gittle(con[0], con[1]) #拉取远程代码到本地 key_file = open("/home/zhang/.ssh/id_rsa") repo.auth(key_file) repo.pull(con[1], "origin/master") sys.exit() untracks = repo.untracked_files if not untracks: print "暂时没有文件新增!" changes = repo.modified_files print changes