# Buggy code
def _git_toplevel(path):
    try:
        with open(os.devnull, "wb") as devnull:
            out = subprocess.check_output(
                ["git", "rev-parse", "--show-toplevel"],
                cwd=(path or "."),
                universal_newlines=True,
                stderr=devnull,
            )
        return os.path.normcase(os.path.realpath(out.strip()))
    except subprocess.CalledProcessError:
        # git returned error, we are not in a git repo
        return None
    except OSError:
        # git command not found, probably
        return None


# Traceback (tarfile.EmptyHeaderError)
/usr/lib64/python3.7/distutils/dist.py:274: UserWarning: Unknown distribution option: 'test_requires'
warnings.warn(msg)
/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/version.py:191: UserWarning: meta invoked without explicit configuration, will use defaults where required.
"meta invoked without explicit configuration,"
running egg_info
writing rio_redis.egg-info/PKG-INFO
writing dependency_links to rio_redis.egg-info/dependency_links.txt
writing requirements to rio_redis.egg-info/requires.txt
writing top-level names to rio_redis.egg-info/top_level.txt
fatal: Not a valid object name
Traceback (most recent call last):
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 2287, in next
tarinfo = self.tarinfo.fromtarfile(self)
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1093, in fromtarfile
obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1029, in frombuf
raise EmptyHeaderError("empty header")
tarfile.EmptyHeaderError: empty header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "setup.py", line 39, in <module>
python_requires=">=3.6.0",
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/__init__.py", line 131, in setup
return distutils.core.setup(**attrs)
File "/usr/lib64/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib64/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib64/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 278, in run
self.find_sources()
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 293, in find_sources
mm.run()
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 524, in run
self.add_defaults()
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 563, in add_defaults
rcfiles = list(walk_revctrl())
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/sdist.py", line 20, in walk_revctrl
for item in ep.load()(dirname):
File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/integration.py", line 33, in find_files
res = command(path)
File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/file_finder_git.py", line 47, in git_find_files
git_files, git_dirs = _git_ls_files_and_dirs(toplevel)
File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/file_finder_git.py", line 31, in _git_ls_files_and_dirs
tf = tarfile.open(fileobj=proc.stdout, mode="r|*")
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1601, in open
t = cls(name, filemode, stream, **kwargs)
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1482, in __init__
self.firstmember = self.next()
File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 2302, in next
raise ReadError("empty file")
tarfile.ReadError: empty file

# Fixed code
def _git_toplevel(path):
    try:
        with open(os.devnull, "wb") as devnull:
            out = subprocess.check_output(
                ["git", "rev-parse", "--show-toplevel"],
                cwd=(path or "."),
                universal_newlines=True,
                stderr=devnull,
            )
        trace("find files toplevel", out)
        return os.path.normcase(os.path.realpath(out.strip()))
    except subprocess.CalledProcessError:
        # git returned error, we are not in a git repo
        return None
    except OSError:
        # git command not found, probably
        return None