import re


def parse_ignore_file(ignorefile, include_star=True):
    """Parses a .gitignore or .cfignore file for fnmatch patterns
    """
    if ignorefile is None:
        return []

    try:
        with open(ignorefile, 'r') as f:
            _cfignore = f.read().split('\n')

        cfignore = []
        for l in _cfignore:
            if l and not l.startswith('#'):
                l = re.sub('\s*#.*', '', l).strip()
                if include_star and l.endswith('/'):
                    l += '*'
                cfignore.append(l)
    except Exception as e:
        print(e)
        cfignore = []

    cfignore.extend(['.git/', '.gitignore', '.cfignore', 'manifest.yml'])

    return cfignore