Ejemplo n.º 1
0
def create_working_md(sender, instance, created, **kwargs):
    path = instance.get_path()
    if created:
        print 'create sroty'
        instance.authors.add(instance.owner)
        instance.save()

    f = codecs.open(path, encoding='utf-8', mode='w+')
    f.write(instance.contents)
    f.seek(0)
    f.close()

    from git import Repo, Commit, Actor, Tree

    author = Actor(instance.owner.username, instance.owner.email)
    committer = Actor(settings.GIT_COMMITTER['name'],
                      settings.GIT_COMMITTER['email'])

    # commit if there are any differences
    repo = Repo.init(settings.GIT_ROOT)

    # add and commit JUST THIS FILE
    #tree = Tree(repo=repo, path=instance.owner.profile.get_path())
    repo.index.add([instance.owner.profile.get_path()])
    repo.index.commit(message=u"saving %s" % instance.title,
                      author=author,
                      committer=committer)
Ejemplo n.º 2
0
    def test_checkout_master(self):
        if not select_tune_date_update(cnn):
            date_updated = create_tune_date_update(cnn)

            git_dir = os.path.join(config.git_folder, cnn.dsn)

            branch_name = '%s_%s' % (
                db_name, date_updated.strftime('%d.%m.%Y_%H.%M')
            )  # %Y%m%d_%H%M%S
            repo = clone_or_open_repo(git_dir)

            past_branch = repo.create_head(branch_name, 'master')
            repo.head.reference = past_branch
            repo.head.reset(index=True, working_tree=True)
            logger.info("checkout new branch %s" % branch_name)

            # Сохраним тексты
            df = select_objects_in_folder_or_date_modified(
                cnn, git_dir, 1, 'd')
            dirs.write_object_from_df(df, git_dir)

            # Закомитим
            # repo.git.add(update=True)
            repo.git.add(A=True)
            index = repo.index

            from git import Actor
            author = Actor("An author", "*****@*****.**")
            committer = Actor("A committer", "*****@*****.**")

            index.commit("my commit message",
                         author=author,
                         committer=committer)
Ejemplo n.º 3
0
def git_magic():
    """
    This creates repository on the downloaded and extracted folder.
    Once that's accomplished, it will push the changes to the repository.
    Preserving the history of the branch.
    """
    print("Before Git, PATH is: %s." % sys.path)
    for param in os.environ.keys():
        print("%20s %s" % (param, os.environ[param]))
    import git
    print("Performing git magic.")
    source = git.Repo.init(path='/tmp/public')
    print("Repo initated.")
    origin = source.create_remote('origin', REPO)
    origin.fetch()
    source.head.reset(commit='origin/master')
    source.heads.master.set_tracking_branch(origin.refs.master)
    source.heads.master.checkout()
    source.git.add(A=True)
    from git import Actor
    author = Actor(NAME, EMAIL)
    committer = Actor(NAME, EMAIL)
    source.index.commit(message='Added new content through AWS Lambda.',
                        author=author,
                        committer=committer)
    source.git.push()
    print("Push done.")
Ejemplo n.º 4
0
    def test_commit_authorized_time(self):
        # repos_dir = r"C:\Users\BryzzhinIS\Documents\Хранилища\sync_script"
        # repo_dir = os.path.join(repos_dir, 'dbs', 'lw-abs-abs')
        # repo = clone_or_open_repo(repo_dir)
        # repo.git.add(A=True)
        # author = Actor('sources_sync_job', '@mail')
        # committer = Actor('sources_sync_job', '@mail')
        # time_str = "2012-07-24T23:14:29-07:00"
        # time_str = "2012-07-24T23:14:29"
        # repo.index.commit('test', author=author, committer=committer, author_date=time_str)

        last_date_update = '28.09.2017 18:30:30'
        df = select_all_by_date_modified(cnn, last_date_update)
        df = df.sort_values('MODIFIED', ascending=False)
        # localtz = pytz.timezone('Europe/Moscow')
        # time_str = localtz.localize(df.iloc[0]["MODIFIED"]).strftime('%d.%m.%YT%H:%M:%S%z')
        time_str = (df.iloc[0]["MODIFIED"] +
                    datetime.timedelta(hours=-3)).strftime('%d.%m.%YT%H:%M:%S')
        print(time_str)
        repos_dir = r"C:\Users\BryzzhinIS\Documents\Хранилища\sync_script"
        # repo_dir = os.path.join(repos_dir, 'dbs', 'lw-abs-abs')
        repo_dir = os.path.join(repos_dir, 'dbs', 'mideveryday')
        repo = clone_or_open_repo(repo_dir)
        repo.git.add(A=True)
        author = Actor('sources_sync_job', '@mail')
        committer = Actor('sources_sync_job', '@mail')
        # time_str = "2012-07-24T23:14:29-07:00"
        # time_str = "2012-07-24T23:14:29"
        repo.index.commit('test',
                          author=author,
                          committer=committer,
                          author_date=time_str)
