MCPcopy Create free account
hub / github.com/idosal/git-mcp / getRepoBranch

Function getRepoBranch

src/api/utils/github.ts:110–171  ·  view source on GitHub ↗
(
  owner: string,
  repo: string,
  env: CloudflareEnvironment,
)

Source from the content-addressed store, hash-verified

108 * @throws Error if the default branch cannot be determined
109 */
110export async function getRepoBranch(
111 owner: string,
112 repo: string,
113 env: CloudflareEnvironment,
114): Promise<string> {
115 try {
116 // First try to get the actual default branch using GitHub API
117 const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
118 const response = await githubApiRequest(apiUrl, {}, env);
119
120 if (response && response.ok) {
121 const data = (await response.json()) as { default_branch?: string };
122 if (data && data.default_branch) {
123 console.log("Default branch found", data.default_branch);
124 return data.default_branch;
125 }
126 }
127
128 console.error(
129 "No default branch found, falling back to main/master check",
130 response,
131 );
132
133 // Fall back to the main/master check if API request fails
134 // Try 'main' branch
135 const mainUrl = constructGithubUrl(owner, repo, "main", "README.md");
136 const mainResponse = await githubApiRequest(
137 mainUrl,
138 { method: "HEAD" },
139 env,
140 );
141
142 if (mainResponse && mainResponse.ok) {
143 return "main";
144 }
145
146 // If 'main' branch doesn't exist, try 'master'
147 const masterUrl = constructGithubUrl(owner, repo, "master", "README.md");
148 const masterResponse = await githubApiRequest(
149 masterUrl,
150 { method: "HEAD" },
151 env,
152 );
153
154 if (masterResponse && masterResponse.ok) {
155 return "master";
156 }
157
158 // If neither branch exists, throw an error
159 throw new Error(
160 `Could not determine default branch for ${owner}/${repo}. Neither 'main' nor 'master' branches found.`,
161 );
162 } catch (error) {
163 console.error(
164 `Error determining default branch for ${owner}/${repo}:`,
165 error,
166 );
167 // Default to 'main' in case of network errors or other issues

Callers 2

fetchDocumentationFunction · 0.85

Calls 5

githubApiRequestFunction · 0.85
constructGithubUrlFunction · 0.85
jsonMethod · 0.80
logMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected