From 43750e2d8a3498df053ffed7d3a95a38ae312f1f Mon Sep 17 00:00:00 2001 From: M_Viper Date: Sun, 1 Mar 2026 10:31:05 +0100 Subject: [PATCH] Update from Git Manager GUI --- src/git/apiHandler.js | 57 +++++++++++++++++++++++++++++-------------- src/git/gitHandler.js | 23 ++++++++++++++--- 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/src/git/apiHandler.js b/src/git/apiHandler.js index 7eaec79..88b9d05 100644 --- a/src/git/apiHandler.js +++ b/src/git/apiHandler.js @@ -178,7 +178,7 @@ async function listGiteaRepos({ token, url }) { * Returns array of items for a directory or single item for file. * Each item includes name, path, type, size, download_url, sha (if present). */ -async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = 'main' }) { +async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = 'HEAD' }) { const base = normalizeBase(url); if (!base) throw new Error('Invalid Gitea base URL'); @@ -188,10 +188,8 @@ async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = repo = parts[1]; } - // FIXED: Verwende den übergebenen ref Parameter statt hardcoded 'master' - // Falls ref explizit 'master' ist, konvertiere zu 'main' - let branchRef = ref || 'main'; - if (branchRef === 'master') branchRef = 'main'; + // HEAD folgt automatisch dem default_branch des Repos (main ODER master) + let branchRef = ref || 'HEAD'; // console.log('=== getGiteaRepoContents DEBUG ==='); // Optional: Stumm geschaltet // console.log('Input ref:', ref, 'Final branchRef:', branchRef, 'Path:', path); @@ -199,6 +197,11 @@ async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = const candidates = []; candidates.push(buildContentsUrl(base, owner, repo, path) + `?ref=${branchRef}`); candidates.push(buildContentsUrl(base, owner, repo, path)); + // Fallback: master und main explizit versuchen + if (branchRef === 'HEAD') { + candidates.push(buildContentsUrl(base, owner, repo, path) + `?ref=main`); + candidates.push(buildContentsUrl(base, owner, repo, path) + `?ref=master`); + } if (path) { candidates.push(`${base}/api/v1/repos/${owner}/${repo}/contents/${path}?ref=${branchRef}`); @@ -211,23 +214,23 @@ async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = if (r.ok) { const payload = r.data; if (Array.isArray(payload)) { - return payload.map(item => ({ + return { ok: true, items: payload.map(item => ({ name: item.name, path: item.path, type: item.type, size: item.size, download_url: item.download_url || item.html_url || null, sha: item.sha || item.commit_id || null - })); + }))}; } else { - return [{ + return { ok: true, items: [{ name: payload.name, path: payload.path, type: payload.type, size: payload.size, download_url: payload.download_url || payload.html_url || null, sha: payload.sha || payload.commit_id || null - }]; + }]}; } } else { lastErr = r; @@ -237,6 +240,20 @@ async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = } } + // Alle Kandidaten fehlgeschlagen — prüfen ob Repo leer ist (kein Commit) + if (lastErr && lastErr.status === 404) { + const repoInfoUrl = `${base}/api/v1/repos/${owner}/${repo}`; + const repoInfo = await tryRequest(repoInfoUrl, token); + if (repoInfo.ok && repoInfo.data.empty) { + // Repo existiert, ist aber leer + return { ok: true, items: [], empty: true }; + } + if (repoInfo.ok && !repoInfo.data.empty) { + // Repo existiert und hat Commits, aber Pfad nicht gefunden + return { ok: true, items: [] }; + } + } + const msg = lastErr ? `Failed (${lastErr.status || 'no-status'}) ${lastErr.url}` : 'Unknown error'; const err = new Error('getGiteaRepoContents failed: ' + msg); err.detail = lastErr; @@ -246,7 +263,7 @@ async function getGiteaRepoContents({ token, url, owner, repo, path = '', ref = /** * Get file content (decoded) from Gitea repo. */ -async function getGiteaFileContent({ token, url, owner, repo, path, ref = 'main' }) { +async function getGiteaFileContent({ token, url, owner, repo, path, ref = 'HEAD' }) { const base = normalizeBase(url); if (!base) throw new Error('Invalid Gitea base URL'); @@ -256,13 +273,17 @@ async function getGiteaFileContent({ token, url, owner, repo, path, ref = 'main' repo = parts[1]; } - let branchRef = ref || 'main'; - if (branchRef === 'master') branchRef = 'main'; + let branchRef = ref || 'HEAD'; const candidates = []; candidates.push(buildContentsUrl(base, owner, repo, path) + `?ref=${branchRef}`); candidates.push(buildContentsUrl(base, owner, repo, path)); + // Fallback: raw API + main/master candidates.push(`${base}/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(path)}?ref=${branchRef}`); + if (branchRef === 'HEAD') { + candidates.push(`${base}/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(path)}?ref=main`); + candidates.push(`${base}/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(path)}?ref=master`); + } candidates.push(`${base}/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(path)}`); let lastErr = null; @@ -299,7 +320,7 @@ async function getGiteaFileContent({ token, url, owner, repo, path, ref = 'main' * Upload (create or update) a file to Gitea. * Implementiert Retry-Logik für Server-Caching-Probleme (404 beim Lesen, 422 beim Schreiben). */ -async function uploadGiteaFile({ token, url, owner, repo, path, contentBase64, message = 'Upload via Git Manager GUI', branch = 'main' }) { +async function uploadGiteaFile({ token, url, owner, repo, path, contentBase64, message = 'Upload via Git Manager GUI', branch = 'HEAD' }) { const base = normalizeBase(url); if (!base) throw new Error('Invalid Gitea base URL'); if (!owner && repo && repo.includes('/')) { @@ -308,8 +329,8 @@ async function uploadGiteaFile({ token, url, owner, repo, path, contentBase64, m repo = parts[1]; } - let branchName = branch || 'main'; - if (branchName === 'master') branchName = 'main'; + // Behalte den branch so wie übergeben - keine Konvertierung + let branchName = branch || 'HEAD'; const fetchSha = async () => { try { @@ -450,7 +471,7 @@ async function uploadGiteaFile({ token, url, owner, repo, path, contentBase64, m /** * Get commit history from Gitea repository */ -async function getGiteaCommits({ token, url, owner, repo, branch = 'main', page = 1, limit = 50 }) { +async function getGiteaCommits({ token, url, owner, repo, branch = 'HEAD', page = 1, limit = 50 }) { const base = normalizeBase(url); if (!base) throw new Error('Invalid Gitea base URL'); @@ -552,7 +573,7 @@ async function getGiteaCommitFiles({ token, url, owner, repo, sha }) { /** * Search commits in Gitea repository */ -async function searchGiteaCommits({ token, url, owner, repo, query, branch = 'main' }) { +async function searchGiteaCommits({ token, url, owner, repo, query, branch = 'HEAD' }) { const base = normalizeBase(url); if (!base) throw new Error('Invalid Gitea base URL'); @@ -662,7 +683,7 @@ async function createGiteaRelease({ token, url, owner, repo, data }) { body: data.body || '', draft: data.draft || false, prerelease: data.prerelease || false, - target_commitish: data.target_commitish || 'main' + target_commitish: data.target_commitish || 'HEAD' }; try { diff --git a/src/git/gitHandler.js b/src/git/gitHandler.js index 2d32437..d478b99 100644 --- a/src/git/gitHandler.js +++ b/src/git/gitHandler.js @@ -15,9 +15,15 @@ async function initRepo(folderPath) { return true; } -async function commitAndPush(folderPath, branch = 'master', message = 'Update from Git Manager GUI', progressCb = null) { +async function commitAndPush(folderPath, branch = null, message = 'Update from Git Manager GUI', progressCb = null) { const git = gitFor(folderPath); + // Branch auto-detect: falls nicht angegeben oder 'HEAD', aktuellen Branch ermitteln + if (!branch || branch === 'HEAD') { + const summary = await git.branchLocal(); + branch = summary.current || 'main'; + } + await git.add('./*'); try { @@ -44,8 +50,19 @@ async function commitAndPush(folderPath, branch = 'master', message = 'Update fr async function getBranches(folderPath) { const git = gitFor(folderPath); - const summary = await git.branchLocal(); - return summary.all; + try { + // Alle Branches: lokal + remote (origin/main, origin/master usw.) + const summary = await git.branch(['-a']); + const all = summary.all + .map(b => b.replace(/^remotes\/origin\//, '').trim()) + .filter(b => !b.startsWith('HEAD')) + .filter((b, i, arr) => arr.indexOf(b) === i); // deduplizieren + return all; + } catch (e) { + // Fallback: nur lokale Branches + const local = await git.branchLocal(); + return local.all; + } } async function getCommitLogs(folderPath, count = 50) {