Ejemplo n.º 5
0
def ampq_addFile(id=None, file=None, message=None, branch="master"):

    repository = get_object_or_404(Repository, pk=id)
    temp = tempfile.mkdtemp()

    repo = Repo.clone_from(repository.url, temp,
                           branch=branch)  # clone du dépot

    new_file_path = os.path.join(repo.working_tree_dir,
                                 os.path.basename(file.file.name))
    fichier = open(new_file_path, 'wb')
    fichier.write(file.file.read())
    fichier.close()
    repo.index.add([new_file_path])

    print(temp + '/' + file.file.name + '/')
    print(os.path.basename(file.file.name))
    print(temp + '/' + os.path.basename(file.file.name))

    author = Actor("Utilisateur", "*****@*****.**")
    committer = Actor("admin la maison des partitions",
                      "*****@*****.**")

    repo.index.commit(message, author=author, committer=committer)

    repo.remotes.origin.push()

    file.file.delete()
    file.delete()

    return 1
Ejemplo n.º 6
0
def get_commit_author():
    """get a dictionary that represent the commit author with ``author`` and ``comitter`` key. If there is no users,
    bot account pk is used.

    :return: correctly formatted commit author for ``repo.index.commit()``
    :rtype: dict
    """
    user = get_current_user()

    if user and user.is_authenticated():
        aut_user = str(user.pk)
        aut_email = None

        if hasattr(user, 'email'):
            aut_email = user.email

    else:
        try:
            aut_user = str(
                User.objects.filter(username=settings.ZDS_APP['member']
                                    ['bot_account']).first().pk)
        except AttributeError:
            aut_user = '******'

        aut_email = None

    if aut_email is None or aut_email.strip() == "":
        aut_email = _(u"inconnu@{}").format(settings.ZDS_APP['site']['dns'])

    return {
        'author': Actor(aut_user, aut_email),
        'committer': Actor(aut_user, aut_email)
    }
Ejemplo n.º 7
0
def test_build_crash(tmpdir):
    """
    Simulate a runtime error in the build.
    """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    # Write a test file to the repo
    with open(tmppath / "test.py", "w") as test_txt:
        test_txt.write("import abc")

    index = repo.index
    index.add(["test.py"])

    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    index.commit("basic test", author=author, committer=committer)
    repo.close()

    import wily.commands.build

    with patch.object(wily.commands.build.Bar,
                      "finish",
                      side_effect=RuntimeError("arggh")) as bar_finish:
        runner = CliRunner()
        result = runner.invoke(main.cli,
                               ["--path", tmpdir, "build", "test.py"])
        assert bar_finish.called_once
        assert result.exit_code == 1, result.stdout
Ejemplo n.º 8
0
    def delete_data(self, repo_path, message, author=None, committer=None):
        """
        Delete a file that's not necessarily a model file.

        :param str repo_path:
            Which file to delete.
        :param str message:
            The commit message.
        :param tuple author:
            The author information (name, email address)
            Defaults repo default if unspecified.
        :param tuple committer:
            The committer information (name, email address).
            Defaults to the author if unspecified.
        :returns:
            The commit
        """
        if not isinstance(message, str):
            raise StorageException('Messages need to be bytestrings.')

        file_path = os.path.join(self.repo.working_dir, repo_path)
        if not os.path.isfile(file_path):
            raise StorageException('File does not exist.')

        author_actor = Actor(*author) if author else None
        committer_actor = Actor(*committer) if committer else author_actor

        # Remove from the index
        index = self.repo.index
        index.remove([file_path], working_tree=True)
        return index.commit(message,
                            author=author_actor,
                            committer=committer_actor)
