198 lines
10 KiB
JavaScript
198 lines
10 KiB
JavaScript
// The version control behind the Commit window, the log, annotate and the diff views (git.js): a provider with the same
|
|
// method names for Git (git.* commands) and Subversion (svn.* commands, ide/src/Svn). Every result comes in Git's shape
|
|
// (status files, log entries, blame lines), so the windows draw both the same way. project.info's capabilities say
|
|
// which: vcs 'git' | 'svn' | null.
|
|
import { backend } from './backend.js';
|
|
import { el, notify, confirm, chooser } from './ui.js';
|
|
import { openVcsSetup, openRemotes } from './vcs-setup.js';
|
|
|
|
export function vcsProvider(caps) {
|
|
return caps.vcs === 'svn' && !caps.git ? new SvnProvider() : new GitProvider(caps);
|
|
}
|
|
|
|
/** Git: the calls git.js always made. */
|
|
export class GitProvider {
|
|
constructor(caps) {
|
|
this.kind = 'git';
|
|
this.label = 'Git';
|
|
this.caps = caps;
|
|
this.output = 'git.output';
|
|
this.baseName = caps.gitMode === 'drafts' ? 'published' : 'HEAD';
|
|
}
|
|
status() { return backend.invoke('git.status'); }
|
|
async original(path, revision) { return (await backend.invoke('git.original', revision ? { path, revision } : { path })).content; }
|
|
commit({ message, paths, push, amend }) { return backend.invoke('git.commit', { message, paths, push, amend }); }
|
|
rollback(paths) { return backend.invoke('git.rollback', { paths }); }
|
|
stage(paths) { return backend.invoke('git.stage', { paths }); }
|
|
log(path, limit) { return backend.invoke('git.log', { path, limit }); }
|
|
commitFiles(c) { return backend.invoke('git.commitFiles', { sha: c.sha }); }
|
|
async commitDiff(c, f) {
|
|
const parent = c.parents[0];
|
|
const [before, after] = await Promise.all([
|
|
parent && f.status !== 'A' ? this.original(f.from || f.path, parent) : '',
|
|
f.status !== 'D' ? this.original(f.path, c.sha) : '',
|
|
]);
|
|
return { before, after };
|
|
}
|
|
blame(path, content) { return backend.invoke('git.blame', { path, content }); }
|
|
}
|
|
|
|
/** Subversion: svn.* answers turned into Git's shapes (a revision is a "commit" whose sha is r<N>). */
|
|
export class SvnProvider {
|
|
constructor() {
|
|
this.kind = 'svn';
|
|
this.label = 'SVN';
|
|
this.output = 'svn.output';
|
|
this.baseName = 'BASE';
|
|
}
|
|
status() { return backend.invoke('svn.status'); }
|
|
async original(path, revision) { return (await backend.invoke('svn.original', revision ? { path, revision } : { path })).content; }
|
|
async commit({ message, paths }) {
|
|
const r = await backend.invoke('svn.commit', { message, paths });
|
|
return { sha: r.sha, revision: r.revision, pushed: false };
|
|
}
|
|
rollback(paths) { return backend.invoke('svn.revert', { paths }); }
|
|
stage(paths) { return backend.invoke('svn.add', { paths }); }
|
|
async log(path, limit) {
|
|
const entries = await backend.invoke('svn.log', { path, limit });
|
|
return entries.map((e) => ({ sha: 'r' + e.revision, short: 'r' + e.revision, revision: e.revision, author: e.author, email: '', time: e.time,
|
|
subject: (e.message || '').split('\n')[0], message: e.message, refs: '', parents: e.revision > 1 ? ['r' + (e.revision - 1)] : [], paths: e.paths }));
|
|
}
|
|
commitFiles(c) {
|
|
return (c.paths || []).filter((p) => p.kind !== 'dir').map((p) => ({ status: p.action === 'R' ? 'M' : p.action, path: p.path, from: p.from || null, fromRevision: p.fromRevision }));
|
|
}
|
|
async commitDiff(c, f) {
|
|
const [before, after] = await Promise.all([
|
|
f.status === 'A' && !f.from ? '' : this.original(f.from || f.path, String(f.from ? f.fromRevision : c.revision - 1)),
|
|
f.status === 'D' ? '' : this.original(f.path, String(c.revision)),
|
|
]);
|
|
return { before, after };
|
|
}
|
|
async blame(path) {
|
|
const lines = await backend.invoke('svn.blame', { path });
|
|
return lines.map((l) => ({ line: l.line, sha: l.revision ? 'r' + l.revision : '0000000', author: l.author, time: l.time, summary: l.summary }));
|
|
}
|
|
}
|
|
|
|
/** The Subversion parts of git.js's Git object: update, conflicts, branches (switch). */
|
|
export const svnUi = {
|
|
async svnUpdate() {
|
|
await this.app.editors.saveAll(true);
|
|
const task = this.app.task?.('Updating the project (svn update)…', { open: () => this.app.showTool('commit') });
|
|
try {
|
|
const r = await backend.invoke('svn.update', {});
|
|
await this.app.editors.reloadFromDisk();
|
|
this.app.tree?.refresh();
|
|
if (r.conflicts?.length) {
|
|
notify(`${r.conflicts.length} file${r.conflicts.length > 1 ? 's' : ''} in conflict: resolve them in the Commit window.`, 'warning', { title: 'SVN update' });
|
|
this.app.showTool('commit');
|
|
} else notify(r.revision ? `Updated to revision ${r.revision}` : 'Project updated', 'success', { title: 'SVN' });
|
|
} catch (e) {
|
|
notify(e.message, 'error', { title: 'SVN update failed' });
|
|
} finally {
|
|
task?.();
|
|
await this.refresh();
|
|
}
|
|
},
|
|
|
|
/** The files left in conflict by an update: compare, open, keep mine / theirs / the file as edited. */
|
|
svnConflictPanel() {
|
|
const conflicts = (this.status?.files || []).filter((f) => f.conflict);
|
|
if (!conflicts.length) return null;
|
|
const take = (f, accept, label, hint) => el('button.btn', { title: hint, onclick: () => this.svnResolve(f.path, accept) }, label);
|
|
return el('div.merge-box', { style: { padding: '8px', borderBottom: '1px solid var(--border)', background: 'var(--bg-2)' } },
|
|
el('div', { style: { marginBottom: '6px' } }, el('strong', {}, `${conflicts.length} conflict${conflicts.length > 1 ? 's' : ''} after the update`)),
|
|
...conflicts.map((f) => el('div.merge-file', { style: { margin: '4px 0 8px' }, title: f.path },
|
|
el('div.row.st-conflict', { style: { paddingLeft: '6px' }, ondblclick: () => this.app.editors.open(f.path) }, el('span.label', {}, f.path)),
|
|
el('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '4px', paddingLeft: '28px' } },
|
|
el('button.btn', { title: 'Edit the file with its <<<<<<< / >>>>>>> markers, then mark it resolved', onclick: () => this.app.editors.open(f.path) }, 'Open'),
|
|
take(f, 'mine-full', 'Mine', 'Keep your version of the whole file'),
|
|
take(f, 'theirs-full', 'Theirs', 'Take the repository\'s version of the whole file'),
|
|
take(f, 'working', 'Resolved', 'The file as it is now (its markers edited away)')))));
|
|
},
|
|
|
|
async svnResolve(path, accept) {
|
|
await this.app.editors.saveAll(true);
|
|
try {
|
|
await backend.invoke('svn.resolve', { path, accept });
|
|
if (accept !== 'working') await this.app.editors.reloadFromDisk();
|
|
} catch (e) { notify(e.message, 'error', { title: 'Not resolved' }); }
|
|
this.refresh();
|
|
},
|
|
|
|
async svnBranchPopup(at) {
|
|
const branches = await backend.invoke('svn.branches').catch(() => []);
|
|
const items = [
|
|
{ label: 'Update Project', icon: 'pull', hint: 'Ctrl+T', action: () => this.pull(), group: 'Actions' },
|
|
{ label: 'Commit…', icon: 'commit', hint: 'Ctrl+K', action: () => this.app.showTool('commit'), group: 'Actions' },
|
|
{ label: 'Show Log', icon: 'log', action: () => this.app.toggleTool('git'), group: 'Actions' },
|
|
{ label: 'Remotes and hosting…', icon: 'globe', action: () => openRemotes(this.app), group: 'Actions' },
|
|
{ label: 'Version control…', icon: 'branch', action: () => openVcsSetup(this.app), group: 'Actions' },
|
|
...branches.map((b) => ({ label: (b.current ? '★ ' : '') + b.name, icon: 'branch', group: 'Branches', action: () => this.svnSwitch(b) })),
|
|
];
|
|
chooser(items, { at, placeholder: 'Search for branches and actions', width: 380 });
|
|
},
|
|
|
|
async svnSwitch(branch) {
|
|
if (branch.current) return;
|
|
if (!(await confirm('Switch', `Switch the working copy to ${branch.name} (svn switch)? Your local changes are kept and merged.`, { ok: 'Switch' }))) return;
|
|
await this.app.editors.saveAll(true);
|
|
const task = this.app.task?.(`Switching to ${branch.name} (svn switch)…`);
|
|
try {
|
|
const r = await backend.invoke('svn.switch', { branch: branch.name });
|
|
await this.app.editors.reloadFromDisk();
|
|
this.app.tree?.refresh();
|
|
notify(r.conflicts?.length ? `On ${branch.name}, ${r.conflicts.length} file(s) in conflict` : `On ${branch.name}`, r.conflicts?.length ? 'warning' : 'success', { title: 'SVN' });
|
|
} catch (e) {
|
|
notify(e.message, 'error', { title: 'Switch failed' });
|
|
} finally {
|
|
task?.();
|
|
await this.refresh();
|
|
}
|
|
},
|
|
};
|
|
|
|
/** The VCS menu's actions of an SVN working copy (app.js, where Git adds its own). */
|
|
export function svnActions(app, a) {
|
|
a.add('commit', 'Commit…', () => app.showTool('commit'), { kbd: 'Ctrl+K', icon: 'commit' });
|
|
a.add('commitTool', 'Commit Tool Window', () => app.toggleTool('commit'), { kbd: 'Alt+0', icon: 'commit' });
|
|
a.add('commitAll', 'Commit All…', () => app.git.commitAll(), { kbd: 'Ctrl+Alt+K', icon: 'upload' });
|
|
a.add('diff', 'Show Diff with BASE', () => app.git.diff(app.editors.activePath()), { kbd: 'Ctrl+D', icon: 'diff', when: () => false });
|
|
a.add('rollbackFile', 'Revert File…', () => { const p = app.editors.activePath(); if (p) app.git.rollback([p]); }, { kbd: 'Ctrl+Alt+Z', icon: 'rollback' });
|
|
}
|
|
|
|
// Claude's tools (agent.js): svn_status / svn_diff / svn_commit / svn_update / svn_log
|
|
export const svnAgentTools = {
|
|
svnOnly() {
|
|
if (this.app.project?.capabilities?.vcs !== 'svn') throw new Error('this project is not a Subversion working copy (no .svn folder)');
|
|
},
|
|
async tool_svn_status() {
|
|
this.svnOnly();
|
|
const s = await backend.invoke('svn.status');
|
|
return { url: s.url, branch: s.branch, revision: s.revision, files: s.files.map((f) => ({ path: f.path, code: f.code, status: f.status })) };
|
|
},
|
|
async tool_svn_diff(args) {
|
|
this.svnOnly();
|
|
return (await backend.invoke('svn.diff', { path: args.path || undefined, revision: args.revision || undefined })).diff || '(no changes)';
|
|
},
|
|
async tool_svn_commit(args) {
|
|
this.svnOnly();
|
|
await this.app.editors.saveAll(true);
|
|
const r = await backend.invoke('svn.commit', { message: String(args.message || ''), paths: Array.isArray(args.paths) && args.paths.length ? args.paths : undefined });
|
|
this.app.git?.refresh();
|
|
return r;
|
|
},
|
|
async tool_svn_update() {
|
|
this.svnOnly();
|
|
await this.app.editors.saveAll(true);
|
|
const r = await backend.invoke('svn.update', {});
|
|
await this.app.editors.reloadFromDisk();
|
|
this.app.tree?.refresh();
|
|
this.app.git?.refresh();
|
|
return { revision: r.revision, conflicts: r.conflicts };
|
|
},
|
|
async tool_svn_log(args) {
|
|
this.svnOnly();
|
|
return backend.invoke('svn.log', { path: args.path || undefined, limit: Math.min(500, Number(args.limit) || 50) });
|
|
},
|
|
};
|