'use strict';
import _0x1869d0__default from 'path';
import _0x3881d1__default from 'fs';
import { folders } from '../../base-repo/app-constants/project-paths.js';
import { head } from '../../base-repo/app-constants/internal-system-constants.js';
import { eslintLoggerLogError } from '@flint/test-infra/logger';

var __defProp=Object['defineProperty'],__name=(_0x1a1501,_0x1eea85)=>__defProp(_0x1a1501,'name',{'value':_0x1eea85,'configurable':!![]});const constants={'dotGit':folders['dotGit'],'head':head,'refPrefix':'ref:','gitdirPrefix':'gitdir:','utf8':'utf-8'};function readGitHead(_0x54551e,_0x9644e1){let _0x4dcfb4='';try{const _0x14d9a9=findGitDir(_0x54551e);if(_0x14d9a9){const _0x64d419=readHeadContent(_0x14d9a9);if(_0x64d419){_0x4dcfb4=_0x64d419;if(isRefHead(_0x64d419)){const _0x23e160=resolveHeadRef(_0x14d9a9,_0x64d419);_0x23e160&&(_0x4dcfb4=_0x23e160);}}}}catch(_0x358dab){eslintLoggerLogError('['+_0x9644e1+']\x20Error\x20reading\x20git\x20HEAD',_0x358dab instanceof Error?_0x358dab['message']:String(_0x358dab));}return _0x4dcfb4;}__name(readGitHead,'readGitHead');function findGitDir(_0x9021a2){let _0x1b4709=_0x1869d0__default['resolve'](_0x9021a2),_0x36ff22,_0x3c81df=!![];while(_0x3c81df){const _0x4e8b9c=_0x1869d0__default['join'](_0x1b4709,constants['dotGit']),_0x547035=_0x3881d1__default['existsSync'](_0x4e8b9c);if(_0x547035)_0x36ff22=resolveGitDirFromDotGitPath(_0x1b4709,_0x4e8b9c),_0x3c81df=![];else {const _0x39f561=_0x1869d0__default['dirname'](_0x1b4709);_0x39f561===_0x1b4709?_0x3c81df=![]:_0x1b4709=_0x39f561;}}return _0x36ff22;}__name(findGitDir,'findGitDir');function resolveGitDirFromDotGitPath(_0x51ccb6,_0x354c96){let _0x491c20;const _0x5a513d=_0x3881d1__default['statSync'](_0x354c96);if(_0x5a513d['isDirectory']())_0x491c20=_0x354c96;else {const _0x5d4abb=_0x3881d1__default['readFileSync'](_0x354c96,constants['utf8'])['trim']();if(_0x5d4abb['startsWith'](constants['gitdirPrefix'])){const _0x1de840=_0x5d4abb['replace'](constants['gitdirPrefix'],'')['trim']();_0x491c20=_0x1869d0__default['resolve'](_0x51ccb6,_0x1de840);}}return _0x491c20;}__name(resolveGitDirFromDotGitPath,'resolveGitDirFromDotGitPath');function readHeadContent(_0x2c40b4){const _0x54e9e9=_0x1869d0__default['join'](_0x2c40b4,constants['head']);let _0x324d74='';return _0x3881d1__default['existsSync'](_0x54e9e9)&&(_0x324d74=_0x3881d1__default['readFileSync'](_0x54e9e9,constants['utf8'])['trim']()),_0x324d74;}__name(readHeadContent,'readHeadContent');function isRefHead(_0x26ee71){return _0x26ee71['startsWith'](constants['refPrefix']);}__name(isRefHead,'isRefHead');function resolveHeadRef(_0x196f54,_0x5b8e27){let _0xa17468='';const _0x1d1c98=_0x5b8e27['replace'](constants['refPrefix'],'')['trim'](),_0x1abb49=readRefSha(_0x196f54,_0x1d1c98);return _0x1abb49&&(_0xa17468=_0x5b8e27+':'+_0x1abb49),_0xa17468;}__name(resolveHeadRef,'resolveHeadRef');function readRefSha(_0x5645fa,_0x549b70){const _0x3416d3=_0x1869d0__default['join'](_0x5645fa,_0x549b70);let _0x31bed4='';return _0x3881d1__default['existsSync'](_0x3416d3)?_0x31bed4=_0x3881d1__default['readFileSync'](_0x3416d3,constants['utf8'])['trim']():_0x31bed4=readPackedRefSha(_0x5645fa,_0x549b70),_0x31bed4;}__name(readRefSha,'readRefSha');function readPackedRefSha(_0x39aaf2,_0x2b38f9){const _0x44688f=_0x1869d0__default['join'](_0x39aaf2,'packed-refs');let _0x31c7b0='';if(_0x3881d1__default['existsSync'](_0x44688f)){const _0x35d86f=_0x3881d1__default['readFileSync'](_0x44688f,constants['utf8']),_0x5eb353=_0x35d86f['split'](/\r?\n/);for(const _0x1d2c54 of _0x5eb353){const _0x36b95f=!_0x1d2c54||_0x1d2c54['startsWith']('#')||_0x1d2c54['startsWith']('^');if(!_0x36b95f){const [_0x549b75,_0xeaec3e]=_0x1d2c54['trim']()['split']('\x20');if(_0xeaec3e===_0x2b38f9&&_0x549b75){_0x31c7b0=_0x549b75;break;}}}}return _0x31c7b0;}__name(readPackedRefSha,'readPackedRefSha');

export { readGitHead };
