Exemple #1
0
def test_minimal_repo(tmp_path):
    dir1 = tmp_path / 'dir1'
    dir1.mkdir()
    file1 = dir1 / 'file1.txt'
    file1.write_text('content file1.txt v1')
    assert len(list(tmp_path.iterdir())) == 1
    assert file1.is_file()
    assert file1.read_text() == 'content file1.txt v1'

    file2 = tmp_path / 'file2.txt'
    file2.write_text('content file2.txt v1')
    assert len(list(tmp_path.iterdir())) == 2
    assert file2.is_file()
    assert file2.read_text() == 'content file2.txt v1'

    execute_bash_command(tmp_path, 'git init')
    assert len(list(tmp_path.iterdir())) == 3
    assert (tmp_path / '.git').is_dir()

    execute_bash_command(tmp_path, 'git add -A')
    execute_bash_command(tmp_path, 'git commit -m commit1')

    dg.DotGraph(str(tmp_path)).persist()

    assert len(list(tmp_path.iterdir())) == 4
    assert (tmp_path / '.gitGraph').is_dir()
    assert len(list((tmp_path / '.gitGraph').iterdir())) == 2

    dot_file_path = list((tmp_path / '.gitGraph').glob('*.dot'))[0]
    with open(dot_file_path) as dot_file:
        dot_graph = dot_file.readlines()
        dot_graph[3:-1] = sorted(dot_graph[3:-1])
        assert ''.join(dot_graph) == minimal_repo_dot_graph
Exemple #2
0
def main(args=None):
    example_text = '''examples:
    git graph
    git graph -p examples/demo -n btc -f svg
    '''

    node_text = '''node types to display in the graph (default is all).
    'commits' and 'branches' will focus output on commits and branches
    respectively.
    For further control, you can also pick the letters corresponding to
    your choice:
    | Node type      | Letter |
    | -------------- | ------ |
    | blob           | b      |
    | tree           | t      |
    | commit         | c      |
    | local branche  | l      |
    | local head     | h      |
    | remote branche | r      |
    | remote head    | d      |
    | remote server  | s      |
    | annotated tag  | a      |
    | tag            | g      |
    | upstream link  | u      |
    '''

    parser = argparse.ArgumentParser(
        prog='git graph',
        description='Save and display your Git repositories inner content '
                    'as a Directed Acyclic Graph (DAG)',
        epilog=example_text,
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-V', '--version', action='version',
                        version=__version__)
    parser.add_argument('-p', '--path', type=str, action='store', default='.',
                        help='path to your git repository (default is here)')
    parser.add_argument('-n', '--nodes', default=dg.ALL_NODES,
                        help=node_text)
    parser.add_argument('-f', '--format', default=dg.DEFAULT_FORMAT,
                        help='format of graph output: pdf, svg, png... '
                             '(default is pdf)')
    parser.add_argument('-c', '--conceal', action='store_true', default=False,
                        help='conceal graph (deactivated by default)')
    args = parser.parse_args(args=args)

    git_path = dg.get_git_repository(pathlib.Path(args.path))
    if git_path is not None:
        dot_graph = dg.DotGraph(git_path, nodes=args.nodes)
        file = dot_graph.persist(form=args.format, conceal=args.conceal)
        git_graph_path = git_path.resolve() / '.gitGraph'
        print(f'{file} saved in {git_graph_path}')
        return file
    else:
        print('Not a git repository')
Exemple #3
0
def test_repo(build_repo):
    git_repo_type, nb_files, tmp_path = build_repo

    output_file = dg.DotGraph(tmp_path).persist()

    git_graph_path = tmp_path / '.gitGraph'
    image_file = git_graph_path / output_file
    dot_file = git_graph_path / image_file.stem

    assert len(list(tmp_path.iterdir())) == nb_files
    assert git_graph_path.is_dir()
    assert len(list(git_graph_path.iterdir())) == 2

    with open(dot_file) as f:
        dot_graph = f.readlines()
        dot_graph[3:-1] = sorted(dot_graph[3:-1])
        golden = tests_data_path / git_repo_type / '.gitGraph' / 'expected.dot'
        with open(golden) as expected_dot_graph:
            assert ''.join(dot_graph) == expected_dot_graph.read()
Exemple #4
0
def test_remote_repo(tmp_path):
    # resume from full_repo
    # git tag v0.0.2 -am "good job"
    # git remote add origin https://github.com/hoduche/git-empty.git
    # git push -u origin master
    # git checkout idea1
    # git push -u origin idea1
    # git checkout master
    # git push origin --tags

    execute_bash_command(tmp_path,
                         'git clone https://github.com/hoduche/git-empty .')
    execute_bash_command(tmp_path, 'git checkout -b idea1 origin/idea1')
    execute_bash_command(tmp_path, 'git branch idea2 v0.0.1')

    dg.DotGraph(str(tmp_path)).persist()
    dot_file_path = list((tmp_path / '.gitGraph').glob('*.dot'))[0]
    with open(dot_file_path) as dot_file:
        dot_graph = dot_file.readlines()
        dot_graph[3:-1] = sorted(dot_graph[3:-1])
        assert ''.join(dot_graph) == remote_repo_dot_graph
Exemple #5
0
def test_full_repo(tmp_path):
    execute_bash_command(tmp_path, 'git init')

    dir1 = tmp_path / 'dir1'
    dir1.mkdir()
    file1 = dir1 / 'file1.txt'
    file1.write_text('content file1.txt v1')
    file2 = tmp_path / 'file2.txt'
    file2.write_text('content file2.txt v1')
    execute_bash_command(tmp_path, 'git add -A')
    execute_bash_command(tmp_path, 'git commit -m commit1')

    execute_bash_command(tmp_path, 'git branch idea1')
    execute_bash_command(tmp_path, 'git checkout idea1')

    file2.write_text('content file2.txt v2')
    file3 = tmp_path / 'file3.txt'
    file3.write_text('content file1.txt v1')
    execute_bash_command(tmp_path, 'git add -A')
    execute_bash_command(tmp_path, 'git commit -m commit2')

    execute_bash_command(tmp_path, 'git checkout master')
    execute_bash_command(tmp_path, 'git checkout -b idea2')
    file1.write_text('content file1.txt v2')
    execute_bash_command(tmp_path, 'git commit -am commit3')

    execute_bash_command(tmp_path, 'git checkout master')
    execute_bash_command(tmp_path, 'git merge idea2')
    execute_bash_command(tmp_path, 'git tag v0.0.1')

    execute_bash_command(tmp_path, 'git merge idea1 --no-edit')
    #    execute_bash_command(tmp_path, 'git tag v0.0.2 -am "good job"')

    dg.DotGraph(str(tmp_path)).persist()
    dot_file_path = list((tmp_path / '.gitGraph').glob('*.dot'))[0]
    with open(dot_file_path) as dot_file:
        dot_graph = dot_file.readlines()
        dot_graph[3:-1] = sorted(dot_graph[3:-1])
        assert ''.join(dot_graph) == full_repo_dot_graph