Exemple #1
0
def get_git(scheme,url,target,overwrite,tag):
    import git

    if os.path.exists(target + '/.git'):
        if not overwrite: return
    else:
        if len(scheme) == 1: giturl = url
        else: giturl = url[4:]
        git.clone(giturl,target)

    fs.goto(target)
    git.fetch()
    out = git.branch()
    for line in out.split('\n'):
        if not line: continue
        if line[0] != '*': continue
        out = line.split()[1]
        break
    #print out,tag
    if out != tag:
        lbranches,rbranches = git.branches()
        if tag in lbranches:
            git.checkout(tag)
        else:
            # git 1.5 does not put remotes/ like 1.6 does
            from exception import CommandFailure
            try:
                git.checkout('origin/'+tag,tag)
            except CommandFailure:
                git.checkout('remotes/origin/'+tag,tag)
    git.pull()
    fs.goback()
    return
Exemple #2
0
    async def on_message(self, message):
        # auto ban on 15+ pings
        if len(message.mentions) > 15:
            embed = discord.Embed(description=message.content)
            await message.delete()
            await message.author.ban()
            await message.channel.send(
                "{} was banned for attempting to spam user mentions.".format(
                    message.author))
            await self.bot.log_channel.send(
                "{} was banned for attempting to spam user mentions.".format(
                    message.author))

        if self.bot.user.mention in message.content:
            if message.author == self.bot.creator:
                await message.channel.send("Yes {}?".format(
                    self.bot.creator.mention))
            else:
                await message.channel.send("F**k off {}.".format(
                    message.author.mention))

        if isinstance(
                message.channel, discord.abc.GuildChannel
        ) and 'git' in message.channel.name and message.author.name == 'GitHub':
            print('Pulling changes')
            git.pull()
            print('Changes pulled!')
Exemple #3
0
def main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage)
    parser.add_option("-m", "--merge-master", dest="merge_master",
                    action="store_true",
                    default=False,
                    help="Merges the latest master into the current branch")
    parser.add_option("-B", "--merge-branch", dest="merge_branch",
                    action="store_true",
                    default=False,
                    help="Merge the current branch into master; forces -m")
    options, args = parser.parse_args()
    repo=Repo(os.getcwd())
    git = repo.git
    if not options.merge_master and not options.merge_branch:
        parser.error('Must choose one-- try -m or -B')

    # Merging branch requires latest merged master
    if options.merge_branch:
        options.merge_master = True

    if options.merge_master:
        output=repo.git.status()
	print output
        match = re.search('On branch ([^\s]*)', output)
	print match
        branch = None
        if match is None:
            raise Exception('Could not get status')
        elif match.group(1) == 'master':
            raise Exception('You must be in the branch that you want to merge, not master')
        else:
            branch = match.group(1)
            logging.info('In branch %s' % branch)

        if output.endswith('nothing to commit, working directory clean'):
            logging.info('Directory clean in branch: %s' % branch)
        else:
            raise Exception('Directory not clean, must commit:\n%s' % output)

        logging.info('Switching to master branch')
        git.checkout("master")
        git.pull()
        logging.info('Pulled latest changes from origin into master')
        logging.info('Ensuring master has the latest changes')
        output=git.pull()
        if 'up-to-date' not in output:
            raise Exception('Local copy was not up to date:\n%s' % output)
        else:
            logging.info('Local copy up to date')

        logging.info('Switching back to branch: %s' % branch)
        repo.git.checkout('remotes/origin/master')
Exemple #4
0
def cherry_pick_real(git, branches, frm, to):
    git.checkout("master")
    git.pull()
    for br in branches:
        print(f"switching to {br}...")
        git.checkout(br)
        print(f"pulling from remote repo...")
        git.pull()
        print(f"cherry picking {frm}..{to}...")
        git.cherry_pick(f"{frm}..{to}")
        print(f"pushing to remote repo...")
        git.push()
        print(f"switching to master...")
        git.checkout("master")
Exemple #5
0
def process_repository(repository_full_path):
    repo_result = {}

    if os.path.isdir(repository_full_path
                     ) and not ignore.is_ignore_path(repository_full_path):

        repository = os.path.basename(repository_full_path)

        # process repository
        cwd_backup = os.getcwd()
        os.chdir(repository_full_path)

        msgo, msge = git.reset_hard_head()
        if 'CONFLICT' in msgo or msge:
            repo_result['reset'] = get_error_info('git reset --hard HEAD',
                                                  msgo, msge, repository)

        msgo, msge = git.checkout('master')
        if 'CONFLICT' in msgo or "Already on 'master'" != msge.strip():
            repo_result['checkout'] = get_error_info('git checkout master',
                                                     msgo, msge, repository)

        msgo, msge = git.pull()
        if 'CONFLICT' in msgo or ('error' in msge) or ('fatal' in msge):
            repo_result['pull'] = get_error_info('git pull', msgo, msge,
                                                 repository)

        msgo, msge = git.status()
        if 'CONFLICT' in msgo or msge:
            repo_result['status'] = get_error_info('git status', msgo, msge,
                                                   repository)

        os.chdir(cwd_backup)
    return repo_result
