当前位置: 首页 > news >正文

Gerrit Commit批量获取

Gerrit Commit批量获取

        stage('3. 获取变更列表') {steps {sh """cd ${WORKSPACE}ssh -p ${GERRIT_PORT} ${GERRIT_USER}@${GERRIT_HOST} gerrit query --current-patch-set status:open owner:${TARGET_OWNER} | awk '
/^change I/ {if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {split(ref,a,"/");print num, a[5], ref, project}num=project=branch=ref=""
}
/^  number: / { num=\$2 }
/^  project: / { project=\$2 }
/^  branch: / { branch=\$2 }
/^    ref: / { ref=\$2 }
END {if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {split(ref,a,"/");print num, a[5], ref, project}
}
' > aptiv_changes.txtcat aptiv_changes.txt"""}}

(1)解析 

ssh -p ${GERRIT_PORT} ${GERRIT_USER}@${GERRIT_HOST} gerrit query --current-patch-set status:open owner:${TARGET_OWNER}
  • 通过 SSH 连接 Gerrit
  • 使用 gerrit query 命令查询变更
  • --current-patch-set:获取最新版本号
  • status:open:只查 ** 待合入(未合并)** 的变更
  • owner:${TARGET_OWNER}:只查这个用户提交的变更
输出内容为:
$ cat log
change I69bcb4a5f2395a3fa4c8b8df9850deac98e3d0f6project: xxxxx-icc-platform-8295/android/vendor/xxxxx/frameworks/tboxbranch: xxxxx-icc-platform-8295-sup-devtopic: xxxxx_doneid: I69bcb4a5f2395a3fa4c8b8df9850deac98e3d0f6number: 405subject: [IXE-xxx][qnx][animation]: test commit 2026年 05月 09日 星期六 21:37:35 CSTowner:name: ssssemail: ssss@ssss.com.cnusername: ssssurl: http://ip:20260/c/xxxxx-icc-platform-8295/android/vendor/xxxxx/frameworks/tbox/+/405hashtags:createdOn: 2026-05-09 13:37:36 UTClastUpdated: 2026-05-09 14:59:13 UTCopen: truestatus: NEWcurrentPatchSet:number: 1revision: bde14aa3c022aa5b941338f5d4cd0f2c97ea8e8bparents:[3b0c5f894bb919119633117f5cdf422426935e19]ref: refs/changes/05/405/1uploader:name: ssssemail: ssss@ssss.com.cnusername: sssscreatedOn: 2026-05-09 13:37:36 UTCauthor:name: ssssemail: ssss@ssss.com.cnusername: sssskind: REWORKsizeInsertions: 14sizeDeletions: 1change I8498bba1359795ff3eccab26cf420b7e6ad9fd56
.....type: stats
rowCount: 6
runTimeMilliseconds: 23
moreChanges: false

 (2)解析 AWK语句

/^change I/ 匹配一个新变更开始

/^change I/ {# 如果上一个变更的数据有效if( num != "" && branch == "${BRANCH}" && project ~ /^${PROJECT_HEAD}/ ) {split(ref,a,"/");  # 把 ref 按 / 切成数组print num, a[5], ref, project;  # 输出干净数据}# 清空变量,准备存下一个变更num=project=branch=ref=""
}
  • 每次遇到新的变更(change I)
  • 先把上一个变更检查是否符合条件:
    • 有编号
    • 是目标分支
    • 是目标项目开头
     
  • 符合就输出一行干净数据
  • 然后清空变量,准备存下一条

  

#把 Gerrit 输出的每一行,对应存到变量里
/^  number: / { num=$2 }    # 抓取变更号 405
/^  project: / { project=$2 } # 抓取项目名
/^  branch: / { branch=$2 }  # 抓取分支
/^    ref: / { ref=$2 }    # 抓取 ref 拉取地址

  END 语句处理最后一个变更。因为最后一个变更后面没有新的 change I,所以要在 END 里输出。

(3) split 拆分 ref 提取版本号

#原始内容
ref: refs/changes/05/405/1split(ref,a,"/") 后
得到a[1] = refs
a[2] = changes
a[3] = 05
a[4] = 405
a[5] = 1  <-- patchSet 版本号

  

(4)结果

aptiv_changes.txt 的值为
405 1 refs/changes/05/405/1 proj-xxx
404 1 refs/changes/04/404/1 proj-xxx

  后面可以用while read c p r proj 读取。