Ejemplo n.º 9
0
    def _prepare(cls, create, **kwargs):
        extract = super(ExtractFactory, cls)._prepare(create, **kwargs)
        chapter = kwargs.pop('chapter', None)
        with open(extract.get_path(relative=False), "w") as f:
            f.write("This dumb content is just here to prove you zep12 is far better than old module")
        if chapter:

            if chapter.tutorial:
                repo = Repo(os.path.join(settings.ZDS_APP['tutorial']['repo_path'],
                                         chapter.tutorial.get_phy_slug()))
                index = repo.index
                index.add([extract.get_path(relative=True)])
                man_path = os.path.join(chapter.tutorial.get_path(), "manifest.json")
                chapter.tutorial.dump_json(path=man_path)
                index.add(["manifest.json"])
                chapter.tutorial.sha_draft = index.commit(
                    "bla",
                    author=Actor("bla", "*****@*****.**"),
                    committer=Actor("bla", "*****@*****.**"))
                chapter.tutorial.save()
            elif chapter.part:
                repo = Repo(os.path.join(settings.ZDS_APP['tutorial']['repo_path'],
                                         chapter.part.tutorial.get_phy_slug()))
                index = repo.index
                index.add([extract.get_path(relative=True)])
                man_path = os.path.join(chapter.part.tutorial.get_path(), "manifest.json")
                chapter.part.tutorial.dump_json(path=man_path)
                index.add(["manifest.json"])
                chapter.part.tutorial.sha_draft = index.commit(
                    "bla",
                    author=Actor("bla", "*****@*****.**"),
                    committer=Actor("bla", "*****@*****.**"))
                chapter.part.tutorial.save()
        return extract
Ejemplo n.º 10
0
def test_build(tmpdir):
    """
    Test that build works in a basic repository.
    """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    # Write a test file to the repo
    with open(tmppath / "test.py", "w") as test_txt:
        test_txt.write("import abc")

    with open(tmppath / ".gitignore", "w") as test_txt:
        test_txt.write(".wily/")

    index = repo.index
    index.add(["test.py", ".gitignore"])

    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    commit = index.commit("basic test", author=author, committer=committer)

    with patch("wily.logger") as logger:
        runner = CliRunner()
        result = runner.invoke(
            main.cli, ["--debug", "--path", tmpdir, "build", "test.py"])
        assert result.exit_code == 0, result.stdout

    cache_path = tmpdir / ".wily"
    assert cache_path.exists()
    index_path = tmpdir / ".wily" / "git" / "index.json"
    assert index_path.exists()
    rev_path = tmpdir / ".wily" / "git" / commit.name_rev.split(
        " ")[0] + ".json"
    assert rev_path.exists()
Ejemplo n.º 11
0
def test_dirty_git(tmpdir):
    """ Check that repository fails to initialise if unchecked files are in the repo """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    index = repo.index
    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    # First commit
    with open(tmppath / ".gitignore", "w") as ignore:
        ignore.write(".wily/")

    index.add([".gitignore"])
    commit1 = index.commit("commit1", author=author, committer=committer)

    # Write a test file to the repo
    with open(tmppath / "blah.py", "w") as ignore:
        ignore.write("*.py[co]\n")
    index.add(["blah.py"])
    repo.close()

    config = DEFAULT_CONFIG
    config.path = tmpdir

    with pytest.raises(DirtyGitRepositoryError):
        archiver = GitArchiver(config)
        archiver.revisions(tmpdir, 2)
Ejemplo n.º 12
0
def get_commit_author():
    """get a dictionary that represent the commit author with ``author`` and ``comitter`` key. If there is no users,
    bot account pk is used.

    :return: correctly formatted commit author for ``repo.index.commit()``
    :rtype: dict
    """
    user = get_current_user()

    if user and user.is_authenticated:
        aut_user = str(user.pk)
        aut_email = None

        if hasattr(user, "email"):
            aut_email = user.email

    else:
        try:
            aut_user = str(
                User.objects.filter(username=settings.ZDS_APP["member"]
                                    ["bot_account"]).first().pk)
        except AttributeError:  # if nothing is found, `first` returns None, which does not have attribute pk
            aut_user = "******"

        aut_email = None

    if aut_email is None or not aut_email.strip():
        aut_email = _("inconnu@{}").format(settings.ZDS_APP["site"]["dns"])

    return {
        "author": Actor(aut_user, aut_email),
        "committer": Actor(aut_user, aut_email)
    }
