diff --git a/code/+matbox/+setup/+internal/+github/+api/getDefaultBranch.m b/code/+matbox/+setup/+internal/+github/+api/getDefaultBranch.m new file mode 100644 index 0000000..26c95b4 --- /dev/null +++ b/code/+matbox/+setup/+internal/+github/+api/getDefaultBranch.m @@ -0,0 +1,28 @@ +function defaultBranch = getDefaultBranch(owner, repoName) +%GETDEFAULTBRANCH Returns the default branch of a GitHub repository. +% defaultBranch = getDefaultBranch(owner, repoName) queries the GitHub +% API to get the repository metadata and returns the default branch name. +% +% Example: +% branch = getDefaultBranch('octocat', 'Hello-World'); + + % Build the API URL + apiUrl = sprintf('https://api.github.com/repos/%s/%s', owner, repoName); + + requestOpts = matbox.setup.internal.github.api.getGithubWebOptions(); + requestOpts.Timeout = 10; + requestOpts.ContentType = 'json'; + + try + response = webread(apiUrl, requestOpts); + catch ME + error('Failed to fetch repository info: %s', ME.message); + end + + % Extract the default_branch field + if isfield(response, 'default_branch') + defaultBranch = response.default_branch; + else + error('default_branch field not found in API response.'); + end +end diff --git a/code/+matbox/+setup/+internal/installGithubRepository.m b/code/+matbox/+setup/+internal/installGithubRepository.m index fb2f5f6..f93170e 100644 --- a/code/+matbox/+setup/+internal/installGithubRepository.m +++ b/code/+matbox/+setup/+internal/installGithubRepository.m @@ -17,18 +17,20 @@ repositoryUrl (1,1) string branchName (1,1) string = "main" options.Update (1,1) logical = false + options.UseGit (1,1) logical = false options.InstallationLocation (1,1) string = matbox.setup.internal.getDefaultAddonFolder() options.AddToPath (1,1) logical = true options.AddToPathWithSubfolders (1,1) logical = true options.Verbose (1,1) logical = true end - % Branch name might be set to missing from upstream callers - % Todo: Should default to "default" and use git api to resolve name of - % default branch - if ismissing(branchName); branchName = "main"; end - - [ownerName, repositoryName] = matbox.setup.internal.github.parseRepositoryURL(repositoryUrl); + [ownerName, repositoryName] = ... + matbox.setup.internal.github.parseRepositoryURL(repositoryUrl); + + if ismissing(branchName) % - Get default branchname + branchName = matbox.setup.internal.github.api.getDefaultBranch(... + ownerName, repositoryName); + end [repoExists, repoFolderLocation] = ... matbox.setup.internal.pathtool.lookForRepository(repositoryName, branchName);