Exemplo n.º 1
0
def tmprepo_context():
    """Return a newly created GitClone, remove when expired.

    Usage examples:

        Create a temporary repo:
        >>> with tmprepo_context() as clone:
        ...     status = clone.call("rev-parse", "--is-inside-work-tree")
        ...     status.strip().lower() == 'true'
        True

    """
    with phlsys_fs.tmpdir_context() as tmpdir:
        clone = GitClone(tmpdir)
        clone.call("init")
        yield clone
def create_add_file(path, content):
    """Return a string diff which adds a new file with the specified content.

    Raise Exception if the is absolute, instead of relative.

    Usage examples:

        create a diff for new file 'README' with content 'hello'
        >>> print create_add_file('README', 'hello')
        diff --git a/README b/README
        new file mode 100644
        index 0000000..b6fc4c6
        --- /dev/null
        +++ b/README
        @@ -0,0 +1 @@
        +hello
        \ No newline at end of file
        <BLANKLINE>

    :path: the string path of the new file
    :content: the string content of the new file
    :returns: the string diff result

    """
    left = "/dev/null"  # this seems to work just the same on Windows
    right = path

    if os.path.isabs(path):
        raise Exception(
            "create_add_file: cannot create fake diff for absolute path")

    with phlsys_fs.tmpdir_context() as dir_name:

        right_full = os.path.join(dir_name, right)
        with open(right_full, "w") as f:
            f.write(content)

        diff = no_index(left, right, dir_name)

    return diff
Exemplo n.º 3
0
def create_add_file(path, content):
    """Return a string diff which adds a new file with the specified content.

    Raise Exception if the is absolute, instead of relative.

    Usage examples:

        create a diff for new file 'README' with content 'hello'
        >>> print(create_add_file('README', 'hello'))
        diff --git a/README b/README
        new file mode 100644
        index 0000000..b6fc4c6
        --- /dev/null
        +++ b/README
        @@ -0,0 +1 @@
        +hello
        \ No newline at end of file
        <BLANKLINE>

    :path: the string path of the new file
    :content: the string content of the new file
    :returns: the string diff result

    """
    left = "/dev/null"  # this seems to work just the same on Windows
    right = path

    if os.path.isabs(path):
        raise Exception(
            "create_add_file: cannot create fake diff for absolute path")

    with phlsys_fs.tmpdir_context() as dir_name:

        right_full = os.path.join(dir_name, right)
        with open(right_full, "w") as f:
            f.write(content)

        diff = no_index(left, right, dir_name)

    return diff