Ejemplo n.º 13
0
def test_detached_head(tmpdir):
    """ Check that repo can initialize in detached head state"""
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    index = repo.index
    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    # First commit
    with open(tmppath / "test.py", "w") as ignore:
        ignore.write("print('hello world')")

    index.add(["test.py"])
    commit1 = index.commit("commit1", author=author, committer=committer)

    # Second commit
    with open(tmppath / "test.py", "w") as ignore:
        ignore.write("print('hello world')\nprint(1)")

    index.add(["test.py"])
    commit2 = index.commit("commit2", author=author, committer=committer)

    repo.git.checkout(commit2.hexsha)
    repo.close()

    config = DEFAULT_CONFIG
    config.path = tmpdir

    archiver = GitArchiver(config)
    assert archiver.revisions(tmpdir, 1) is not None
Ejemplo n.º 14
0
def ampq_deleteFile(id=None, file=None, message=None, branch="master"):

    repository = get_object_or_404(Repository, pk=id)
    temp = tempfile.mkdtemp()

    # clone du dépot
    repo = Repo.clone_from(repository.url, temp,
                           branch=branch)  # clone du dépot

    #repo.git.checkout(branch)

    new_file_path = os.path.join(repo.working_tree_dir,
                                 os.path.basename(file.name))
    repo.index.remove([new_file_path])

    print(temp + '/' + file.name + '/')
    print(os.path.basename(file.name))
    print(temp + '/' + os.path.basename(file.name))

    author = Actor("Utilisateur", "*****@*****.**")
    committer = Actor("admin la maison des partitions",
                      "*****@*****.**")

    repo.index.commit(str(message), author=author, committer=committer)

    repo.remotes.origin.push()

    return 1
Ejemplo n.º 15
0
def patch_and_push(dfp, repository, target_branch, release_version):
    """patch the Dockerfile and push the created branch to origin"""

    # lets create a new branch
    new_branch = repository.create_head(target_branch)
    new_branch.checkout()

    # set dockerfile version to release version
    dfp.envs['MATTERMOST_VERSION'] = release_version
    dfp.envs['MATTERMOST_VERSION_SHORT'] = release_version.replace('.', '')

    # and write back the Dockerfile
    with open('./mattermost-openshift-workdir/Dockerfile', 'w') as f:
        f.write(dfp.content)
        f.close()

    # commit our work
    repository.index.add(['Dockerfile'])
    author = Actor('Mattermost Server Updater Bot', '*****@*****.**')
    committer = Actor('Mattermost Server Updater Bot', '*****@*****.**')
    repository.index.commit("Update to Mattermost " + release_version,
                            author=author,
                            committer=committer)

    # and push to origin
    with repository.git.custom_environment(GIT_SSH_COMMAND=SSH_CMD):
        repository.remotes.origin.push(
            refspec='{}:{}'.format(target_branch, target_branch))
Ejemplo n.º 16
0
def init_repo(message, bot, path_to_user_folder):

    try:
        with open(path_to_user_folder + '/user_data/token.txt', 'r') as f:
            token = f.read()
        with Git().custom_environment(HTTPS_REMOTE_URL='https://' + token +
                                      ':x-oauth-basic@' + message.text[8:]):
            repo = Repo.init(path_to_user_folder + '/main')
            repo.index.add('*')
            author = Actor("seppass", "*****@*****.**")
            committer = Actor("seppass", "*****@*****.**")
            repo.index.commit('initial commit',
                              author=author,
                              committer=committer)
            origin = repo.create_remote("origin",
                                        url='https://' + token +
                                        ':x-oauth-basic@' + message.text[8:])
            repo.git.push("--set-upstream", origin, repo.head.ref)
            msg = bot.send_message(
                message.chat.id,
                'Я закончил.',
                reply_markup=types.ReplyKeyboardRemove(selective=False))
            del_mess(msg, bot, 6)
            return

    except:
        msg = bot.send_message(
            message.chat.id,
            'Произошла ошибка.',
            reply_markup=types.ReplyKeyboardRemove(selective=False))
        del_mess(msg, bot, 4)
        return
