refactor lsRemote.

This commit is contained in:
Redbeanw44602
2025-10-02 12:39:11 +08:00
Unverified
parent 70415a13be
commit a47ca21d88
+28 -16
View File
@@ -3,6 +3,7 @@ import * as io from '@actions/io';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import { Sha, RefDic, Repo } from './interfaces'; import { Sha, RefDic, Repo } from './interfaces';
import { valid as isValidSemver } from 'semver';
function makeOpt(ref: Sha): { cwd: string } { function makeOpt(ref: Sha): { cwd: string } {
return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) }; return { cwd: path.join(os.tmpdir(), `xmake-git-${ref}`) };
@@ -20,24 +21,35 @@ export async function lsRemote(repo: Repo): Promise<RefDic> {
}); });
const data: RefDic = { heads: {}, tags: {}, pull: {} }; const data: RefDic = { heads: {}, tags: {}, pull: {} };
out.split('\n').forEach((line) => { out.split('\n').forEach((line) => {
const [ref, tag] = line.trim().split('\t'); const [ref, path] = line.trim().split('\t') as [Sha, string];
if (ref && tag?.startsWith('refs/')) { if (ref && path?.startsWith('refs/')) {
const tagPath = tag.split('/').splice(1); const tagPath = path.split('/').splice(1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any switch (tagPath[0]) {
let ldata = data as any; // eslint-disable-line @typescript-eslint/no-unsafe-assignment case 'heads':
for (let i = 0; i < tagPath.length - 1; i++) { // refs/heads/copilot/fix-6807
const seg = tagPath[i]; const head = tagPath.slice(1).join('/')
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access data.heads[head] = ref
if (typeof ldata[seg] === 'object') { break
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access case 'pull':
ldata = ldata[seg]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment // refs/pull/11/head
} else { // refs/pull/11/merge
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access const pr = Number(tagPath[1])
ldata = ldata[seg] = {}; if (!data.pull[pr]) {
data.pull[pr] = {} as RefDic['pull'][number]
} }
data.pull[pr][tagPath[2] as 'head' | 'merge'] = ref
break;
case 'tags':
// refs/tags/preview
// refs/tags/v3.0.3
const tag = tagPath.slice(1).join('/')
if (isValidSemver(tag)) {
data.tags[tag] = ref
}
break;
default:
break;
} }
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
ldata[tagPath[tagPath.length - 1]] = ref;
} }
}); });
return data; return data;