Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions code/+matbox/+setup/+internal/+github/+api/getDefaultBranch.m
Original file line number Diff line number Diff line change
@@ -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
14 changes: 8 additions & 6 deletions code/+matbox/+setup/+internal/installGithubRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down