Ejemplo n.º 17
0
async def add_to_index(author_name: Optional[str] = Body(...),
                       author_email: Optional[str] = Body(...),
                       msg: Optional[str] = None):
    if gitrepo.is_dirty(untracked_files=True):
        # add the changed files to the index for this commit
        changedFiles = [item.a_path for item in gitrepo.index.diff(None)]

        # create a status message
        num_changed_files = len(changedFiles)
        num_new_files = len(gitrepo.untracked_files)
        if msg is None:
            msg = f"adds {num_changed_files} changed and {num_new_files} new files to repo"

        # add changed and new files to index for commit
        gitrepo.index.add(gitrepo.untracked_files)
        gitrepo.index.add(changedFiles)

        # set author and committer
        committer = Actor("file-repo", "*****@*****.**")
        if author_email is None:
            author_email = "*****@*****.**"
        if author_name is None:
            author_name = "file-repo"

        author = Actor(f"{author_name}", f"{author_email}")

        gitrepo.index.commit(msg, author=author, committer=committer)

        return {
            "num_changed_files": num_changed_files,
            "num_new_files": num_new_files,
            "changed": (num_new_files > 0 or num_changed_files > 0)
        }
Ejemplo n.º 18
0
async def change_repo(url):
    # Remove Default Dockerfile then download New Dockerfile
    os.remove("Dockerfile")
    urllib.request.urlretrieve(url, "Dockerfile")

    # Commit Changes
    repo = Repo()
    index = repo.index
    index.add(["Dockerfile"])  # add a new file to the index
    from git import Actor

    author = Actor("Nana", "*****@*****.**")
    committer = Actor("Nana", "*****@*****.**")
    # commit by commit message and author and committer
    index.commit("Change Repo", author=author, committer=committer)
    if HEROKU_API is not None:
        import heroku3

        heroku = heroku3.from_key(HEROKU_API)
        heroku_applications = heroku.apps()
        if len(heroku_applications) >= 1:
            heroku_app = heroku_applications[0]
            heroku_git_url = heroku_app.git_url.replace(
                "https://", "https://*****:*****@"
            )
            if "heroku" in repo.remotes:
                remote = repo.remote("heroku")
                remote.set_url(heroku_git_url)
            else:
                remote = repo.create_remote("heroku", heroku_git_url)
            remote.push(refspec="HEAD:refs/heads/master", force=True)
Ejemplo n.º 19
0
def database_backup():
    repo = git.Repo(BACKUP_PATH)
    origin = repo.remote('origin')
    origin.pull()
    try:
        now = get_time_format()
        json_name = 'data_{}.json'.format(now)
        try:
            cmd = 'python manage.py dumpdata > %s ' % json_name
            os.system(cmd)
        except:
            cmd = 'python3 manage.py dumpdata > %s ' % json_name
            os.system(cmd)

        if not os.path.isfile(json_name):
            return 1

        shutil.move(json_name, BACKUP_PATH)

        author = Actor("taomercy", "*****@*****.**")
        committer = Actor("taomercy", "*****@*****.**")

        repo.git.add(os.path.join(BACKUP_PATH, json_name))
        if repo.index.diff('HEAD'):
            commit_msg = 'data backup %s' % now
            repo.index.commit(commit_msg, author=author, committer=committer)
            origin.push()
        else:
            logging.info("no changes")
        logging.info('backup end')
        return 0
    except Exception as err:
        print(repo.git.status())
        logging.info(err)
        return 1
Ejemplo n.º 20
0
 def __init__(self,
              storage: GitStorage,
              message: str,
              author: Tuple[str, str] = None,
              committer: Tuple[str, str] = None):
     super().__init__(storage, message)
     self.author = None if author is None else Actor(*author)
     self.committer = None if committer is None else Actor(*committer)
Ejemplo n.º 21
0
def gitdir(tmpdir):
    """ Create a project and add code to it """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)
    testpath = tmppath / "src" / "test.py"
    (tmppath / "src").mkdir()
    # Write a test file to the repo
    with open(testpath, "w") as test_txt:
        test_txt.write("import abc")

    index = repo.index
    index.add([str(testpath)])

    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    index.commit("basic test", author=author, committer=committer)

    first_test = """
    import abc
    foo = 1
    def function1():
        a = 1 + 1
    
    class Class1(object):
        def method(self):
            b = 1 + 5
    """
    with open(testpath, "w") as test_txt:
        test_txt.write(dedent(first_test))

    index.add([str(testpath)])
    index.commit("add line", author=author, committer=committer)

    second_test = """
    import abc
    foo = 1
    def function1():
        a = 1 + 1
    class Class1(object):
        def method(self):
            b = 1 + 5
            if b == 6:
                return 'banana'
    """

    with open(testpath, "w") as test_txt:
        test_txt.write(dedent(second_test))

    with open(tmppath / ".gitignore", "w") as test_txt:
        test_txt.write(".wily/")

    index.add([str(testpath), ".gitignore"])
    index.commit("remove line", author=author, committer=committer)

    return tmpdir
