def read_repo(self, git=None, ls=None): clone_path = path.join(self.dir_base, str(uuid.uuid4()) if git is not None else 'upstream') print('github url', git if git is not None else self.upstream) git_url = urlparse(git if git is not None else self.upstream) clone_url = None splitted_path = git_url.path.strip('/').split('/') if len(splitted_path) > 2: clone_url = git_url.scheme + '://' + git_url.netloc + '/' + splitted_path[0] + '/' + splitted_path[1] + '.git' else: clone_url = git if git is not None else self.upstream print('clone url', clone_url) # cloning the repository Gittle.clone(clone_url, clone_path) if len(splitted_path) > 2: readme_file = self.find_readme(clone_path, '/'.join(str(x) for x in splitted_path[2:])) else: readme_file = self.find_readme(clone_path) print(clone_url, readme_file) try: with open(path.join(clone_path, readme_file)) as f: soup = BeautifulSoup(mistune.markdown(f.read()), 'html.parser') self.save_md(soup, True if git is None else False, ls) self.stdout.write(self.style.SUCCESS('Successfully read the upstream')) except Exception as exp: print('An error happened while reading the repo.', exp)
def gitPull(): logging.info('*** Pulling changed files from remote repository ***') destinations = [dataDestination, PDFdestination] remotes = ['dataRemote', 'PDFRemote'] for d, r in zip(destinations, remotes): repo = Gittle(d, origin_uri=config.get('Git', r)) repo.pull()
def get_repo_info(repo_path,is_my_repo): if not os.path.exists(repo_path): return dict( info=[], error=True, msg='repo not exist' ) git_path = os.path.join(repo_path, '.git') if not os.path.exists(git_path): return dict( info=[], error=True, msg='.git file lost' ) repo = Gittle(repo_path) list_dir = os.listdir(repo_path) folders = [] print list_dir for i in range(len(list_dir)): if os.path.isdir(os.path.join(repo_path, list_dir[i])): folders.append(list_dir[i]) return dict( commit_info=repo.commit_info(start=0), remote_branches=repo.remote_branches, local_branches=repo.branches, active_branch=repo.active_branch, folders=folders, is_my_repo=is_my_repo )
def git_checkout(args): if len(args) == 1: repo = Gittle('.') #repo.checkout('refs/heads/{0}'.format(args[0])) repo.switch_branch('{0}'.format(args[0])) else: print command_help['checkout']
def getFiles(self): try: if os.path.exists(self.tmp_path): shutil.rmtree(self.tmp_path) Gittle.clone(self.repo, self.tmp_path) except Exception, e: print e sys.exit(1)
def _clone_sync_file(self): try: if os.path.exists(self.sync_tmp_file): shutil.rmtree(self.sync_tmp_file) Gittle.clone(self.sync_file_project, self.sync_tmp_file) except Exception, e: print e sys.exit(1)
def __init__(self, uri, branch="master", project_name="project", specific_sha=None, threshold=False, correction=False): self._data_frame = None self.files = {} self.project_name = project_name self.git_repository = uri git_url = re.compile(GIT_URL) _uri_safe = ''.join([c for c in uri if c.isalnum()]) self.repo_path = os.path.join(TMP_DIR, _uri_safe) self.__tmp_repository = self.repo_path self.index_sha = 0 self.size = 0 self.__first = True self.__spec_file = [] self.specific_sha = specific_sha if os.path.exists(self.repo_path): #dont use cached repo shutil.rmtree(self.repo_path) if os.path.exists("diff_{0}.txt".format(self.project_name)): os.remove("diff_{0}.txt".format(self.project_name)) is_url = git_url.search(uri) if is_url is None: self.__repository = Gittle(self.git_repository) self.__tmp_repository = self.git_repository else: if self.git_repository.find(".git") < 0: LOGGER.info(r"Must end .git i will add manualy") self.git_repository += ".git" try: LOGGER.info(r'Cloning git repo: {0}'.format(self.repo_path)) Gittle.clone(self.git_repository, self.__tmp_repository) except InvalidRemoteUrl as err: raise Exception(r"Could not clone repository! Is not url." " Error: {0}".format(err)) except ValueError as err: raise Exception(r"Is not url." " Error: {0}".format(err)) except KeyError as err: raise Exception(r"Could not clone repository." " Error: {0}".format(err)) except RefFormatError: n_path = "/tmp/{0}".format(_uri_safe) if os.path.exists(n_path): #dont use cached repo shutil.rmtree(n_path) if branch is None: os.system("git clone {0} {1} 2>&1".format(uri, n_path)) else: os.system("git clone -b {0} {1} {2} 2>&1" .format(branch, uri, n_path)) self.__tmp_repository = n_path self.__repository = Gittle(self.__tmp_repository, origin_uri=uri) self.__repository.DEFAULT_BRANCH = branch if branch not in self.__repository.branches: LOGGER.error("Branch {0} is no in {1}".format(branch, uri)) raise Exception("Branch {0} is no in {1}".format(branch, uri)) self.__fill_data(branch, specific_sha) self.eval_commit_to_future(threshold, correction)
def git_commit(args): if len(args) == 3: try: repo = Gittle('.') print repo.commit(name=args[1],email=args[2],message=args[0]) except: print 'Error: {0}'.format(sys.exc_value) else: print command_help['commit']
def __init__(self, path): try: self.gittle = Gittle(path) except NotGitRepository: self.gittle = Gittle.init(path) # Dulwich repo self.repo = self.gittle.repo self.path = path
def git_pull(args): if len(args) <= 1: repo = Gittle('.') url = args[0] if len(args)==1 else repo.remotes.get('origin','') if url: repo.pull(origin_uri=url) else: print 'No pull URL.' else: print command_help['git pull']
def create_work_path(): for root, dirs, files in os.walk(repo_path, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) if not os.path.exists(repo_path): os.mkdir(repo_path) if not os.path.exists("%s/.git" % repo_path): Gittle.init(repo_path) return None
def clone(repo_info): repo_context = None try: repo_context = AgentGitHandler.create_git_repo_context(repo_info) if os.path.isdir(repo_context.local_repo_path): # delete local repo path if exists cartridgeagentutils.delete_folder_tree( repo_context.local_repo_path) # create local repo path cartridgeagentutils.create_dir(repo_context.local_repo_path) #TODO: remove gittle stuff auth = AgentGitHandler.create_auth_configuration(repo_context) if auth is not None: # authentication is required, use Gittle gittle_repo = Gittle.clone(repo_context.repo_url, repo_context.local_repo_path, auth=auth) repo = Repo(repo_context.local_repo_path) else: # authentication is not required, use GitPython repo = Repo.clone_from(repo_context.repo_url, repo_context.local_repo_path) gittle_repo = Gittle(repo_context.local_repo_path) repo_context.cloned = True repo_context.gittle_repo = gittle_repo repo_context.repo = repo AgentGitHandler.add_repo_context(repo_context) AgentGitHandler.log.info( "Git clone operation for tenant %r successful" % repo_context.tenant_id) except urllib2.URLError: AgentGitHandler.log.exception( "Accessing remote git repository failed for tenant %r" % repo_context.tenant_id) except OSError: AgentGitHandler.log.exception( "Permission denied for repository path for tenant %r" % repo_context.tenant_id) except: AgentGitHandler.log.exception( "Git clone operation for tenant %r failed" % repo_context.tenant_id) finally: return repo_context
def update_repo(cookie): # TODO: add commit info to the CookieCutter model if not os.path.isdir(cookie.repo_path): repo = Gittle.clone(cookie.url, cookie.repo_path) else: repo = Gittle(cookie.repo_path, cookie.url) repo.pull() cookie.options = {'repo_name': 'your repo'} options_file = os.path.join(cookie.repo_path, 'cookiecutter.json') if os.path.isfile(options_file): cookie.options.update(json.load(open(options_file))) cookie.save()
def init(path): try: repo = Gittle.init(path) return repo except: AgentGitHandler.log.exception("Initializing local repo at %r failed" % path) raise Exception("Initializing local repo at %r failed" % path)
def init_repo_if_empty(self,repo_name): repopath=os.path.join(self.view['repo'].base,repo_name) self.g= Gittle.init(repopath,bare=False ) self.g.commit('name','email','initial commit') self.view['repo'].text=repo_name console.hud_alert('Repo {} created'.format(repo_name)) self.did_select_repo(self.view['repo'])
def pull(self): ''' pull the latest version ''' self.clone() self.repo = Gittle(self.repo_path, origin_uri=self.repo_url) self.repo.pull()
def init_repo_if_empty(self, repo_name): repopath = os.path.join(self.view["repo"].base, repo_name) self.g = Gittle.init(repopath, bare=False) self.g.commit("name", "email", "initial commit") self.view["repo"].text = repo_name console.hud_alert("Repo {} created".format(repo_name)) self.did_select_repo(self.view["repo"])
def git_push(args): import argparse parser = argparse.ArgumentParser(prog='git push' , usage='git push [http(s)://<remote repo>] [-u username[:password]]' , description="Push to a remote repository") parser.add_argument('url', type=str, nargs='?', help='URL to push to') parser.add_argument('-u', metavar='username[:password]', type=str, required=False, help='username[:password]') result = parser.parse_args(args) user, sep, pw = result.u.partition(':') if result.u else (None,None,None) repo = Gittle('.') #Try to get the remote origin if not result.url: result.url = repo.remotes.get('origin','') branch_name = os.path.join('refs','heads', repo.active_branch) #'refs/heads/%s' % repo.active_branch print "Attempting to push to: {0}, branch: {1}".format(result.url, branch_name) if user: if not pw: pw = getpass.getpass('Enter password for {0}: '.format(user)) opener = auth_urllib2_opener(None, result.url, user, pw) print porcelain.push(repo.repo, result.url, branch_name, opener=opener) else: print porcelain.push(repo.repo, result.url, branch_name)
def get_readme(project): """ This function tries to get the content of the readme file. If it does, then it updates the description with the content of the latest readme. It runs every time a project is saved, on post save hook. """ repos = project.repository_set.all() if repos: for repository in repos: if repository.url: path = ("/tmp/%s/") % "".join(random.choice(string.lowercase) for i in range(20)) try: repo = Gittle.clone(repository.url, path, bare=True) except: # We use update, because we want to bypass # the post_save function. from index.models import Project Project.objects.filter(id=project.id).update(public=False) pass else: readme = repo.file_versions("README.md") if readme: readme = readme[0].get("data") # We use update, because we want to bypass # the post_save function. from index.models import Project Project.objects.filter(id=project.id).update(description=readme) shutil.rmtree(repo.path) return True return False
def init_repo_if_empty(self,repo_name,gitpath): if not os.path.exists(gitpath): self.g= Gittle.init(gitpath,bare=False ) self.g.commit('name','email','initial commit') self.view['repo'].text=repo_name console.hud_alert('Repo {} created'.format(repo_name)) self.refresh()
def git_pull(self, TMP_DIR, CODE_DIR): ''' git pull code use commit id tag you code lastest = [info['sha'] for info in repo.commit_info()[1:3]] print((repo.diff(*lastest, diff_type='classic'))) ''' print("========%s: git pull for env:test=========" % self.PRO_NAME) repo = Gittle(self.CODE_DIR, self.REPO_URL) command = 'cd %s && git pull' % self.CODE_DIR os.system('command') COMMIT_ID = repo.commit()[0:-30] CVER = "%s_%s_%s" % (self.PRO_NAME, self.CTIME, COMMIT_ID) CODE_TMP = '%s/%s' % (TMP_DIR, CVER) shutil.copytree(CODE_DIR, CODE_TMP) echo_info("git pull success") return CVER
def clone(repo_info): repo_context = None try: repo_context = AgentGitHandler.create_git_repo_context(repo_info) #create the directory if it doesn't exist if not os.path.isdir(repo_context.local_repo_path): cartridgeagentutils.create_dir(repo_context.local_repo_path) auth = AgentGitHandler.create_auth_configuration(repo_context) if auth is not None: # authentication is required, use Gittle gittle_repo = Gittle.clone(repo_context.repo_url, repo_context.local_repo_path, auth=auth) repo = Repo(repo_context.local_repo_path) else: # authentication is not required, use GitPython repo = Repo.clone_from(repo_context.repo_url, repo_context.local_repo_path) gittle_repo = Gittle(repo_context.local_repo_path) repo_context.cloned = True repo_context.gittle_repo = gittle_repo repo_context.repo = repo AgentGitHandler.add_repo_context(repo_context) AgentGitHandler.log.info("Git clone operation for tenant %r successful" % repo_context.tenant_id) except urllib2.URLError: AgentGitHandler.log.exception("Accessing remote git repository failed for tenant %r" % repo_context.tenant_id) except OSError: AgentGitHandler.log.exception("Permission denied for repository path for tenant %r" % repo_context.tenant_id) except: AgentGitHandler.log.exception("Git clone operation for tenant %r failed" % repo_context.tenant_id) finally: return repo_context
def init_repo_if_empty(self, repo_name): repopath = os.path.join(self.view['repo'].base, repo_name) self.g = Gittle.init(repopath, bare=False) self.g.commit('name', 'email', 'initial commit') self.view['repo'].text = repo_name console.hud_alert('Repo {} created'.format(repo_name)) self.did_select_repo(self.view['repo'])
def get_requirements(project): """ This function tries to get the content of the requirements file. If it does, then it updates the requirements. In case we cant access the repository we will try to see if the user has uploaded a requirements.txt by himself. It runs every time a project is saved, on post save hook. """ repos = project.repository_set.all() if repos: for repository in repos: if repository.url: path = ("/tmp/%s/") % "".join([random.choice(string.lowercase) for i in range(20)]) try: repo = Gittle.clone(repository.url, path, bare=True) except: # We use update, because we want to bypass # the post_save function. from index.models import Project Project.objects.filter(id=project.id).update(public=False) else: requirements = repo.file_versions("requirements.txt") if requirements: requirements = requirements[0].get("data") for r in requirements.split("\n"): parse_dependencies(r, project) shutil.rmtree(repo.path) return True if project.dependency_file: if os.path.isfile(project.dependency_file.path): for l in project.dependency_file.readlines(): parse_dependencies(l, project) return True return False
def gitPush(): logging.info('*** Versioning files and pushing to remote repository ***') destinations = [dataDestination, PDFdestination] remotes = ['dataRemote', 'PDFRemote'] for d, r in zip(destinations, remotes): repo = Gittle(d, origin_uri=config.get('Git', r)) repo.stage(repo.pending_files) repo.commit(message=commitMessage) repo.push()
def git_branch(args): if len(args) == 0: repo = Gittle('.') active = repo.active_branch for key, value in repo.branches.items(): print ('* ' if key == active else '') + key, value else: print command_help['branch']
class Git(Base): ''' a git vcs object ''' def __init__(self, repo_url, repo_path): super(Git, self).__init__(repo_url, repo_path) self._cloned = path.exists(repo_path) def clone(self, key_file=None, username=None, password=None): ''' clone the git repo :param key_file: string : None location of private key if you want to connect using RSA :param username: string : None username if you wnat to connect using basic authentication :param password: string : None password if you wnat to connect using basic authentication ''' if self._cloned is False: if key_file is not None: # Authentication with RSA private key key_file = open(key_file) Gittle.clone(self.repo_url, self.repo_path, auth=GittleAuth(pkey=key_file)) elif username is not None and password is not None: # With username and password Gittle.clone(self.repo_url, self.repo_path, auth=GittleAuth(username=username, password=password)) else: # Without anything , is it even possible? Gittle.clone(self.repo_url, self.repo_path) def pull(self): ''' pull the latest version ''' self.clone() self.repo = Gittle(self.repo_path, origin_uri=self.repo_url) self.repo.pull() def push(self): ''' push to the remote repository ''' raise NotImplementedError
def init(path): try: repo = Gittle.init(path) return repo except: AgentGitHandler.log.exception( "Initializing local repo at %r failed" % path) raise Exception("Initializing local repo at %r failed" % path)
def _get_repo(self): try: repopath = self._repo_path() repobase = self._find_repo(repopath) if repobase: return Gittle(repobase) except: return None
def git_remote(args): '''List remote repos''' if len(args) == 0: repo = Gittle('.') for key, value in repo.remotes.items(): print key, value else: print command_help['remote']
def git_init(self): try: # Init repository logger.debug("Local repository path:{}, Git server url: {}".format( conf.git.local_repository_path, conf.git.git_server_url)) self.repo = Gittle(conf.git.local_repository_path, origin_uri=conf.git.git_server_url) logger.info("Pulling from git..") # Update local working copy self.repo.pull() logger.info("GitGittle - Git is up to date !") except Exception as exc: logger.error( "GitGittle - Failed to initialize Git. Reason: {}".format( exc.message)) raise GitInitError(exc.message)
def getRepo(path): from dulwich.errors import NotGitRepository repo = None try: repo = Gittle(path) except NotGitRepository as e: print e return repo
def is_git_repo(path): ''' 判断一个路径是不是git仓库 :param path: 要判断的路径 :return: 是否是git路径 ''' try: repo = Gittle(path) return True except Exception, e: return False
def git_clone(args): if len(args) > 0: url = args[0] repo = Gittle.clone(args[0], args[1] if len(args)>1 else '.', bare=False) #Set the origin config = repo.repo.get_config() config.set(('remote','origin'),'url',url) config.write_to_path() else: print command_help['clone']
def git_clone(args): if len(args) > 0: url = args[0] repo = Gittle.clone(args[0], args[1] if len(args)>1 else os.path.split(args[0])[-1].rstrip('.git'), bare=False) #Set the origin config = repo.repo.get_config() config.set(('remote','origin'),'url',url) config.write_to_path() else: print(command_help['clone'])
def git_clone(args): if len(args) > 0: url = args[0] repo = Gittle.clone(args[0], args[1] if len(args) > 1 else ".", bare=False) # Set the origin config = repo.repo.get_config() config.set(("remote", "origin"), "url", url) config.write_to_path() else: print command_help["clone"]
def git_status(args): if len(args) == 0: repo = Gittle('.') status = porcelain.status(repo.repo) print status #repo.diff_working() #repo.diff(diff_type='changes') #print repo.modified_files.intersection(repo.added_files) #repo.tracked_files.intersection(repo.added_files) #print repo.added_files else: print command_help['git_staged']
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 handle(self, *args, **options): projects = Project.objects.all() for project in projects: print('Checking {0} for new commits'.format(project)) if project.git_url: repo_path = '/tmp/' + project.slug try: repo = Gittle(repo_path, project.git_url) repo.pull() except: try: repo = Gittle.clone(project.git_url, repo_path) except: # put some logging here repo = None if repo: new_commits = [] for commit in repo.commit_info(): try: prev_commit = repo.get_previous_commit( commit['sha']) time = (datetime.fromtimestamp(commit['time']) + timedelta(hours=commit['timezone'] / (60 * 60))).replace( tzinfo=pytz.utc) try: user_author = get_user_model().objects.get( email=commit['author']['email']) string_author = None except: string_author = commit['author']['name'] user_author = None summary = commit['message'].split('\n')[0][:45] pcommit = ProjectCommit.objects.create( project=project, chash=commit['sha'], message=commit['message'], summary=summary, user_author=user_author, string_author=string_author, created=time, time=time, diff=repo.diff(commit['sha'], prev_commit).next()['diff']) print(pcommit, ' added.') new_commits.append(pcommit) except: pass
def getRepo(path): ''' get local repo. ''' from dulwich.errors import NotGitRepository repo = None try: repo = Gittle(path) except NotGitRepository as e: info_logger.info(e) return repo
class GitGittle(BaseGit): def __init__(self): self.repo = None def git_init(self): try: # Init repository logger.debug("Local repository path:{}, Git server url: {}".format( conf.git.local_repository_path, conf.git.git_server_url)) self.repo = Gittle(conf.git.local_repository_path, origin_uri=conf.git.git_server_url) logger.info("Pulling from git..") # Update local working copy self.repo.pull() logger.info("GitGittle - Git is up to date !") except Exception as exc: logger.error( "GitGittle - Failed to initialize Git. Reason: {}".format( exc.message)) raise GitInitError(exc.message) def git_upload_changes(self): commit_id = "" try: logger.info("Commit changes in progress ..") # Stage modified files self.repo.stage(self.repo.pending_files) commit_message = conf.git.commit_message_format.format( self.repo.added_files) # Commit the change commit_id = self.repo.commit(conf.git.commit_user, conf.git.commit_email, commit_message) logger.info("Commit details: commit_user:{}, commit_email:{}, " "commit_message:{}, commit_id:{}".format( conf.git.commit_user, conf.git.commit_email, commit_message, commit_id)) # Push to repository self.repo.push() except Exception as exc: logger.error("GitGittle - Filed to upload file to git.") raise GitUploadError("Failed to upload file to Git") return commit_id def validate_git(self): pass
def git_checkout(args): if len(args) == 1: repo = Gittle('.') repo.clean_working() #repo.checkout('refs/heads/{0}'.format(args[0])) repo.switch_branch('{0}'.format(args[0])) else: print command_help['checkout']
def checkout(slist): fp = open(slist, 'r') lines = fp.readlines() for line in lines: line_list = line.split('\n')[0].split('\t') if len(line_list) == 4: repo_url = line_list[3] repo_path = line_list[1] try: repo = Gittle.clone(repo_url, repo_path) except: print('clone')
class GPassGit: #Creates a Git object def __init__(self, repoPath): self.repoPath = repoPath self.repo = None if self.isRepoSet(): self.repo = Gittle(self.repoPath) #Check For Git Repo def isRepoSet(self): if os.path.isfile(self.repoPath + '/.git/config'): return True return False def init(self, repoPath): self.repo = Gittle.init(repoPath) print("Created Repo") def add(self, filePath): self.repo.stage([filePath]) def commit(self, user, em, msg): self.repo.commit( name=user, email=em, message=msg ) # Authentication with RSA private key def auth(self, key): key_file = open(key) repo.auth(pkey=key_file) def push(self, key): self.auth(key) self.repo.push() def pull(self, key): self.auth(key) self.repo.pull() def acp(self, filepath, user, em, msg): self.add(filepath) self.commit(user, em, msg) self.push(key)
class MyGit: def __init__(self, gitdir): self.git = Gittle(gitdir) def add(self, files, message): self.git.commit(message=message, files=files) def rm(self, files, message): self.git.rm(files) self.git.commit(message=message)
def git_clone(args): if len(args) > 0: url = args[0] #def clone(source, target=None, bare=False, checkout=None, config=None, opener=None, outstream=sys.stdout): repo = Gittle.clone(args[0], args[1] if len(args)>1 else '.', bare=False) #porcelain.clone(url, target='.') #repo = Gittle('.') #Set the origin config = repo.repo.get_config() config.set(('remote','origin'),'url',url) config.write_to_path() else: print command_help['clone']
def clone(self,clonedict): remote=clonedict['remote url'] local=clonedict['local path'] repo_name= os.path.join(self.view['repo'].base, local) if remote: try: repo = Gittle.clone(remote, repo_name, bare=False) #Set the origin config = repo.repo.get_config() config.set(('remote','origin'),'url',remote) config.write_to_path() self.view['repo'].txt=repo_name self.refresh() except Exception as e: console.hud_alert(e.message)
def git_push(args): from gittle import GittleAuth if len(args) == 1 or len(args) == 3: if len(args) > 1: user = args[1] pw = args[2] repo = Gittle('.') print repo.push_to(args[0],username=user,password=pw) else: repo = Gittle('.', origin_uri=args[0]) repo.push() else: print command_help['push']
class Git(object): @classmethod def is_dir_git_repo(cls, directory): return os.system("git rev-parse --is-inside-work-tree") == 0 def __init__(self, gitpath): self._gitpath = gitpath try: self._gittle = Gittle(gitpath) except dulwich.errors.NotGitRepository: raise NoTesseraRepoError() @property def git_dir(self): """ Returns the git dir. """ return self._gittle.git_dir def is_working(self): """ Checks if git is working """ return self._gittle.is_working def commit_repo(self, tesserae, message): """ Commits the git tessera files. """ return self._gittle.commit(message=message, files=[os.path.relpath(tesserae.configpath, self._gitpath)]) def add_tessera(self, tessera): """ Commits a Tessera created by the create() method to the repository. """ return self._gittle.commit(message="tessera created: %s" % tessera.title, files=[os.path.relpath(tessera.tessera_file, self._gitpath), os.path.relpath(tessera.info_file, self._gitpath)]) def update_tessera(self, tessera): """ Commits an updated Tessera to the repository. """ return self._gittle.commit(message="tessera updated: %s" % tessera.title, files=[os.path.relpath(tessera.tessera_file, self._gitpath), os.path.relpath(tessera.info_file, self._gitpath)]) def rm_tessera(self, tessera): """ Removes a tessera and commits to git repository. """ files = [str(os.path.relpath(tessera.tessera_file, self._gitpath)), str(os.path.relpath(tessera.info_file, self._gitpath))] self._gittle.rm(files) return self._gittle.commit(message="tessera removed: %s" % tessera.title, files=files)
def push_directory_to_repo(directory, github_repo): auth = FixedGittleAuth(pkey=open(settings.GITHUB_PRIVATE_KEY)) repo = Gittle.init(directory, auth=auth) files = [] with work_in(directory): for root, dirnames, filenames in os.walk("."): # remove the leading './' root = root[2:] # skip .git directories if ".git" in dirnames: dirnames.remove(".git") for f in filenames: path = os.path.join(root, f) files.append(str(path)) repo.commit(name="bakehouse", message="Hello world", files=files) repo.push(github_repo.ssh_url, branch_name="master")
def handle(self, *args, **options): projects = Project.objects.all() for project in projects: print('Checking {0} for new commits'.format(project)) if project.git_url: repo_path = '/tmp/' + project.slug try: repo = Gittle(repo_path, project.git_url) repo.pull() except: try: repo = Gittle.clone(project.git_url, repo_path) except: # put some logging here repo = None if repo: new_commits = [] for commit in repo.commit_info(): try: prev_commit = repo.get_previous_commit(commit['sha']) time = (datetime.fromtimestamp(commit['time']) + timedelta(hours=commit['timezone']/(60*60))).replace(tzinfo=pytz.utc) try: user_author = get_user_model().objects.get(email=commit['author']['email']) string_author = None except: string_author = commit['author']['name'] user_author = None summary = commit['message'].split('\n')[0][:45] pcommit = ProjectCommit.objects.create( project=project, chash=commit['sha'], message=commit['message'], summary=summary, user_author=user_author, string_author=string_author, created=time, time=time, diff=repo.diff(commit['sha'], prev_commit).next()['diff'] ) print(pcommit, ' added.') new_commits.append(pcommit) except: pass
def whole_project(repo_url): real_repo_url = get_real_repo_url(repo_url) from gittle import Gittle try: repo_path = '/tmp/gittle_bare' repo = Gittle(repo_path) except: repo = Gittle.clone(repo_url, repo_path) with open('%s/docs/index.rst' % repo_path, 'r') as index_page: file_content = index_page.read() initial_content = rst2html(file_content) #except: # return render_template("project/home.html", repo_url=repo_url, repo=repo, initial_content=initial_content, page_title="index.rst" )