Exemple #6
0
    def put(self):
        """action sent from js. Does all the work!"""

        # get current directory (to return later)
        #might not be needed
        cwd = os.getcwd()

        try:
            # make connection to git remote server
            git = git_cnxn()

            # obtain filename and msg for commit
            data = json.loads(self.request.body.decode('utf-8'))
            filename = urllib.parse.unquote(data['filename'])
            msg = data['msg']
            add_all = data['add_all']
            pull = data['pull']
            if pull:
                git.pull()
                self.write({
                    'status':
                    200,
                    'statusText':
                    ('Success!  '
                     'Pulled from {} everything up to date!'.format(git.url))
                })
            else:
                git.pull()
                git.add(filename, add_all)
                git.commit(msg)
                git.push()
                #git_pr()
                # close connection
                self.write({
                    'status':
                    200,
                    'statusText':
                    ('Success!  '
                     'Changes to {} captured on branch {} at {}'.format(
                         filename, git.branch_nm, git.url))
                })

            # return to directory
            os.chdir(cwd)
        except ErrorPrintToJupyter as e:
            self.error_and_return(cwd, str(e).replace('\n', '</br> '))
Exemple #7
0
 def refresh(self):
     """
       Refresh (git pull) repository
     """
     git = self.__getGitCmd()
     # git.fetch()
     git.reset('--hard', 'HEAD')
     counter = 5
     try:
         git.pull()
         return
     except:
         if counter == 0:
             self.__getLog().warning("Problem with pulling of "
                                     "the repository '%s'" % self.name)
             return
         counter -= 1
Exemple #8
0
 def refresh(self):
     """
       Refresh (git pull) repository
     """
     git = self.__getGitCmd()
     # git.fetch()
     git.reset('--hard', 'HEAD')
     counter = 5
     try:
         git.pull()
         return
     except:
         if counter == 0:
             self.__getLog().warning("Problem with pulling of "
                                     "the repository '%s'" % self.name)
             return
         counter -= 1
Exemple #9
0
def build_repo(repository, ref, docker_repo, docker_tag, namespace, push,
               registry, repos_folder, logger):
    ''' Builds one line of a library file.
        repository:     URL of the git repository that needs to be built
        ref:            Git reference (or commit ID) that needs to be built
        docker_repo:    Name of the docker repository where the image will
                        end up.
        docker_tag:     Tag for the image in the docker repository.
        namespace:      Namespace for the docker repository.
        push:           If the image should be pushed at the end of the build
        registry:       URL to private registry where image should be pushed
        repos_folder:   Directory where repositories should be cloned
        logger:         Logger instance
    '''
    dst_folder = None
    img_id = None
    commit_id = None
    if repos_folder:
        # Repositories are stored in a fixed location and can be reused
        dst_folder = os.path.join(repos_folder, docker_repo + _random_suffix())
    docker_repo = '{0}/{1}'.format(namespace or 'library', docker_repo)

    if '{0}@{1}'.format(repository, ref) not in processed.keys():
        # Not already built
        rep = None
        logger.info('Cloning {0} (ref: {1})'.format(repository, ref))
        if repository not in processed:  # Repository not cloned yet
            rep, dst_folder = git.clone(repository, ref, dst_folder)
            processed[repository] = rep
            processed_folders.append(dst_folder)
        else:
            rep = processed[repository]
            if ref in rep.refs:
                # The ref already exists, we just need to checkout
                dst_folder = git.checkout(rep, ref)
            else:  # ref is not present, try pulling it from the remote origin
                rep, dst_folder = git.pull(repository, rep, ref)
        if not 'Dockerfile' in os.listdir(dst_folder):
            raise RuntimeError('Dockerfile not found in cloned repository')
        commit_id = rep.head()
        logger.info('Building using dockerfile...')
        img_id, logs = client.build(path=dst_folder, quiet=True)
    else:
        logger.info('This ref has already been built, reusing image ID')
        img_id = processed['{0}@{1}'.format(repository, ref)]
        if ref.startswith('refs/'):
            commit_id = processed[repository].ref(ref)
        else:
            commit_id = ref
    logger.info('Committing to {0}:{1}'.format(docker_repo,
                docker_tag or 'latest'))
    client.tag(img_id, docker_repo, docker_tag)
    if push:
        logger.info('Pushing result to registry {0}'.format(
            registry or "default"))
        push_repo(img_id, docker_repo, registry=registry, logger=logger)
    return img_id, commit_id