Ejemplo n.º 22
0
def test_build_twice(tmpdir, cache_path):
    """
    Test that build works when run twice.
    """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    # Write a test file to the repo
    with open(tmppath / "test.py", "w") as test_txt:
        test_txt.write("import abc")

    index = repo.index
    index.add(["test.py"])

    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    commit = index.commit("basic test", author=author, committer=committer)

    runner = CliRunner()
    result = runner.invoke(
        main.cli,
        [
            "--debug", "--path", tmpdir, "--cache", cache_path, "build",
            "test.py"
        ],
    )
    assert result.exit_code == 0, result.stdout

    cache_path = pathlib.Path(cache_path) / "git"
    assert cache_path.exists()
    index_path = cache_path / "index.json"
    assert index_path.exists()
    rev_path = cache_path / (commit.name_rev.split(" ")[0] + ".json")
    assert rev_path.exists()

    # Write a test file to the repo
    with open(tmppath / "test.py", "w") as test_txt:
        test_txt.write("import abc\nfoo = 1")

    index.add(["test.py"])

    commit2 = index.commit("basic test", author=author, committer=committer)
    repo.close()

    result = runner.invoke(main.cli,
                           ["--debug", "--path", tmpdir, "build", "test.py"])
    assert result.exit_code == 0, result.stdout

    assert cache_path.exists()
    index_path = cache_path / "index.json"
    assert index_path.exists()
    rev_path = cache_path / (commit.name_rev.split(" ")[0] + ".json")
    assert rev_path.exists()
    rev_path2 = cache_path / (commit2.name_rev.split(" ")[0] + ".json")
    assert rev_path2.exists()
Ejemplo n.º 23
0
def commit(repo, commit_message, cfg):
    if cfg.username and cfg.usermail:
        author = Actor(cfg.username, cfg.usermail)
    else:
        author = Actor.committer(repo.config_reader())

    if cfg.trace:
        click.echo(f' > Using "{author.name} <{author.email}>" as commit author')

    repo.index.commit(commit_message, author=author, committer=author)
Ejemplo n.º 24
0
def commit(date):
    repo = Repo(search_parent_directories=True)

    author = Actor('wirbot', '*****@*****.**')
    committer = Actor('wirbot', '*****@*****.**')

    repo.git.add('.')
    repo.index.commit(f'Updates: {date}', author=author, committer=committer)

    repo.git.push()
Ejemplo n.º 25
0
def init_repo2():
    repo = Repo.init(os.path.join(REPOS_ROOT, "repo2"))
    repo_file(repo, "bar.txt", "BAR")
    repo.index.add([os.path.join(repo.working_dir, "bar.txt")])
    commit = repo.index.commit("Commit 2",
                               committer=Actor("Committer2",
                                               "*****@*****.**"),
                               author=Actor("Author2", "*****@*****.**"))
    repo.create_head("master2", commit)
    return IndividualRepo(repo.working_dir, "master2")
Ejemplo n.º 26
0
def test_git_end_to_end(tmpdir):
    """
    Complete end-to-end test of the git integration
    """
    repo = Repo.init(path=tmpdir)
    tmppath = pathlib.Path(tmpdir)

    index = repo.index
    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    # First commit
    with open(tmppath / ".gitignore", "w") as ignore:
        ignore.write(".wily/")
    index.add([".gitignore"])
    commit1 = index.commit("commit1", author=author, committer=committer)

    # Second commit
    with open(tmppath / "test.py", "w") as file1:
        file1.write("print(1)")
    index.add(["test.py"])
    commit2 = index.commit("commit2", author=author, committer=committer)
    repo.close()

    config = DEFAULT_CONFIG
    config.path = tmpdir

    archiver = GitArchiver(config)
    assert archiver.config == config

    revisions = archiver.revisions(tmpdir, 3)
    assert len(revisions) == 2
    assert revisions[0].message == "commit2"
    assert revisions[0].author_email == "*****@*****.**"
    assert revisions[0].author_name == "An author"
    assert (
        revisions[0].key in commit2.name_rev
        and revisions[0].key not in commit1.name_rev
    )

    assert revisions[1].message == "commit1"
    assert revisions[1].author_email == "*****@*****.**"
    assert revisions[1].author_name == "An author"
    assert (
        revisions[1].key in commit1.name_rev
        and revisions[1].key not in commit2.name_rev
    )

    checkout = archiver.checkout(revisions[1], None)

    assert not (tmppath / "test.py").exists()

    finish = archiver.finish()

    assert (tmppath / "test.py").exists()
