You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker镜像中git show命令输出异常,返回%cs而非提交日期

问题分析

你遇到的核心问题是:基于osixia/openldap的Docker镜像中,git show --format=%cs <branch>无法解析格式符%cs,直接返回字符串%cs,但本地运行完全正常。大概率是以下两个原因之一:

  1. 镜像内安装的Git版本过低,不支持%cs这个格式占位符;
  2. Bitbucket流水线默认使用浅克隆仓库,缺少完整提交历史,导致git show无法获取对应分支的提交信息。
解决方案

1. 检查并升级Git版本

osixia/openldap镜像通常基于旧版Debian(比如Debian 9 Stretch),默认源中的Git版本可能较老。先验证镜像内的Git版本:

# 在Docker容器内执行
git --version

如果版本低于2.2.0(%cs稳定支持的最低版本),通过backports源升级Git,修改Dockerfile如下:

FROM osixia/openldap

# 添加Debian backports源(适配Debian 9)
RUN echo "deb http://deb.debian.org/debian stretch-backports main" >> /etc/apt/sources.list.d/backports.list

# 优先从backports安装新版Git,再安装其他依赖
RUN apt-get update && apt-get install -y -t stretch-backports git && \
    apt-get install -y tar xz-utils openssl bash curl jq gcc libc-dev vim coreutils util-linux fortune sed findutils openssh-client && \
    rm -rf /var/lib/apt/lists/*

# 安装kubectl
RUN curl -L https://storage.googleapis.com/kubernetes-release/release/v1.21.0/bin/linux/amd64/kubectl > /usr/bin/kubectl && \
    chmod +x /usr/bin/kubectl 

ADD bb_cleanup_new.sh /bin/

2. 替换为兼容性更好的Git命令

如果不想升级Git,改用git log替代git show获取提交日期,兼容性更强:
修改脚本中的get_active_branches函数:

get_active_branches(){
    declare -ag active_branches=()

    echo -e "\e[1;32m Scanning all active branches... \e[0m"

    for branch in $(git branch -r | grep -v HEAD); do 
        # 用git log -1获取最新一次提交的日期,兼容旧版Git
        branch_data=$(git log -1 --format=%cs "$branch")
        active_branches+=("$branch_data $branch")
    done 

    echo -e "\e[1;32m Done. \e[0m"
}

3. 配置Bitbucket流水线拉取完整仓库历史

Bitbucket流水线默认是浅克隆(仅拉取最近1次提交),导致远程分支的提交历史缺失。在bitbucket-pipelines.yml中配置完整克隆:

# 全局配置完整克隆
definitions:
  clone:
    depth: full

pipelines:
  default:
    - step:
        script:
          - # 执行你的分支清理脚本

或者在单个step中临时拉取完整历史:

pipelines:
  default:
    - step:
        script:
          - git fetch --unshallow
          - ./bin/bb_cleanup_new.sh

额外调试技巧

在脚本中添加分支检查的输出,快速定位问题:

for branch in $(git branch -r | grep -v HEAD); do 
    echo "Debug: Checking branch $branch"
    # 输出提交哈希和日期,确认是否能获取到有效数据
    git log -1 --format="%H | %cs" "$branch"
    branch_data=$(git log -1 --format=%cs "$branch")
    active_branches+=("$branch_data $branch")
done 

内容的提问来源于stack exchange,提问作者CodeLover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:57:43