Exemple #10
0
def build_repo(repository, ref, docker_repo, docker_tag, namespace, push,
               registry, repos_folder, logger):
    ''' Builds one line of a library file.
        repository:     URL of the git repository that needs to be built
        ref:            Git reference (or commit ID) that needs to be built
        docker_repo:    Name of the docker repository where the image will
                        end up.
        docker_tag:     Tag for the image in the docker repository.
        namespace:      Namespace for the docker repository.
        push:           If the image should be pushed at the end of the build
        registry:       URL to private registry where image should be pushed
        repos_folder:   Directory where repositories should be cloned
        logger:         Logger instance
    '''
    dst_folder = None
    img_id = None
    commit_id = None
    if repos_folder:
        # Repositories are stored in a fixed location and can be reused
        dst_folder = os.path.join(repos_folder, docker_repo + _random_suffix())
    docker_repo = '{0}/{1}'.format(namespace or 'library', docker_repo)

    if '{0}@{1}'.format(repository, ref) not in processed.keys():
        # Not already built
        rep = None
        logger.info('Cloning {0} (ref: {1})'.format(repository, ref))
        if repository not in processed:  # Repository not cloned yet
            rep, dst_folder = git.clone(repository, ref, dst_folder)
            processed[repository] = rep
            processed_folders.append(dst_folder)
        else:
            rep = processed[repository]
            if ref in rep.refs:
                # The ref already exists, we just need to checkout
                dst_folder = git.checkout(rep, ref)
            else:  # ref is not present, try pulling it from the remote origin
                rep, dst_folder = git.pull(repository, rep, ref)
        if not 'Dockerfile' in os.listdir(dst_folder):
            raise RuntimeError('Dockerfile not found in cloned repository')
        commit_id = rep.head()
        logger.info('Building using dockerfile...')
        img_id, logs = client.build(path=dst_folder, quiet=True)
    else:
        logger.info('This ref has already been built, reusing image ID')
        img_id = processed['{0}@{1}'.format(repository, ref)]
        if ref.startswith('refs/'):
            commit_id = processed[repository].ref(ref)
        else:
            commit_id = ref
    logger.info('Committing to {0}:{1}'.format(docker_repo, docker_tag
                                               or 'latest'))
    client.tag(img_id, docker_repo, docker_tag)
    if push:
        logger.info('Pushing result to registry {0}'.format(registry
                                                            or "default"))
        push_repo(img_id, docker_repo, registry=registry, logger=logger)
    return img_id, commit_id
Exemple #11
0
def git_sync():
  
  branch_name = git.branch_name()

  if git.is_dirty():
    print_color('fatal: Can\'t publish a dirty directory.', colors.FAIL)
    quit()

  if git.has_untracked():
    print_color('fatal: There are untracked files.', colors.FAIL)
    quit()

  git.pull(branch_name)

  if git.is_dirty():
    print_color('Fix merge conflicts.', colors.FAIL)
    quit()

  git.push(branch_name)
Exemple #12
0
    def _clone_or_checkout(self, url, ref, dst_folder, rep):
        if rep:
            try:
                # The ref already exists, we just need to checkout
                dst_folder = git.checkout(rep, ref)
            except git.GitException:
                # ref is not present, try pulling it from the remote origin
                rep, dst_folder = git.pull(url, rep, ref)
            return rep, dst_folder

        if dst_folder:
            rmtree(dst_folder)
        return git.clone(url, ref, dst_folder)
Exemple #13
0
    def _clone_or_checkout(self, url, ref, dst_folder, rep):
        if rep:
            try:
                # The ref already exists, we just need to checkout
                dst_folder = git.checkout(rep, ref)
            except git.GitException:
                # ref is not present, try pulling it from the remote origin
                rep, dst_folder = git.pull(url, rep, ref)
            return rep, dst_folder

        if dst_folder:
            rmtree(dst_folder)
        return git.clone(url, ref, dst_folder)
