"""
Demo git
Packages:
  PyYaml    - import yaml
  GitPython - import git
"""
import git
import yaml


def parse_config():
    cfg_file = open('gittest_conf.yaml', 'r')
    cfg_map = yaml.safe_load(cfg_file)
    cfg_file.close()
    return cfg_map


def dump_gitlog_as_string(cfg):
    g = git.Git(cfg.get('repo_dir', '.'))
    log_info = g.log()
    print("Contents of log_info:\n" + str(log_info))
    print("Data type of log_info: " + str(type(log_info)))


def working_repo_example(cfg):
    print("\n\ngit.Repo example:\n--------------------------------\n")
    repo = git.Repo(cfg.get('repo_dir', '.'))
    assert not repo.bare
    print("repo.head.ref: %s" % repo.head.ref)

    heads = repo.heads
    master_branch = heads['master']

    # print the reflog
    for entry in master_branch.log():
#        print(dir(entry))
        print('newhexsha: %s' % str(entry.newhexsha))
        print('  oldhexsha: %s' % str(entry.oldhexsha))
        print('  actor: %s' % str(entry.actor))
        print('  message: %s' % str(entry.message))
        print('  format(): %s' % str(entry.format()))
        print('  time: %s' % str(entry.time))
        commit = repo.commit(entry.newhexsha)
        print('  commit object:')
        print('    commit: %s' % commit)
        print('    authored_datetime: %s' % commit.authored_datetime)
        print('    committer : %s' % commit.committer)
        print('     : %s' % commit.message)
        print('     : %s' % commit.parents)
        print('     : %s' % str(commit.repo))
        print('     : %s' % commit.summary)
        print("---\n")

    commit = master_branch.commit
    print('current commit: %s' % commit)


def main():
    cfg = parse_config()
    print(cfg)
#    dump_gitlog_as_string(cfg)
    working_repo_example(cfg)


if __name__ == '__main__':
    main()