Ejemplo n.º 27
0
def make_commits(repo: Repo,
                 commits_data: list,
                 has_mirror: bool = False) -> list:
    """Loads commit data from JSON file and makes commits in given repo.

    Args:
        repo (`Repo`): git repo instance where commits will be made
        commits_data (list): Contains the commit details to write
        has_mirror (bool): Indicates whether to write to `.gitchmirror`

    Returns:
        list: list of dicts representing commits made
    """
    # Simulate git add-commit workflow for each commit item
    for i, commit_item in enumerate(commits_data):
        changes = []
        hexsha = commit_item["hexsha"]
        message = commit_item["message"]
        commit_dt = datetime.fromtimestamp(
            commit_item["timestamp"]).isoformat()

        # Create new file
        fname = f"{i:05d}.txt"
        fpath = os.path.join(repo.working_dir, fname)
        with open(fpath, "w", encoding="utf-8") as f:
            # Write commit message as file content
            f.write(message)
        changes.append(fpath)

        # Write to .gitchmirror file
        if has_mirror:
            gpath = os.path.join(repo.working_dir, GITCHMFILE)
            with open(gpath, "a+", encoding="utf-8") as g:
                g.write(f"{hexsha}\n")
            changes.append(gpath)

        # Create author and committer
        author = Actor(name=commit_item["author_name"],
                       email=commit_item["author_email"])
        committer = Actor(
            name=commit_item["committer_name"],
            email=commit_item["committer_email"],
        )

        # Stage and commit the created file(s)
        repo.index.add(changes)
        repo.index.commit(
            message=message,
            author=author,
            author_date=commit_dt,
            committer=committer,
            commit_date=commit_dt,
        )

    return commits_data
Ejemplo n.º 28
0
    def add_and_commit(self, repo_dir, filename):

        author = Actor("teaster", "*****@*****.**")
        committer = Actor("teaster", "*****@*****.**")

        repo = Repo('{}/{}'.format(docker_build_dir, repo_dir))
        new_file_path = os.path.join(repo.working_tree_dir, filename)
        repo.index.add([new_file_path])

        repo.index.commit("added Dockerfile",
                          author=author,
                          committer=committer)
Ejemplo n.º 29
0
    def test_repo_not_synced(self):
        index = self.bp_repo.index

        new_file_path = os.path.join(self.test_dir, "new-file-name")
        open(new_file_path, "w").close()
        index.add([new_file_path])

        author = Actor("An author", "*****@*****.**")
        committer = Actor("A committer", "*****@*****.**")

        index.commit("my commit message", author=author, committer=committer)
        self.assertFalse(self.bp_repo.is_current_branch_synced())
Ejemplo n.º 30
0
def test_operator_on_code_with_metric_named_objects(operator, tmpdir):
    code_with_metric_named_objects = """

    # CyclomaticComplexity
    def complexity(): pass

    # Halstead
    def h1(): pass
    def h2(): pass
    def N1(): pass
    def N2(): pass
    def vocabulary(): pass
    def length(): pass
    def volume(): pass
    def difficulty(): pass
    def error(): pass

    # Maintainability
    def rank(): pass
    def mi(): pass

    # RawMetrics
    def loc(): pass
    def lloc(): pass
    def sloc(): pass
    def comments(): pass
    def multi(): pass
    def blank(): pass
    def single_comments(): pass

    """

    testpath = pathlib.Path(tmpdir) / "test.py"
    author = Actor("An author", "*****@*****.**")
    committer = Actor("A committer", "*****@*****.**")

    with open(testpath, "w") as test_py:
        test_py.write(dedent(code_with_metric_named_objects))

    with Repo.init(path=tmpdir) as repo:
        repo.index.add(["test.py"])
        repo.index.commit("add test.py", author=author, committer=committer)

    runner = CliRunner()

    result = runner.invoke(
        main.cli,
        ["--debug", "--path", tmpdir, "build",
         str(testpath), "-o", operator],
        catch_exceptions=False,
    )
    assert result.exit_code == 0, result.stdout
