| layout | default |
|---|---|
| title | Chapter 4: Git Repository Source Imports |
| nav_order | 4 |
| parent | OpenSrc Tutorial |
Welcome to Chapter 4: Git Repository Source Imports. In this part of OpenSrc Tutorial: Deep Source Context for Coding Agents, you will build an intuitive mental model first, then move into concrete implementation details and practical production tradeoffs.
OpenSrc can fetch direct git repositories when package metadata is not the right entry path.
github:owner/repoowner/repo(defaults to GitHub)owner/repo@tagorowner/repo#branchhttps://github.com/owner/repogitlab:owner/repoand other supported hosts
Repositories are organized under host/owner/repo path segments:
opensrc/
repos/
github.com/
vercel/
ai/
You now understand how OpenSrc imports repository source directly and normalizes storage paths.
Next: Chapter 5: AGENTS.md and sources.json Integration
The resolveGitHubRepo function in src/lib/repo.ts handles a key part of this chapter's functionality:
if (host === "github.com") {
return resolveGitHubRepo(host, owner, repo, ref);
} else if (host === "gitlab.com") {
return resolveGitLabRepo(host, owner, repo, ref);
} else {
// For unsupported hosts, assume default branch is "main"
return {
host,
owner,
repo,
ref: ref || "main",
repoUrl: `https://${host}/${owner}/${repo}`,
displayName: `${host}/${owner}/${repo}`,
};
}
}
async function resolveGitHubRepo(
host: string,
owner: string,
repo: string,
ref?: string,
): Promise<ResolvedRepo> {
const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
const response = await fetch(apiUrl, {
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "opensrc-cli",
},
});This function is important because it defines how OpenSrc Tutorial: Deep Source Context for Coding Agents implements the patterns covered in this chapter.
The resolveGitLabRepo function in src/lib/repo.ts handles a key part of this chapter's functionality:
return resolveGitHubRepo(host, owner, repo, ref);
} else if (host === "gitlab.com") {
return resolveGitLabRepo(host, owner, repo, ref);
} else {
// For unsupported hosts, assume default branch is "main"
return {
host,
owner,
repo,
ref: ref || "main",
repoUrl: `https://${host}/${owner}/${repo}`,
displayName: `${host}/${owner}/${repo}`,
};
}
}
async function resolveGitHubRepo(
host: string,
owner: string,
repo: string,
ref?: string,
): Promise<ResolvedRepo> {
const apiUrl = `https://api.github.com/repos/${owner}/${repo}`;
const response = await fetch(apiUrl, {
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "opensrc-cli",
},
});
if (!response.ok) {This function is important because it defines how OpenSrc Tutorial: Deep Source Context for Coding Agents implements the patterns covered in this chapter.
The displayNameToSpec function in src/lib/repo.ts handles a key part of this chapter's functionality:
* Convert a repo display name back to host/owner/repo format
*/
export function displayNameToSpec(displayName: string): {
host: string;
owner: string;
repo: string;
} | null {
const parts = displayName.split("/");
if (parts.length !== 3) {
return null;
}
return { host: parts[0], owner: parts[1], repo: parts[2] };
}
/**
* @deprecated Use displayNameToSpec instead
*/
export function displayNameToOwnerRepo(displayName: string): {
owner: string;
repo: string;
} | null {
// Handle old format: owner--repo
if (displayName.includes("--") && !displayName.includes("/")) {
const parts = displayName.split("--");
if (parts.length !== 2) {
return null;
}
return { owner: parts[0], repo: parts[1] };
}
// Handle new format: host/owner/repo
const spec = displayNameToSpec(displayName);This function is important because it defines how OpenSrc Tutorial: Deep Source Context for Coding Agents implements the patterns covered in this chapter.
The displayNameToOwnerRepo function in src/lib/repo.ts handles a key part of this chapter's functionality:
* @deprecated Use displayNameToSpec instead
*/
export function displayNameToOwnerRepo(displayName: string): {
owner: string;
repo: string;
} | null {
// Handle old format: owner--repo
if (displayName.includes("--") && !displayName.includes("/")) {
const parts = displayName.split("--");
if (parts.length !== 2) {
return null;
}
return { owner: parts[0], repo: parts[1] };
}
// Handle new format: host/owner/repo
const spec = displayNameToSpec(displayName);
if (!spec) {
return null;
}
return { owner: spec.owner, repo: spec.repo };
}This function is important because it defines how OpenSrc Tutorial: Deep Source Context for Coding Agents implements the patterns covered in this chapter.
flowchart TD
A[resolveGitHubRepo]
B[resolveGitLabRepo]
C[displayNameToSpec]
D[displayNameToOwnerRepo]
E[GitHubApiResponse]
A --> B
B --> C
C --> D
D --> E