Exemple #14
0
    async def on_message(self, message):
        # auto update
        if message.author.name == "GitHub" and "git" in message.channel.name:
            print("Pulling changes!")
            git.pull()
            print("Changes pulled!")

        # receive private messages
        if isinstance(message.channel, discord.abc.PrivateChannel
                      ) and message.author.id != self.bot.user.id:
            embed = discord.Embed(description=message.content)
            if message.attachments:
                attachment_urls = []
                for attachment in message.attachments:
                    attachment_urls.append('[{}]({})'.format(
                        attachment.filename, attachment.url))
                attachment_msg = '\N{BULLET} ' + '\n\N{BULLET} s '.join(
                    attachment_urls)
                embed.add_field(name='Attachments',
                                value=attachment_msg,
                                inline=False)
            await self.bot.private_messages_channel.send(
                "Private message sent by {0.mention} | {0}:".format(
                    message.author),
                embed=embed)

        # auto ban on 15+ pings
        if len(message.mentions) > 15:
            embed = discord.Embed(description=message.content)
            await message.delete()
            await message.author.kick()
            await message.channel.send(
                "{} was kicked for trying to spam ping users.".format(
                    message.author))
            await self.bot.logs_channnel.send(
                "{} was kicked for trying to spam ping users.".format(
                    message.author))
            await self.bot.logs_channel.send(embed=embed)
Exemple #15
0
  def write_txn(branch):

    if sync:
      git.pull(branch)

    hostery_file = open(HOSTERY_FILE, 'r')
    all_data = json.loads(hostery_file.read())
    all_data.update(data)
    hostery_file.close()

    hostery_file = open(HOSTERY_FILE, 'w')
    hostery_file.write(json.dumps(all_data, indent=2))
    hostery_file.close()

    git.add(HOSTERY_FILE)

    # command('git status', verbose=True)

    git.commit('hostery mark')

    if sync:
      git.push(branch)

    return all_data
Exemple #16
0
	def pull(self, what):
		if type(what) is list:
			for w in what:
				p=w.split(':')
				git.pull(p[0], p[1], self.stdout)
		elif ':' in what:
			p=what.split(':')
			git.pull(p[0], p[1], self.stdout)
		elif what in self.config['git']['pull']:
			for key, val in self.config['git']['pull'][what].iteritems():
				git.pull(key, val, self.stdout)
		else:
			print 'we do not know what you are trying to do here...'
Exemple #17
0
def updateBranches(repo, branches):
    git = repo.git
    result = ""
    for branch in branches:
        try:
            result = git.pull('origin', branch, "--ff-only")
            print("\t" + bcolors.HEADER,
                  str(branch).strip(), bcolors.ENDC, "pulled: " + result)
        except Exception as e:
            message = e.stderr
            if ("Couldn't find remote ref" in message):
                # Local branch only, this branch DNE in remote repository
                print("\t" + bcolors.HEADER,
                      str(branch).strip() + ":", bcolors.ENDC, bcolors.FAIL,
                      "Couldn't find remote ref.", bcolors.ENDC)
            else:
                # Some other kind of issue. Use more generic message for debugging.
                print("\t",
                      bcolors.FAIL, "There was a problem fast-forwarding ",
                      str(branch), ": ", e, bcolors.ENDC)
Exemple #18
0
def pull_path(repository_path, b_verbose=False):
    """
    cd to repository_path
    git pull
    return to original path
    """

    org_path = repo_path.cd(repository_path)

    clean_repo_before_update(b_verbose=b_verbose, caller_name='pull_path')

    git.checkout('master', b_verbose=False)
    stdout, stderr = git.pull(b_verbose=False)

    # if there was an error during pull
    clean_repo_after_error(
        stdout,
        stderr,
        'pull_path',
        b_verbose=False,
    )

    os.chdir(org_path)
Exemple #19
0
#!/usr/bin/python3
import git
import os
from config import repo_path, after_pull_instructions

repo = git.Repo(repo_path if repo_path != '' else os.getcwd())


def get_latest_remote_commit(repo):
    return repo.git.ls_remote("--heads", "origin", 'gh-pages').split()[0]


def get_latest_local_commit(repo):
    return repo.commit('gh-pages')


if get_latest_local_commit(repo) != get_latest_remote_commit(repo):
    git = repo.git
    repo.heads.default.checkout()
    git.pull('origin', 'default')
    git.branch('-d', 'gh-pages')
    git.checkout('-b', 'gh-pages')
    git.pull('origin', 'gh-pages')
    if after_pull_instructions:
        fd = open(after_pull_instructions, 'r')
        instructions = fd.readlines()
        for ins in instructions:
            os.system(ins)
Exemple #20
0
 async def on_message(self, message):
     #auto update
     if message.author.name == "GitHub" and message.author.discriminator == "0000":
         print("Pulling changes!")
         git.pull()
         print("Changes pulled!")
