@@ -48,12 +48,15 @@ def setup_scratch(
4848 )
4949 for repo in config .repositories :
5050 local_directory = config .root / repo .name
51- ensure_repo (repo .remote_url , local_directory , repo .branch )
51+ ensure_repo (repo .remote_url , local_directory , repo .branch , repo . tag )
5252 scratch_install (local_directory , timeout = install_timeout )
5353
5454
5555def ensure_repo (
56- remote_url : str , local_directory : Path , branch : str | None = None
56+ remote_url : str ,
57+ local_directory : Path ,
58+ branch : str | None = None ,
59+ tag : str | None = None ,
5760) -> None :
5861 """
5962 Ensure that a repository is checked out for use in the scratch area.
@@ -64,34 +67,43 @@ def ensure_repo(
6467 local_directory: Output path for cloning
6568 """
6669
70+ if tag and branch :
71+ raise ValueError (f"Can't use both branch ({ branch !r} ) and tag ({ tag !r} )" )
72+
6773 # Set umask to DLS standard
6874 os .umask (stat .S_IWOTH )
6975
7076 if not local_directory .exists ():
7177 LOGGER .info (f"Cloning { remote_url } " )
7278 repo = Repo .clone_from (remote_url , local_directory )
7379 LOGGER .info (f"Cloned { remote_url } -> { local_directory } " )
80+ if branch :
81+ if not (local := getattr (repo .heads , branch , None )):
82+ origin = repo .remotes [0 ]
83+ origin .fetch ()
84+ LOGGER .info (
85+ "Creating branch '%s' to track remote '%s'" ,
86+ branch ,
87+ origin .refs [branch ],
88+ )
89+ local = repo .create_head (branch , origin .refs [branch ])
90+ local .set_tracking_branch (origin .refs [branch ])
91+
92+ LOGGER .info ("Checking out branch %r" , branch )
93+ local .checkout ()
94+ elif tag :
95+ if tag_obj := getattr (repo .tags , tag , None ):
96+ LOGGER .info ("Checking out tag %r" , tag )
97+ tag_obj .checkout ()
98+ else :
99+ raise ValueError ("Could not find tag: " + repr (tag ))
74100 elif local_directory .is_dir ():
75- repo = Repo (local_directory )
76101 LOGGER .info (f"Found { local_directory } " )
77102 else :
78103 raise KeyError (
79104 f"Unable to open { local_directory } as a git repository because it is a file"
80105 )
81106
82- if branch :
83- if not (local := getattr (repo .heads , branch , None )):
84- origin = repo .remotes [0 ]
85- origin .fetch ()
86- LOGGER .info (
87- "Creating branch '%s' to track remote '%s'" , branch , origin .refs [branch ]
88- )
89- local = repo .create_head (branch , origin .refs [branch ])
90- local .set_tracking_branch (origin .refs [branch ])
91-
92- LOGGER .info ("Checking out branch '%s'" , branch )
93- local .checkout ()
94-
95107
96108def scratch_install (path : Path , timeout : float = _DEFAULT_INSTALL_TIMEOUT ) -> None :
97109 """
0 commit comments