Elffuss BraidHub

utopia-ia/elffuss-code public

gitInfo() returns the commit SHA and a credential-free link to the remote

gitInfo() parsed .git/logs/HEAD but threw away the new SHA (the second
field), so nothing could show or link the last commit, and `git log` in the
terminal printed "commit <branch>". It now returns lastCommit.sha (the
author's e-mail is still never kept) and `web`, built by the new
remoteWeb() from the origin URL in .git/config: https and ssh remotes become
https://<host>/<org>/<repo> without any user:token, with branch and commit
links for GitHub, GitLab, Bitbucket and Gitea (any other host, as
git.elffuss.com). `git log` prints the real SHA with the branch beside it.

The menu that links them comes in the next commit.
Changeset21d6a5c0c0992dd9388ecc39733128bafa59379d16236fbfe6a8c978d1ddf568
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-10 16:38 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-11 14:15 +02:00
Parentse7ee0f72085f

Browse the files at this changeset

2 files changed

modified web/js/shell.js +2 −1
@@ -278,7 +278,8 @@
278278 if (!info.isRepo) return DIM('no es un repositorio git (no hay .git)');
279279 if (sub === 'branch' || !sub) return '* ' + GRN(info.branch);
280280 if (sub === 'status') return `En la rama ${GRN(info.branch)}\n` + (info.lastCommit ? DIM(`último commit: ${info.lastCommit.msg}`) : DIM('sin commits todavía')) + '\n' + DIM('(git de solo lectura aquí; commit/push reales no disponibles todavía)');
281- if (sub === 'log') return info.lastCommit ? `${YEL('commit ' + info.branch)}\nAutor: ${info.lastCommit.author}\nFecha: ${new Date(info.lastCommit.when).toLocaleString()}\n\n ${info.lastCommit.msg}` : DIM('sin commits');
281+ // El SHA de verdad (antes ponía «commit <rama>», que no es un commit).
282+ if (sub === 'log') return info.lastCommit ? `${YEL('commit ' + (info.lastCommit.sha || '?'))} ${DIM('(' + info.branch + ')')}\nAutor: ${info.lastCommit.author}\nFecha: ${new Date(info.lastCommit.when).toLocaleString()}\n\n ${info.lastCommit.msg}` : DIM('sin commits');
282283 return DIM(`git ${sub}: de solo lectura aquí (branch/status/log).`);
283284 },
284285 };
modified web/js/tools/code.js +35 −5
@@ -19,26 +19,56 @@
1919 return (await (await dir.getFileHandle(name)).getFile()).text();
2020 }
2121
22-// Integración git SIN dependencias: parsea el .git directamente (rama + último
23-// commit). Suficiente para orientar; commitear se le pide al agente/terminal.
22+// Integración git SIN dependencias: parsea el .git directamente (rama, último
23+// commit con su SHA y el remoto «origin»). Suficiente para orientar; commitear
24+// se le pide al agente/terminal.
2425 export async function gitInfo() {
2526 if (!projectHandle) return { isRepo: false };
2627 let git;
2728 try { git = await projectHandle.getDirectoryHandle('.git'); }
2829 catch { return { isRepo: false }; }
29- const out = { isRepo: true, branch: '(detached)', lastCommit: null };
30+ const out = { isRepo: true, branch: '(detached)', lastCommit: null, web: null };
3031 try {
3132 const head = (await readIn(git, 'HEAD')).trim();
3233 out.branch = head.match(/ref:\s*refs\/heads\/(.+)/)?.[1] || head.slice(0, 7);
3334 } catch { /* sin HEAD */ }
3435 try {
36+ // «<viejo> <nuevo> Autor <correo> <ts> <tz>\t<mensaje>»: el SHA del commit
37+ // es el SEGUNDO campo (antes se tiraba). El correo no se guarda: no se enseña.
3538 const last = (await readIn(git, 'logs/HEAD')).trim().split('\n').pop();
36- const m = last.match(/^\S+ \S+ (.+?) <[^>]*> (\d+)[^\t]*\t(.+)$/);
37- if (m) out.lastCommit = { author: m[1], when: new Date(+m[2] * 1000), msg: m[3] };
39+ const m = last.match(/^\S+ (\S+) (.+?) <[^>]*> (\d+)[^\t]*\t(.+)$/);
40+ if (m) out.lastCommit = { sha: /^[0-9a-f]{7,64}$/i.test(m[1]) ? m[1] : null, author: m[2], when: new Date(+m[3] * 1000), msg: m[4] };
3841 } catch { /* sin logs (repo recién creado) */ }
42+ try { out.web = remoteWeb(await readIn(git, 'config')); } catch { /* sin config */ }
3943 return out;
4044 }
4145
46+// La URL web del remoto «origin», SIN credenciales (https://usuario:token@…
47+// no puede acabar en un enlace) y con los enlaces de rama y commit según el
48+// tipo de forja. null si no hay remoto o no es http(s)/ssh reconocible.
49+export function remoteWeb(config) {
50+ const sec = String(config || '').split(/^\s*\[/m).find(b => /^remote\s+"origin"\s*\]/.test(b));
51+ const raw = sec?.match(/^\s*url\s*=\s*(\S+)/m)?.[1];
52+ if (!raw) return null;
53+ let host, path;
54+ const ssh = raw.match(/^(?:ssh:\/\/)?[\w.-]+@([\w.-]+)(?::\d+)?[:/](.+)$/);
55+ if (/^https?:\/\//i.test(raw)) {
56+ try { const u = new URL(raw); host = u.hostname; path = u.pathname; } catch { return null; }
57+ } else if (ssh) { host = ssh[1]; path = ssh[2]; }
58+ else return null;
59+ path = path.replace(/^\/+/, '').replace(/\.git\/?$/, '').replace(/\/+$/, '');
60+ if (!host || !/^[\w.-]+\/[\w.\-/]+$/.test(path)) return null;
61+ const base = `https://${host}/${path}`;
62+ const kind = /(^|\.)github\.com$/i.test(host) ? 'github' : /(^|\.)gitlab\.com$/i.test(host) ? 'gitlab'
63+ : /(^|\.)bitbucket\.org$/i.test(host) ? 'bitbucket' : 'gitea';
64+ const enc = s => String(s).split('/').map(encodeURIComponent).join('/');
65+ return {
66+ base, kind,
67+ branch: b => kind === 'gitea' ? `${base}/src/branch/${enc(b)}` : kind === 'bitbucket' ? `${base}/src/${enc(b)}` : kind === 'gitlab' ? `${base}/-/tree/${enc(b)}` : `${base}/tree/${enc(b)}`,
68+ commit: sha => kind === 'bitbucket' ? `${base}/commits/${sha}` : kind === 'gitlab' ? `${base}/-/commit/${sha}` : `${base}/commit/${sha}`,
69+ };
70+}
71+
4272 // Lista plana de archivos (para el command palette). Cacheada por proyecto.
4373 let fileListCache = null;
4474 export function invalidateFileList() { fileListCache = null; }