def update_repo_to_commit(hook_data):
    commit_hash = hook_data['after']
    git.pull(commit_hash)
    branch = get_branch_from_push(hook_data)
    git.checkout(branch)
    return
    text = readme.read()
    for sub in subs:
        (find, replace) = sub
        text = re.sub(find, replace, text)
    readme.seek(0)
    readme.write(text)

print(INFO + "Adding git changes." + ENDC)
git.add(readme_file)
if not git.status("-s"):
    print(INFO + "No changes. Exiting." + ENDC)
    sys.exit(0)
git.commit('-m',
           f'Update {readme_file} examples to reflect new version {version}')

push_failures = 0
while True:
    try:
        print(INFO + "Pushing updated README." + ENDC)
        git.push()
        break
    except GitCommandError:
        push_failures += 1
        if push_failures <= 5:
            print(NOTICE + "Failed to push. Going to pull and try again." +
                  ENDC)
            git.pull(rebase=True)
        else:
            print(ERROR + "Failed to push again. Giving up." + ENDC)
            raise
Exemple #23
0
def build_if_need():
    git_info = config.config_dic['git']

    project_path = config.config_dic['project_path']

    branch = 'master'
    current_branch = git.get_current_branch(project_path)
    if current_branch:
        branch = current_branch

    if git_info['pull_before_build']:
        print('Pulling latest code...')

        if git_info['branch']:
            if git_info['branch'] == branch:
                git.pull(project_path, branch=branch)
            else:
                git.checkout_and_pull(project_path, branch=git_info['branch'])
        else:
            git.pull(project_path, branch=branch)

        print('Pull code complete!')

    current_commit = git.get_current_commit(project_path)

    last_try_file = config.config_dic['log_path'] + 'last_try_build.txt'
    last_build_file = config.config_dic['log_path'] + 'last_build.txt'

    last_try_commit = '0'
    last_build_commit = '0'

    if os.path.exists(last_try_file):
        with open(last_try_file, 'r') as f:
            last_try_commit = f.read()

    if os.path.exists(last_build_file):
        with open(last_build_file, 'r') as f:
            last_build_commit = f.read()

    if last_try_commit == current_commit:
        print('Build have tried, exit!')
        return (False, None)

    commit_msg = git.get_latest_commit_msg(project_path)

    build_target = None
    for key in config.config_dic['build']:
        if config.config_dic['build'][key]['build_identifier'] in commit_msg:
            build_target = key
            break

    if not build_target:
        print('No build identifier, exit!')
        return (False, None)

    if last_build_commit == current_commit:
        print('This build has been builded, exit!')
        return (False, None)

    print('Build identifier detect, build start...')
    print('Build info {}'.format(config.config_dic['build'][build_target]))
    return (True, build_target)
Exemple #24
0
from pprint import pprint

import prepare
import git

# Option parser
parser = optparse.OptionParser(description='Markdown files to html from git. Run and get on with your life.')
parser.add_option('-s', dest='source', default='./source', help='Documentation source', metavar='SOURCE')
parser.add_option('-t', dest='target', default='./build', help='Documentation output target', metavar='TARGET')
parser.add_option('-r', dest='resources', default='./resource', help='Template and resources', metavar='RESOURCES')
parser.add_option('-f', dest='force', action="store_true", default=False, help='Force rebuild', metavar='FORCE')

(options, args) = parser.parse_args()

# check git & update only if needed
need_refresh = git.pull(options.source)
if options.force or need_refresh:
    # reset state
    prepare.empty_target(options.target)
    prepare.init_template(options.resources)
    # publish all
    items = prepare.get_items(options.source)
    for item in items:
        targetdir = options.target + item[0][len(options.source):]
        prepare.ensure_dir(targetdir)
        prepare.push_resources(options.resources, targetdir)
        for name in item[2]:
            prepare.push_file(item[0], targetdir, name)


Exemple #25
0
import subprocess
import os
import sys
import time
import shutil
import codecs
import cgi
import webbrowser
import argparse
import git

# g = git.cmd.Git('F:\git\py\git_insights\mcu_project_generator')
# print g.pull()

import git

repo_dir = os.path.abspath('./repo/')
test_repo_url = '[email protected]:jsplyy/test.git'
test_repo_dir = '/'.join(
    [repo_dir, test_repo_url.split('/')[-1].split('.')[0]])
print test_repo_dir

if not os.path.exists(test_repo_dir):
    global repo
    repo = git.Repo.clone_from(test_repo_url, test_repo_dir, recursive=True)
else:
    repo = git.Repo(test_repo_dir)
git = repo.git
print git.pull()
print git.branch()