{"record":{"id":"ec48241d69eb4c3f","repo":"dotnet/runtime","slug":"couldn-t-determine-current-git-hash","errorCode":null,"errorMessage":"Couldn't determine current git hash","messagePattern":"Couldn't determine current git hash","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/coreclr/scripts/jitrollingbuild.py","lineNumber":185,"sourceCode":"    \"\"\"\n\n    if coreclr_args.git_hash is not None:\n        return\n\n    # Do all the remaining commands, including a number of 'git' commands including relative paths,\n    # from the root of the runtime repo.\n\n    with ChangeDir(coreclr_args.runtime_repo_location):\n        command = [ \"git\", \"rev-parse\", \"HEAD\" ]\n        logging.debug(\"Invoking: {}\".format(\" \".join(command)))\n        proc = subprocess.Popen(command, stdout=subprocess.PIPE)\n        stdout_git_rev_parse, _ = proc.communicate()\n        return_code = proc.returncode\n        if return_code == 0:\n            current_hash = stdout_git_rev_parse.decode('utf-8').strip()\n            logging.info(\"Current hash: {}\".format(current_hash))\n        else:\n            raise RuntimeError(\"Couldn't determine current git hash\")\n\n        # We've got the current hash; figure out the baseline hash.\n        # First find the newest hash for any branch matching */main.\n        command = [ \"git\", \"branch\", \"-r\", \"--sort=-committerdate\", \"-v\", \"--list\", \"*/main\" ]\n        logging.debug(\"Invoking: %s\", \" \".join(command))\n        proc = subprocess.Popen(command, stdout=subprocess.PIPE)\n        stdout_git_main_branch, _ = proc.communicate()\n        return_code = proc.returncode\n        if return_code != 0:\n            raise RuntimeError(\"Couldn't determine newest 'main' git hash\")\n\n        main_hash = stdout_git_main_branch.decode('utf-8').strip().split()[1]\n\n        # Get the merge-base between the newest main and our current rev\n        command = [ \"git\", \"merge-base\", current_hash, main_hash ]\n        logging.debug(\"Invoking: %s\", \" \".join(command))\n        proc = subprocess.Popen(command, stdout=subprocess.PIPE)\n        stdout_git_merge_base, _ = proc.communicate()","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/coreclr/scripts/jitrollingbuild.py#L167-L203","documentation":"Raised by process_git_hash_arg() in jitrollingbuild.py when 'git rev-parse HEAD' fails (returns non-zero exit code). This command retrieves the current commit hash of the repository. The failure means git cannot determine the current HEAD, which is the starting point for finding a baseline JIT build for asm diffs.","triggerScenarios":"Triggered when subprocess.Popen(['git', 'rev-parse', 'HEAD']) returns a non-zero returncode. The command is run from coreclr_args.runtime_repo_location (the root of the runtime repo). This fails if the directory is not a git repository, if HEAD is detached in a broken state, if git is not installed or not on PATH, or if the .git directory is corrupt.","commonSituations":"The script is run from a directory that is not a git repo (e.g., a downloaded tarball instead of a clone). The git binary is not available in the environment. The repository is in a corrupted state after an interrupted operation. The runtime_repo_location was computed incorrectly and points to a non-repo directory.","solutions":["Verify the script is being run from within a valid git clone of the dotnet/runtime repository.","Check that 'git rev-parse HEAD' works manually from the same directory.","Ensure git is installed and on PATH (run 'which git').","If using -git_hash argument instead, this code path is skipped entirely — pass -git_hash to bypass the auto-detection.","Verify coreclr_args.runtime_repo_location points to the correct repo root."],"exampleFix":"# before\ncommand = [\"git\", \"rev-parse\", \"HEAD\"]\nproc = subprocess.Popen(command, stdout=subprocess.PIPE)\nstdout_git_rev_parse, _ = proc.communicate()\nreturn_code = proc.returncode\nif return_code == 0:\n    current_hash = stdout_git_rev_parse.decode('utf-8').strip()\nelse:\n    raise RuntimeError(\"Couldn't determine current git hash\")\n\n# after - include git stderr in error for diagnosis\ncommand = [\"git\", \"rev-parse\", \"HEAD\"]\nproc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\nstdout_git_rev_parse, stderr = proc.communicate()\nreturn_code = proc.returncode\nif return_code == 0:\n    current_hash = stdout_git_rev_parse.decode('utf-8').strip()\nelse:\n    raise RuntimeError(\n        \"Couldn't determine current git hash: {}\".format(\n            stderr.decode('utf-8').strip()))","handlingStrategy":"validation","validationCode":"import subprocess, os\n\ndef can_determine_git_hash(repo_path: str) -> bool:\n    \"\"\"Check if git rev-parse HEAD works in the given repo.\"\"\"\n    if not os.path.isdir(os.path.join(repo_path, '.git')):\n        return False\n    result = subprocess.run(\n        ['git', 'rev-parse', 'HEAD'],\n        cwd=repo_path, capture_output=True)\n    return result.returncode == 0 and bool(result.stdout.strip())\n\n# Usage before calling process_git_hash_arg:\nif not can_determine_git_hash(coreclr_args.runtime_repo_location):\n    print(\"ERROR: Cannot determine git hash. Pass -git_hash explicitly.\")\n    sys.exit(1)","typeGuard":"null","tryCatchPattern":"try:\n    process_git_hash_arg(coreclr_args)\nexcept RuntimeError as e:\n    if \"current git hash\" in str(e):\n        logging.error(f\"Git error: {e}. Ensure you're in a valid git repo or pass -git_hash.\")\n    raise","preventionTips":["Always run from within a valid git clone of the repository.","Pass -git_hash explicitly when you know the target hash to avoid git auto-detection entirely.","Ensure git is installed and on PATH.","Verify the repo is not in a corrupted state with 'git fsck'.","Check that runtime_repo_location resolves to the actual repo root."],"tags":["git","jit","rolling-build","coreclr","superpmi"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}