<repo_name>hkage/git-tools<file_sep>/commands/git-lost-commits
#!/usr/bin/env python3

import argparse
import subprocess

# Copied from SO http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'


def get_commits():
    p = subprocess.Popen(['git', '--no-pager', 'log', '--decorate=short',
                          '--pretty=format:%h;%ci;%an;%s', '--all'],
                         stdout=subprocess.PIPE)
    return p.stdout.readlines()


def get_branches_containing_commit(revision):
    p = subprocess.Popen(['git', 'branch', '-r', '--contains', revision],
                         stdout=subprocess.PIPE)
    return p.stdout.readlines()


def main():
    try:
        for line in get_commits():
            data = line.split(b';')
            revision = data[0]
            creation_time = data[1][:19]
            author = data[2].replace(b'\n', b'')
            message = '%s...' % data[3].replace(b'\n', b'')[:60]
            branches = get_branches_containing_commit(revision)
            if len(branches) > 0:
                continue
            print(creation_time, FAIL, revision, ENDC, OKBLUE, author, ENDC, message)
    except KeyboardInterrupt:
        print("Terminated")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Script to check for potential'
                                                 ' missing commits.')
    args = parser.parse_args()
    main()
<file_sep>/README.md
# git-tools

git-lost-commits will look for commits that were pushed to the git server but are potentially not part of any remote branch.

## Requirements

* Python 3
* Flake8
* git installed

## git hooks

### pre-commit-python

This is a git pre-commit hook, that will do some Python/Django specific validation checks:

* Flake8 checks for the changed files
* Check for Python debug statements (ipbd/pdb)
* Check for fuzzy translations in PO files

## git-lost-commits

### Usage

Simply type:

    $ ./git-lost-commits

and the result should look like this e.g.:

    b'2017-03-09 16:20:20'  b'4c7c37c'   b'H<NAME>'  b"Merge branch 'feature/abc' into development"...
    b'2017-03-09 14:59:43'  b'ebe9be0'   b'Henning Kage'  b"Merge branch 'feature/def' into 'feature/abc'"...
    b'2017-03-09 14:29:00'  b'2d2b740'   b'H<NAME>'  b"Merge branch 'development' into feature/abc"...
