1- import type { GitWorkerEventAdd } from "./types/git-context.ts"
1+ import type {
2+ GitWorkerEventAdd ,
3+ GitWorkerEventClone ,
4+ GitWorkerEventRemoteSetup ,
5+ } from "./types/git-context.ts"
26import type { GitAnnexAttributes , GitAnnexBackend } from "../gitattributes.ts"
37import { matchGitAttributes , parseGitAttributes } from "../gitattributes.ts"
48import { dirname , join } from "@std/path"
@@ -28,11 +32,12 @@ async function done() {
2832/**
2933 * Clone or fetch the draft
3034 */
31- async function update ( ) {
35+ async function update ( event : GitWorkerEventClone ) {
36+ const version = event . data ?. version
3237 try {
3338 await context . fs . promises . access ( join ( context . repoPath , ".git" ) )
3439 logger . info (
35- `Fetching ${ context . datasetId } draft from "${ context . repoEndpoint } "` ,
40+ `Fetching ${ context . datasetId } from "${ context . repoEndpoint } "` ,
3641 )
3742 // Reset first to be sure that an interrupted run didn't create untracked files
3843 await resetWorktree ( context , await getDefault ( context ) )
@@ -49,19 +54,26 @@ async function update() {
4954 theirs : "origin/git-annex" ,
5055 } ,
5156 )
52- logger . info ( "Checkout latest HEAD." )
5357 // Make sure the default branch checkout is updated
5458 const defaultBranch = await getDefault ( context )
59+ const ref = version || defaultBranch
60+ logger . info ( `Checking out ${ ref } branch.` )
5561 await git . checkout ( {
5662 ...context . config ( ) ,
57- ref : defaultBranch , // Checkout main
63+ ref, // Checkout main
5864 noUpdateHead : false , // Make sure we update the local branch HEAD
5965 } )
6066 } catch ( _err ) {
6167 logger . info (
62- `Cloning ${ context . datasetId } draft from "${ context . repoEndpoint } "` ,
68+ `Cloning ${ context . datasetId } from "${ context . repoEndpoint } "` ,
6369 )
70+ // Get the default branch
6471 await git . clone ( context . config ( ) )
72+ // Checkout a specific version if needed
73+ if ( version ) {
74+ logger . info ( `Checking out ${ version } .` )
75+ await git . checkout ( { ...context . config ( ) , ref : version } )
76+ }
6577 await git . fetch ( { ...context . config ( ) , ref : "git-annex" } )
6678 try {
6779 await git . branch ( {
@@ -232,14 +244,14 @@ async function createAnnexBranch() {
232244/**
233245 * Generate a commit for remote.log updates if needed
234246 */
235- async function remoteSetup ( ) {
236- await commitAnnexBranch ( )
247+ async function remoteSetup ( event : GitWorkerEventRemoteSetup ) {
248+ await commitAnnexBranch ( event . data ?. version )
237249}
238250
239251/**
240252 * Generate one commit for all pending git-annex branch changes
241253 */
242- async function commitAnnexBranch ( ) {
254+ async function commitAnnexBranch ( version ?: string ) {
243255 // Find the UUID of this repository if it exists already
244256 const expectedRemote = "OpenNeuro" // TODO - This could be more flexible?
245257 let uuid
@@ -347,7 +359,7 @@ async function commitAnnexBranch() {
347359 }
348360 }
349361 } finally {
350- const defaultBranch = await getDefault ( context )
362+ const defaultBranch = version || await getDefault ( context )
351363 await git . checkout ( {
352364 ...context . config ( ) ,
353365 ref : defaultBranch ,
@@ -556,7 +568,7 @@ self.onmessage = (event: GitWorkerEvent) => {
556568 )
557569 setupLogging ( event . data . logLevel )
558570 } else if ( event . data . command === "clone" ) {
559- workQueue . enqueue ( update )
571+ workQueue . enqueue ( update , event )
560572 } else if ( event . data . command === "clearWorktree" ) {
561573 workQueue . enqueue ( clearWorktree )
562574 } else if ( event . data . command === "add" ) {
@@ -566,7 +578,7 @@ self.onmessage = (event: GitWorkerEvent) => {
566578 } else if ( event . data . command === "push" ) {
567579 workQueue . enqueue ( push )
568580 } else if ( event . data . command === "remote-setup" ) {
569- workQueue . enqueue ( remoteSetup )
581+ workQueue . enqueue ( remoteSetup , event )
570582 } else if ( event . data . command === "done" ) {
571583 workQueue . enqueue ( done )
572584 }
0 commit comments