Ejemplo n.º 31
0
    def test_stats(self):
        commit = self.rorepo.commit('33ebe7acec14b25c5f84f35a664803fcab2f7781')
        stats = commit.stats

        def check_entries(d):
            assert isinstance(d, dict)
            for key in ("insertions", "deletions", "lines"):
                assert key in d
        # END assertion helper
        assert stats.files
        assert stats.total

        check_entries(stats.total)
        assert "files" in stats.total

        for filepath, d in stats.files.items():
            check_entries(d)
        # END for each stated file

        # assure data is parsed properly
        michael = Actor._from_string("Michael Trier <*****@*****.**>")
        assert commit.author == michael
        assert commit.committer == michael
        assert commit.authored_date == 1210193388
        assert commit.committed_date == 1210193388
        assert commit.author_tz_offset == 14400, commit.author_tz_offset
        assert commit.committer_tz_offset == 14400, commit.committer_tz_offset
        assert commit.message == "initial project\n"
Ejemplo n.º 32
0
 def test_unicode_actor(self):
     # assure we can parse unicode actors correctly
     name = u"Üäöß ÄußÉ"
     assert len(name) == 9
     special = Actor._from_string(u"%s <*****@*****.**>" % name)
     assert special.name == name
     assert isinstance(special.name, text_type)
Ejemplo n.º 33
0
    def test_from_string_should_separate_name_and_email(self):
        a = Actor._from_string("Michael Trier <*****@*****.**>")
        assert_equal("Michael Trier", a.name)
        assert_equal("*****@*****.**", a.email)

        # base type capabilities
        assert a == a
        assert not (a != a)
        m = set()
        m.add(a)
        m.add(a)
        assert len(m) == 1
Ejemplo n.º 34
0
def newcommit(repo, message, date, name=None, email=None):
    """Creates a commit object with a custom date.
    :param repo: Repo object the commit should be part of
    :param tree: Tree object
    :param message: Commit message. It may be an empty string
                    if no message is provided.
    :param date: Date in seconds, as an Int
    :return: Commit object representing the new commit
    :note:
        Additional information about the committer and Author are taken from
        the git configuration
    """

    tree = repo.index.write_tree()
    try:
        parents = [repo.head.commit]
    except ValueError:
        parents = []

    # Committer and Author info
    cr = repo.config_reader()
    if name == None or email == None:
        actor = Actor.committer(cr)
    else:
        actor = Actor(name, email)

    committer = actor
    author = actor

    # Date
    # offset = altzone # 3600*offsethours
    offset = 0  # UTC

    author_time, author_offset = date, offset
    committer_time, committer_offset = date, offset

    # assume utf8 encoding
    enc_section, enc_option = Commit.conf_encoding.split('.')
    conf_encoding = cr.get_value(enc_section,
                                 enc_option,
                                 Commit.default_encoding)

    # Create New Commit
    new_commit = Commit(repo, Commit.NULL_BIN_SHA, tree,
                    author, author_time, author_offset,
                    committer, committer_time, committer_offset,
                    message, parents, conf_encoding)

    stream = StringIO()
    new_commit._serialize(stream)
    streamlen = stream.tell()
    stream.seek(0)

    istream = repo.odb.store(IStream(Commit.type, streamlen, stream))
    new_commit.binsha = istream.binsha

    # Advance HEAD to the new commit automatically.
    # need late import here, importing git at the very beginning throws.
    import git.refs
    try:
        repo.head.set_commit(new_commit, logmsg="commit: %s" % message)
    except ValueError:
        # head is not yet set to the ref our HEAD points to
        # Happens on first commit
        import git.refs
        master = git.refs.Head.create(repo, repo.head.ref,
                                      new_commit, logmsg="commit (initial): %s" % message)
        repo.head.set_reference(master, logmsg='commit: Switching to %s' % master)
    # END handle empty repositories
    return new_commit
Ejemplo n.º 35
0
 def test_from_string_should_handle_just_name(self):
     a = Actor._from_string("Michael Trier")
     assert_equal("Michael Trier", a.name)
     assert_equal(None, a.email)
Ejemplo n.º 36
0
 def test_str_should_alias_name(self):
     a = Actor._from_string("Michael Trier <*****@*****.**>")
     assert_equal(a.name, str(a))
Ejemplo n.º 37
0
 def test_should_display_representation(self):
     a = Actor._from_string("Michael Trier <*****@*****.**>")
     assert_equal('<git.Actor "Michael Trier <*****@*****.**>">', repr(a))