Skip to content

Commit 876778f

Browse files
authored
Merge pull request #3569 from OpenNeuroOrg/cli/annex-filename-fix
fix(cli): Support annex keys with all valid filename characters
2 parents 25b4330 + 7f3ace0 commit 876778f

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

cli/src/worker/annex.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { assertEquals } from "@std/assert/equals"
22
import { join } from "@std/path/join"
3-
import { annexRelativePath, hashDirLower, hashDirMixed } from "./annex.ts"
3+
import {
4+
annexKeyRegex,
5+
annexRelativePath,
6+
hashDirLower,
7+
hashDirMixed,
8+
} from "./annex.ts"
49

510
Deno.test("annexRelativePath() returns appropriate paths", () => {
611
assertEquals(
@@ -26,3 +31,28 @@ Deno.test("hashDirMixed() returns the correct key prefix", async () => {
2631
["Xk", "Mx"],
2732
)
2833
})
34+
35+
Deno.test("annexKeyRegex matches valid keys", () => {
36+
// Typical key
37+
assertEquals(
38+
"SHA256E-s311112--c3527d7944a9619afb57863a34e6af7ec3fe4f108e56c860d9e700699ff806fb.nii.gz"
39+
.match(annexKeyRegex)?.slice(1, 5),
40+
[
41+
"SHA256E",
42+
"311112",
43+
"c3527d7944a9619afb57863a34e6af7ec3fe4f108e56c860d9e700699ff806fb",
44+
".nii.gz",
45+
],
46+
)
47+
// Filenames with other characters
48+
assertEquals(
49+
"SHA256E-s6148--e53302cf7b8beb7c5b908c070618bb11e2590719465d0869522716ecc2cfecd8.DS_Store"
50+
.match(annexKeyRegex)?.slice(1, 5),
51+
[
52+
"SHA256E",
53+
"6148",
54+
"e53302cf7b8beb7c5b908c070618bb11e2590719465d0869522716ecc2cfecd8",
55+
".DS_Store",
56+
],
57+
)
58+
})

cli/src/worker/annex.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ const textDecoder = new TextDecoder("utf-8")
2323
const computeHashMD5 = await createMD5()
2424
const computeHashSHA256 = await createSHA256()
2525

26+
/**
27+
* Matches valid git-annex keys
28+
* Example input: SHA256E-s311112--c3527d7944a9619afb57863a34e6af7ec3fe4f108e56c860d9e700699ff806fb.nii.gz
29+
* Group 1: SHA256E
30+
* Group 2: 311112
31+
* Group 3: c3527d7944a9619afb57863a34e6af7ec3fe4f108e56c860d9e700699ff806fb
32+
* Group 4: .nii.gz
33+
*/
34+
export const annexKeyRegex = /^([A-Z0-9]+)-s(\d+)--([0-9a-fA-F]+)(\.[\w\-. ]*)?/
35+
2636
/**
2737
* git-annex hashDirLower implementation based on https://git-annex.branchable.com/internals/hashing/
2838
* Compute the directory path from a git-annex filename
@@ -196,7 +206,7 @@ export async function getAnnexKeys(
196206
// Check that annexKey conforms to the git-annex key format
197207
// Other symlinks are allowed but may be rejected on push if they point outside of the repo
198208
if (
199-
annexKey.match(/^[A-Z0-9]+-s\d+--[0-9a-fA-F]+(\.[a-zA-Z0-9.]*)?$/)
209+
annexKey.match(annexKeyRegex)
200210
) {
201211
logger.info(`Found key "${annexKey}" in HEAD.`)
202212
annexKeys[annexKey] = filepath

0 commit comments

Comments
 (0)