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
26 changes: 26 additions & 0 deletions docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
</script>
{% endblock %}

{# 将 giscus 插入到正文(内容)块的末尾,紧跟文章内容之后 #}
{% block content %}
{{ super() }}
{% if giscus and giscus.get('enabled') and giscus.get('repo') and giscus.get('repo_id') and (giscus.get('category_id') or giscus.get('category')) %}
<div id="giscus_thread" class="giscus" style="margin: 2rem 0;"></div>
<script src="https://giscus.app/client.js"
data-repo="{{ giscus.get('repo') }}"
data-repo-id="{{ giscus.get('repo_id') }}"
{% if giscus.get('category_id') %}
data-category-id="{{ giscus.get('category_id') }}"
{% elif giscus.get('category') %}
data-category="{{ giscus.get('category') }}"
{% endif %}
data-mapping="{{ giscus.get('mapping', 'pathname') }}"
data-strict="{{ giscus.get('strict', 0) }}"
data-reactions-enabled="{{ giscus.get('reactions_enabled', 1) }}"
data-emit-metadata="{{ giscus.get('emit_metadata', 0) }}"
data-input-position="{{ giscus.get('input_position', 'bottom') }}"
data-theme="{{ giscus.get('theme', 'preferred_color_scheme') }}"
data-lang="{{ giscus.get('lang', 'zh-CN') }}"
{% if giscus.get('lazy') %} data-loading="lazy"{% endif %}
crossorigin="anonymous"
async>
</script>
{% endif %}
{% endblock %}

{# 将 giscus 插入到正文(内容)块的末尾,紧跟文章内容之后 #}
{% block content %}
Expand Down
118 changes: 27 additions & 91 deletions docs/source/build_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,104 +73,40 @@ def get_version_configs(self) -> List[VersionConfig]:
versions.append(VersionConfig(version_dict))
return versions

def get_current_branch():
"""
安全获取当前分支名,处理分离头指针和错误情况
"""
try:
# 检查是否在Git仓库中
subprocess.run(
['git', 'rev-parse', '--git-dir'],
capture_output=True, check=True
)

# 尝试获取分支名称
result = subprocess.run(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
capture_output=True, text=True, check=True
)
branch = result.stdout.strip()

# 如果返回HEAD,说明在分离头指针状态
if branch == "HEAD":
result = subprocess.run(
['git', 'rev-parse', '--short', 'HEAD'],
capture_output=True, text=True, check=True
)
return f"detached-{result.stdout.strip()}"

return branch

except subprocess.CalledProcessError:
return "not-a-repo"
except Exception as e:
return f"error: {str(e)}"

def is_in_ci_environment() -> bool:
"""检查是否在CI环境中"""
return os.environ.get('CI') == 'true' or os.environ.get('GITHUB_ACTIONS') == 'true'

def create_worktree(self, version_config: VersionConfig) -> Path:
"""为指定版本创建 Git worktree"""
worktree_path = self.worktrees_dir / version_config.name

if is_in_ci_environment():
print("检测到CI环境,使用特殊处理逻辑")
# 在CI环境中,我们通常已经在正确的分支上
# 或者可以直接使用指定的分支

# 检查目标分支是否存在
# 获取当前分支
current_branch = subprocess.run(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
capture_output=True, text=True, check=True
).stdout.strip()

# 如果目标分支就是当前分支,直接使用当前目录
if version_config.branch == current_branch:
print(f"目标分支 {version_config.branch} 就是当前分支,使用当前目录")
return Path.cwd()

# 清理已存在的 worktree
if worktree_path.exists():
print(f"清理已存在的 worktree: {worktree_path}")
try:
subprocess.run(['git', 'show-ref', '--verify', f'refs/heads/{version_config.branch}'],
check=True, capture_output=True, cwd=Path.cwd())
# 如果分支存在,直接使用当前目录(假设CI已经checkout到正确分支)
return Path.cwd()
subprocess.run(['git', 'worktree', 'remove', str(worktree_path)],
check=True, capture_output=True)
except subprocess.CalledProcessError:
# 分支不存在,可能需要从远程获取
print(f"分支 {version_config.branch} 不存在,尝试从远程获取")
try:
subprocess.run(['git', 'fetch', 'origin', f'{version_config.branch}:{version_config.branch}'],
check=True, cwd=Path.cwd())
except subprocess.CalledProcessError:
print(f"无法获取分支 {version_config.branch},使用当前目录")
return Path.cwd()

def create_worktree(self, version_config: VersionConfig) -> Path:
"""为指定版本创建 Git worktree"""
worktree_path = self.worktrees_dir / version_config.name
# 如果 worktree remove 失败,手动删除
shutil.rmtree(worktree_path, ignore_errors=True)

try:
# 安全获取当前分支
current_branch = get_current_branch()

if current_branch and version_config.branch == current_branch:
print(f"目标分支 {version_config.branch} 就是当前分支,使用当前目录")
return Path.cwd()

# 清理已存在的 worktree
if worktree_path.exists():
print(f"清理已存在的 worktree: {worktree_path}")
try:
subprocess.run(['git', 'worktree', 'remove', '--force', str(worktree_path)],
check=True, capture_output=True, cwd=Path.cwd())
except subprocess.CalledProcessError:
print(f"Git worktree remove 失败,尝试手动删除")
shutil.rmtree(worktree_path, ignore_errors=True)

# 创建新的 worktree
print(f"创建 worktree: {version_config.branch} -> {worktree_path}")
subprocess.run([
'git', 'worktree', 'add', '--force',
str(worktree_path), version_config.branch
], check=True, cwd=Path.cwd())

return worktree_path

except Exception as e:
print(f"创建worktree失败: {e}")
# 失败时返回当前目录作为fallback
return Path.cwd()

# 创建新的 worktree
print(f"创建 worktree: {version_config.branch} -> {worktree_path}")
subprocess.run([
'git', 'worktree', 'add',
str(worktree_path), version_config.branch
], check=True)

return worktree_path

def build_docs_in_worktree(self, worktree_path: Path, version_config: VersionConfig) -> bool:
"""在 worktree 中构建文档"""
print(f"在 worktree 中构建文档: {worktree_path}")
Expand Down
Loading