Skip to content

Commit

Permalink
feat(cli): Create the git-annex branch if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
nellh committed May 23, 2024
1 parent c757e0b commit 6b4e879
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
14 changes: 11 additions & 3 deletions cli/src/worker/git.test.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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 (
Expand All @@ -123,5 +131,5 @@ LICENSE annex.largefiles=nothing`),
assertArrayIncludes(expectedFiles, [relativePath])
}
}
assertEquals(gitObjects, 9)
assertEquals(gitObjects, 10)
})
58 changes: 52 additions & 6 deletions cli/src/worker/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,65 @@ async function add(event: GitWorkerEventAdd) {
}
}

/**
* Create the empty git-annex branch if needed

Check warning on line 151 in cli/src/worker/git.ts

View check run for this annotation

Codecov / codecov/patch

cli/src/worker/git.ts#L151

Added line #L151 was not covered by tests
*/
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
*/
async function commitAnnexBranch(annexKeys: Record<string, string>) {
// 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(" ")
Expand Down Expand Up @@ -234,7 +280,7 @@ async function commitAnnexBranch(annexKeys: Record<string, string>) {
...context.config(),
ref: "main",
})
} catch (err) {
} catch (_err) {
// Fallback to master and error if neither exists
await git.checkout({
...context.config(),
Expand Down

0 comments on commit 6b4e879

Please sign in to comment.