# Buggy code
def check_git_src(dockerfile_path):
    """Given the src_path and the dockerfile path, return the git
    repository name and sha information in the format of string.
    Currently we only consider the following situation:
    - target_git_project
        - dir1
        - dir2/dockerfile
    So we only use dockerfile_path to find the git repo info."""
    # get the path of the folder containing the dockerfile
    dockerfile_folder_path = os.path.dirname(dockerfile_path)
    # locate the top level directory
    path_to_toplevel = get_git_toplevel(dockerfile_folder_path)
    # get the path of the target folder or file
    logger.debug("looking into path: %s for git repo.", path_to_toplevel)
    comment_line = ""
    if path_to_toplevel:
        sha_info = get_git_sha(path_to_toplevel)
        # if path_to_toplevel exists, name_info should be the folder name
        name_info = os.path.basename(path_to_toplevel)
        comment_line = "git project name: " + name_info + ", HEAD sha: " + sha_info
    else:
        comment_line = "Not a git repository"
    return comment_line


# Traceback (FileNotFoundError)
Traceback (most recent call last):
File "/home/rjudge/ternenv/bin/tern", line 10, in <module>
sys.exit(main())
File "/home/rjudge/ternenv/tern/tern/__main__.py", line 201, in main
do_main(args)
File "/home/rjudge/ternenv/tern/tern/__main__.py", line 90, in do_main
run.execute_dockerfile(args)
File "/home/rjudge/ternenv/tern/tern/analyze/docker/run.py", line 176, in execute_dockerfile
output = dockerfile.create_locked_dockerfile(dfobj)
File "/home/rjudge/ternenv/tern/tern/analyze/docker/dockerfile.py", line 283, in create_locked_dockerfile
expand_add_command(dfobj)
File "/home/rjudge/ternenv/tern/tern/analyze/docker/dockerfile.py", line 270, in expand_add_command
dockerfile_path)
File "/home/rjudge/ternenv/tern/tern/analyze/docker/dockerfile.py", line 256, in find_git_info
comment_line = common.check_git_src(dockerfile_path)
File "/home/rjudge/ternenv/tern/tern/analyze/common.py", line 536, in check_git_src
path_to_toplevel = get_git_toplevel(dockerfile_folder_path)
File "/home/rjudge/ternenv/tern/tern/analyze/common.py", line 618, in get_git_toplevel
command, stderr=subprocess.DEVNULL, cwd=path)
File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '': ''

# Fixed code
def check_git_src(dockerfile_path):
    """Given the src_path and the dockerfile path, return the git
    repository name and sha information in the format of string.
    Currently we only consider the following situation:
    - target_git_project
        - dir1
        - dir2/dockerfile
    So we only use dockerfile_path to find the git repo info."""
    # get the path of the folder containing the dockerfile
    dockerfile_folder_path = os.path.dirname(os.path.abspath(dockerfile_path))
    # locate the top level directory
    path_to_toplevel = get_git_toplevel(dockerfile_folder_path)
    # get the path of the target folder or file
    logger.debug("looking into path: %s for git repo.", path_to_toplevel)
    comment_line = ""
    if path_to_toplevel:
        sha_info = get_git_sha(path_to_toplevel)
        # if path_to_toplevel exists, name_info should be the folder name
        name_info = os.path.basename(path_to_toplevel)
        comment_line = "git project name: " + name_info + ", HEAD sha: " + sha_info
    else:
        comment_line = "Not a git repository"
    return comment_line