From d246b9b1e8ca94304f807b8aec9e1856fe115451 Mon Sep 17 00:00:00 2001 From: ehennestad Date: Tue, 1 Jul 2025 20:52:21 +0200 Subject: [PATCH 1/2] Create getDefaultBranch.m --- .../+internal/+github/+api/getDefaultBranch.m | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 code/+matbox/+setup/+internal/+github/+api/getDefaultBranch.m 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 From 50a8d8b5a5fe8ff6fcf812a286283e6ae92406e6 Mon Sep 17 00:00:00 2001 From: ehennestad Date: Tue, 1 Jul 2025 20:53:03 +0200 Subject: [PATCH 2/2] Update installGithubRepository.m completed todo to check for default branch using github api --- .../+setup/+internal/installGithubRepository.m | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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);