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

如何通过AWS CLI筛选近一年修改的CodeCommit仓库?

查询近1年内有修改的CodeCommit仓库的正确方法

原命令无效的原因

aws codecommit list-repositories返回的lastModifiedDate字段仅代表仓库元数据的修改时间(比如仓库名称、描述、权限设置变更),而非代码提交的最后修改时间。如果你的仓库只有代码提交、未修改过元数据,这个字段不会更新,导致查询无结果。

两种正确实现方式

方式一:查询元数据近1年修改过的仓库

修正日期格式(AWS返回的lastModifiedDate是ISO 8601 UTC格式),用JMESPath正确比较:

# 生成1年前的UTC ISO 8601日期
ONE_YEAR_AGO=$(date -u --date='1 year ago' +%Y-%m-%dT%H:%M:%SZ)
# 执行查询
aws codecommit list-repositories --query "repositories[?lastModifiedDate >= '$ONE_YEAR_AGO']" --output text

方式二:查询代码近1年有提交的仓库(更常用)

由于list-repositories不包含代码提交信息,需要遍历所有仓库,检查默认分支的最后提交时间:

# 获取所有仓库名称
REPOS=$(aws codecommit list-repositories --query 'repositories[].repositoryName' --output text)

# 遍历仓库筛选符合条件的
for REPO in $REPOS; do
    # 获取默认分支(示例为main,可改为master或你的实际默认分支)的最后提交时间
    LAST_COMMIT_DATE=$(aws codecommit get-branch --repository-name "$REPO" --branch-name main --query 'branch.commit.commitDate' --output text 2>/dev/null)
    if [ -n "$LAST_COMMIT_DATE" ]; then
        # 转换为时间戳比较
        COMMIT_TIMESTAMP=$(date -d "$LAST_COMMIT_DATE" +%s)
        ONE_YEAR_AGO_TIMESTAMP=$(date -d '1 year ago' +%s)
        if [ "$COMMIT_TIMESTAMP" -ge "$ONE_YEAR_AGO_TIMESTAMP" ]; then
            echo "$REPO"
        fi
    fi
done

注意事项

  • 若使用macOS,date命令参数需调整:
    • 生成1年前ISO日期:ONE_YEAR_AGO=$(date -u -v-1y +%Y-%m-%dT%H:%M:%SZ)
    • 转换提交时间为时间戳:COMMIT_TIMESTAMP=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$LAST_COMMIT_DATE" +%s)
  • 确保AWS CLI拥有权限:codecommit:ListRepositories、codecommit:GetBranch

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:55:23