mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Simplify Nostr Clone, require HEAD tag in state event, improve HEAD detection in push
This commit is contained in:
2 files changed
+61
-91
No files matched your search
+11
-38
@@ -1089,7 +1089,7 @@ describe('Git', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should use local HEAD when state event exists but has no HEAD tag', async () => {
|
||||
it('should reject clone when state event exists but has no HEAD tag', async () => {
|
||||
// Create a mock Git HTTP server
|
||||
const mockServer = new MockGitHttpServer();
|
||||
const mockRepo = createMockRepository();
|
||||
@@ -1109,7 +1109,7 @@ describe('Git', () => {
|
||||
sig: 'test-sig',
|
||||
};
|
||||
|
||||
// State event without HEAD tag
|
||||
// State event without HEAD tag (malformed)
|
||||
const stateEvent: NostrEvent = {
|
||||
id: 'state-event-id',
|
||||
pubkey: testPubkey,
|
||||
@@ -1136,42 +1136,15 @@ describe('Git', () => {
|
||||
fetch: mockServer.fetch,
|
||||
});
|
||||
|
||||
// Clone the repository
|
||||
await gitWithMock.clone({
|
||||
url: `nostr://${testNpub}/no-head-repo`,
|
||||
dir: '/no-head-repo',
|
||||
singleBranch: true,
|
||||
depth: 1,
|
||||
});
|
||||
|
||||
// Make a local change
|
||||
await fs.mkdir('/no-head-repo/.shakespeare');
|
||||
await fs.writeFile('/no-head-repo/.shakespeare/git.json', JSON.stringify({ name: 'Test', email: '[email protected]' }));
|
||||
await fs.writeFile('/no-head-repo/test.txt', 'test');
|
||||
await gitWithMock.add({ dir: '/no-head-repo', filepath: 'test.txt' });
|
||||
await gitWithMock.commit({ dir: '/no-head-repo', message: 'Test' });
|
||||
|
||||
// Clear the mock to track new calls
|
||||
vi.mocked(nostr.event).mockClear();
|
||||
|
||||
// Push to Nostr
|
||||
try {
|
||||
await gitWithMock.push({ dir: '/no-head-repo', remote: 'origin' });
|
||||
} catch {
|
||||
// Expected to fail on actual git push
|
||||
}
|
||||
|
||||
// Find the published state event
|
||||
const eventCalls = vi.mocked(nostr.event).mock.calls;
|
||||
const publishedStateEvent = eventCalls.find(([event]) => event.kind === 30618)?.[0];
|
||||
expect(publishedStateEvent).toBeDefined();
|
||||
|
||||
if (publishedStateEvent) {
|
||||
// HEAD should use local HEAD since state event had no HEAD tag
|
||||
const headTag = publishedStateEvent.tags.find(([name]: string[]) => name === 'HEAD');
|
||||
expect(headTag).toBeDefined();
|
||||
expect(headTag![1]).toBe('ref: refs/heads/main');
|
||||
}
|
||||
// Attempt to clone the repository should fail
|
||||
await expect(
|
||||
gitWithMock.clone({
|
||||
url: `nostr://${testNpub}/no-head-repo`,
|
||||
dir: '/no-head-repo',
|
||||
singleBranch: true,
|
||||
depth: 1,
|
||||
}),
|
||||
).rejects.toThrow('Repository HEAD not found in state event');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+50
-53
@@ -685,78 +685,70 @@ export class Git {
|
||||
throw new Error('Repository state not found on Nostr network');
|
||||
}
|
||||
|
||||
const cloneUrls = repo.tags.find(([name]) => name === 'clone')?.slice(1) ?? [];
|
||||
|
||||
if (cloneUrls.length === 0) {
|
||||
throw new Error('No clone URLs found in repository announcement');
|
||||
const HEAD = state.tags.find(([name]) => name === 'HEAD')?.[1];
|
||||
if (!HEAD) {
|
||||
throw new Error('Repository HEAD not found in state event');
|
||||
}
|
||||
|
||||
// Try cloning in order until one succeeds
|
||||
let cloneSucceeded = false;
|
||||
for (const url of cloneUrls) {
|
||||
try {
|
||||
await Promise.race([
|
||||
new Promise((_, reject) => setTimeout(() => reject(new Error('Clone from Nostr clone URL timed out')), 10_000)),
|
||||
git.clone({
|
||||
...options,
|
||||
fs: this.fs,
|
||||
http: this.httpNoProxy,
|
||||
url,
|
||||
}),
|
||||
]);
|
||||
cloneSucceeded = true;
|
||||
break;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Determine if HEAD is symbolic or direct
|
||||
const headIsSymbolic = HEAD.startsWith('ref: ');
|
||||
const headRef = headIsSymbolic ? HEAD.substring(5) : HEAD;
|
||||
|
||||
if (!cloneSucceeded) {
|
||||
throw new Error('Failed to clone from any of the provided clone URLs');
|
||||
}
|
||||
// Initialize a new Git repository
|
||||
await git.init({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
defaultBranch: 'main',
|
||||
});
|
||||
|
||||
// Update the remote URL to point to the Nostr URL instead of the HTTP clone URL
|
||||
// Set the remote to the Nostr URI
|
||||
const remote = options.remote || 'origin';
|
||||
await this.setRemoteURL({
|
||||
await git.addRemote({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
remote,
|
||||
url: nostrURI.toString(),
|
||||
});
|
||||
|
||||
// Fetch from all clone URLs to update remote tracking branches
|
||||
// Fetch from clone URLs to get the repository objects
|
||||
await this.nostrFetch(nostrURI, {
|
||||
...options,
|
||||
remote,
|
||||
});
|
||||
|
||||
// The NIP-34 state event is the source of truth for repository state
|
||||
// Set local refs to match the state event
|
||||
for (const [name, val] of state.tags) {
|
||||
if (name === 'HEAD') {
|
||||
// Handle HEAD specially - it can be symbolic or direct
|
||||
const symbolic = val.startsWith('ref: ');
|
||||
const value = symbolic ? val.substring(5) : val;
|
||||
await git.writeRef({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
ref: 'HEAD',
|
||||
value,
|
||||
symbolic,
|
||||
force: true,
|
||||
});
|
||||
} else if (name.startsWith('refs/heads/')) {
|
||||
// Set local branch refs to match the state event
|
||||
for (const [name, value] of state.tags) {
|
||||
if (name.startsWith('refs/heads/')) {
|
||||
// Set local branch refs to match state event
|
||||
await git.writeRef({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
ref: name,
|
||||
value: val,
|
||||
value,
|
||||
force: true,
|
||||
});
|
||||
}
|
||||
// Note: We don't set refs/tags/ here as those are typically immutable
|
||||
// and should come from the clone/fetch operations
|
||||
}
|
||||
|
||||
// Checkout working directory if requested (extracts files from objects)
|
||||
if (!options.noCheckout) {
|
||||
await git.checkout({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
ref: headRef,
|
||||
});
|
||||
}
|
||||
|
||||
// Set HEAD after checkout (checkout modifies HEAD, setting it to a commit SHA)
|
||||
await git.writeRef({
|
||||
fs: this.fs,
|
||||
dir: options.dir,
|
||||
ref: 'HEAD',
|
||||
value: headRef,
|
||||
symbolic: headIsSymbolic,
|
||||
force: true,
|
||||
});
|
||||
}
|
||||
|
||||
async setRemoteURL(options: { dir: string; remote: string; url: string }): Promise<void> {
|
||||
@@ -919,17 +911,22 @@ export class Git {
|
||||
stateTags.push(existingHeadTag);
|
||||
} else {
|
||||
// No existing HEAD, use local HEAD
|
||||
const headRef = await git.resolveRef({
|
||||
// Check if HEAD is symbolic (points to a branch) by checking currentBranch
|
||||
const currentBranch = await git.currentBranch({
|
||||
fs: this.fs,
|
||||
dir,
|
||||
ref: 'HEAD',
|
||||
depth: 2,
|
||||
});
|
||||
|
||||
// Check if HEAD is a symbolic ref
|
||||
if (headRef.startsWith('refs/')) {
|
||||
stateTags.push(['HEAD', `ref: ${headRef}`]);
|
||||
if (currentBranch) {
|
||||
// Symbolic ref - HEAD points to a branch
|
||||
stateTags.push(['HEAD', `ref: refs/heads/${currentBranch}`]);
|
||||
} else {
|
||||
// Detached HEAD - use the commit SHA
|
||||
const headRef = await git.resolveRef({
|
||||
fs: this.fs,
|
||||
dir,
|
||||
ref: 'HEAD',
|
||||
});
|
||||
stateTags.push(['HEAD', headRef]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user