diff --git a/cli/src/worker/git.test.ts b/cli/src/worker/git.test.ts index 0c0c7f740..6f3f69384 100644 --- a/cli/src/worker/git.test.ts +++ b/cli/src/worker/git.test.ts @@ -1,4 +1,11 @@ -import { assertArrayIncludes, assertEquals, git, join, walk, SEPARATOR } from "../deps.ts" +import { + assertArrayIncludes, + assertEquals, + git, + join, + SEPARATOR, + walk, +} from "../deps.ts" import { addGitFiles } from "../commands/upload.ts" import fs from "node:fs" @@ -102,12 +109,13 @@ LICENSE annex.largefiles=nothing`), const expectedFiles = [ join(".git", "refs", "heads", "main"), + join(".git", "refs", "heads", "git-annex"), join(".git", "config"), join(".git", "HEAD"), join(".git", "index"), ".gitattributes", "dataset_description.json", - join("sub-01", "anat", "sub-01_T1w.nii.gz") + join("sub-01", "anat", "sub-01_T1w.nii.gz"), ] let gitObjects = 0 for await ( @@ -123,5 +131,5 @@ LICENSE annex.largefiles=nothing`), assertArrayIncludes(expectedFiles, [relativePath]) } } - assertEquals(gitObjects, 9) + assertEquals(gitObjects, 10) }) diff --git a/cli/src/worker/git.ts b/cli/src/worker/git.ts index 5119f2489..bd13a5828 100644 --- a/cli/src/worker/git.ts +++ b/cli/src/worker/git.ts @@ -147,6 +147,38 @@ async function add(event: GitWorkerEventAdd) { } } +/** + * Create the empty git-annex branch if needed + */ +async function createAnnexBranch() { + const now = new Date() + const timestamp = Math.floor(now.getTime() / 1000) + const timezoneOffset = now.getTimezoneOffset() + const commit = { + message: "[OpenNeuro CLI] branch created", + tree: "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + parent: [], + author: { + ...context.author, + timestamp, + timezoneOffset, + }, + committer: { + ...context.author, + timestamp, + timezoneOffset, + }, + } + const object = await git.writeCommit({ ...context.config(), commit }) + await git.branch({ + ...context.config(), + ref: "git-annex", + checkout: true, + // Commit added above + object, + }) +} + /** * Generate one commit for all pending git-annex branch changes */ @@ -154,12 +186,26 @@ async function commitAnnexBranch(annexKeys: Record) { // Find the UUID of this repository if it exists already const expectedRemote = "OpenNeuro" // TODO - This could be more flexible? let uuid - const uuidLog = await readAnnexPath("uuid.log", context) + let uuidLog = "" try { - await git.checkout({ - ...context.config(), - ref: "git-annex", - }) + uuidLog = await readAnnexPath("uuid.log", context) + } catch (err) { + if (err.name !== "NotFoundError") { + throw err + } + } + try { + try { + await git.checkout({ + ...context.config(), + ref: "git-annex", + }) + } catch (err) { + // Create the branch if it doesn't exist + if (err.name === "NotFoundError") { + await createAnnexBranch() + } + } for (const line of uuidLog.split(/\n/)) { if (line.includes(expectedRemote)) { const endUuid = line.indexOf(" ") @@ -234,7 +280,7 @@ async function commitAnnexBranch(annexKeys: Record) { ...context.config(), ref: "main", }) - } catch (err) { + } catch (_err) { // Fallback to master and error if neither exists await git.checkout({ ...context.config(),