开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
claude-code
/
src
/
utils
/
detectRepository.ts
claude-code
/
src
/
utils
/
detectRepository.ts
detectRepository.ts 5.92 KB
一键复制 编辑 原始数据 按行查看 历史
claude-code-best 提交于 2026年03月31日 19:22 +08:00 . feat: build
import { getCwd } from './cwd.js'
import { logForDebugging } from './debug.js'
import { getRemoteUrl } from './git.js'
export type ParsedRepository = {
host: string
owner: string
name: string
}
const repositoryWithHostCache = new Map<string, ParsedRepository | null>()
export function clearRepositoryCaches(): void {
repositoryWithHostCache.clear()
}
export async function detectCurrentRepository(): Promise<string | null> {
const result = await detectCurrentRepositoryWithHost()
if (!result) return null
// Only return results for github.com to avoid breaking downstream consumers
// that assume the result is a github.com repository.
// Use detectCurrentRepositoryWithHost() for GHE support.
if (result.host !== 'github.com') return null
return `${result.owner}/${result.name}`
}
/**
* Like detectCurrentRepository, but also returns the host (e.g. "github.com"
* or a GHE hostname). Callers that need to construct URLs against a specific
* GitHub host should use this variant.
*/
export async function detectCurrentRepositoryWithHost(): Promise<ParsedRepository | null> {
const cwd = getCwd()
if (repositoryWithHostCache.has(cwd)) {
return repositoryWithHostCache.get(cwd) ?? null
}
try {
const remoteUrl = await getRemoteUrl()
logForDebugging(`Git remote URL: ${remoteUrl}`)
if (!remoteUrl) {
logForDebugging('No git remote URL found')
repositoryWithHostCache.set(cwd, null)
return null
}
const parsed = parseGitRemote(remoteUrl)
logForDebugging(
`Parsed repository: ${parsed ? `${parsed.host}/${parsed.owner}/${parsed.name}` : null} from URL: ${remoteUrl}`,
)
repositoryWithHostCache.set(cwd, parsed)
return parsed
} catch (error) {
logForDebugging(`Error detecting repository: ${error}`)
repositoryWithHostCache.set(cwd, null)
return null
}
}
/**
* Synchronously returns the cached github.com repository for the current cwd
* as "owner/name", or null if it hasn't been resolved yet or the host is not
* github.com. Call detectCurrentRepository() first to populate the cache.
*
* Callers construct github.com URLs, so GHE hosts are filtered out here.
*/
export function getCachedRepository(): string | null {
const parsed = repositoryWithHostCache.get(getCwd())
if (!parsed || parsed.host !== 'github.com') return null
return `${parsed.owner}/${parsed.name}`
}
/**
* Parses a git remote URL into host, owner, and name components.
* Accepts any host (github.com, GHE instances, etc.).
*
* Supports:
* https://host/owner/repo.git
* git@host:owner/repo.git
* ssh://git@host/owner/repo.git
* git://host/owner/repo.git
* https://host/owner/repo (no .git)
*
* Note: repo names can contain dots (e.g., cc.kurs.web)
*/
export function parseGitRemote(input: string): ParsedRepository | null {
const trimmed = input.trim()
// SSH format: git@host:owner/repo.git
const sshMatch = trimmed.match(/^git@([^:]+):([^/]+)\/([^/]+?)(?:\.git)?$/)
if (sshMatch?.[1] && sshMatch[2] && sshMatch[3]) {
if (!looksLikeRealHostname(sshMatch[1])) return null
return {
host: sshMatch[1],
owner: sshMatch[2],
name: sshMatch[3],
}
}
// URL format: https://host/owner/repo.git, ssh://git@host/owner/repo, git://host/owner/repo
const urlMatch = trimmed.match(
/^(https?|ssh|git):\/\/(?:[^@]+@)?([^/:]+(?::\d+)?)\/([^/]+)\/([^/]+?)(?:\.git)?$/,
)
if (urlMatch?.[1] && urlMatch[2] && urlMatch[3] && urlMatch[4]) {
const protocol = urlMatch[1]
const hostWithPort = urlMatch[2]
const hostWithoutPort = hostWithPort.split(':')[0] ?? ''
if (!looksLikeRealHostname(hostWithoutPort)) return null
// Only preserve port for HTTPS — SSH/git ports are not usable for constructing
// web URLs (e.g. ssh://git@ghe.corp.com:2222 → port 2222 is SSH, not HTTPS).
const host =
protocol === 'https' || protocol === 'http'
? hostWithPort
: hostWithoutPort
return {
host,
owner: urlMatch[3],
name: urlMatch[4],
}
}
return null
}
/**
* Parses a git remote URL or "owner/repo" string and returns "owner/repo".
* Only returns results for github.com hosts — GHE URLs return null.
* Use parseGitRemote() for GHE support.
* Also accepts plain "owner/repo" strings for backward compatibility.
*/
export function parseGitHubRepository(input: string): string | null {
const trimmed = input.trim()
// Try parsing as a full remote URL first.
// Only return results for github.com hosts — existing callers (VS Code extension,
// bridge) assume this function is GitHub.com-specific. Use parseGitRemote() directly
// for GHE support.
const parsed = parseGitRemote(trimmed)
if (parsed) {
if (parsed.host !== 'github.com') return null
return `${parsed.owner}/${parsed.name}`
}
// If no URL pattern matched, check if it's already in owner/repo format
if (
!trimmed.includes('://') &&
!trimmed.includes('@') &&
trimmed.includes('/')
) {
const parts = trimmed.split('/')
if (parts.length === 2 && parts[0] && parts[1]) {
// Remove .git extension if present
const repo = parts[1].replace(/\.git$/, '')
return `${parts[0]}/${repo}`
}
}
logForDebugging(`Could not parse repository from: ${trimmed}`)
return null
}
/**
* Checks whether a hostname looks like a real domain name rather than an
* SSH config alias. A simple dot-check is not enough because aliases like
* "github.com-work" still contain a dot. We additionally require that the
* last segment (the TLD) is purely alphabetic — real TLDs (com, org, io, net)
* never contain hyphens or digits.
*/
function looksLikeRealHostname(host: string): boolean {
if (!host.includes('.')) return false
const lastSegment = host.split('.').pop()
if (!lastSegment) return false
// Real TLDs are purely alphabetic (e.g., "com", "org", "io").
// SSH aliases like "github.com-work" have a last segment "com-work" which
// contains a hyphen.
return /^[a-zA-Z]+$/.test(lastSegment)
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jbcode/claude-code.git
git@gitee.com:jbcode/claude-code.git
jbcode
claude-code
claude-code
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /