1

我在 git 中使用邮递员集合。Postman 做的很多事情都很好,但是导入时发生的 id 再生并不理想。

本质上是导入一个邮递员集合,然后再次导出它会导致每个人都发生变化id

例如输出git diff-index -p HEAD --

@@ -2404,7 +2412,7 @@
      {
        "listen": "test",
        "script": {
-           "id": "60ff37a6-9bf7-4cb4-b142-2da49ff4b86e",
+           "id": "38c15d28-8382-4eaf-ad17-f053c143212d",
            "exec": [
                "pm.test(\"Status code is 200\", function () {",
                "    pm.response.to.have.status(200);",

我想检查文件中的更改并撤消所有 id 更改,但保留所有其他更改。

本质上,我想通过更改 id 自动运行对每一行的git add -p {my_postman.collection.json}回答。n

我可以看到Git 命令以编程方式将文件的一系列行添加到索引中?正在朝着正确的方向前进,并使 git在提交之前自动删除尾随空格

4

2 回答 2

0

这是我根据@LeGEC 的建议提出的解决方案,供其他寻求实际解决方案的人使用。


    #!/usr/bin/env bash
    
    # Check the command line argument value exists or not
    if [ $1 != "" ]; then
    
        if [ $1 == "--help" ]; then
    
            echo "Replaces UUIDs in *.postman_collection.json exported files with 00000000-0000-0000-0000-000000000000"
            exit 0
        else
    
            if [ "${1: -24}" == ".postman_collection.json" ]; then
    
                echo "Removing IDs from $1"
                # stat -c %y "$1" # show last modified time
    
                # format:   60ff37a6-9bf7-4cb4-b142-2da499f4b86e
                #           00000000-0000-0000-0000-000000000000
                #           12345678-1234-1234-1234-123456789012
    
                 sed -i -r 's/"(_postman_id|id)": "([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})"/"\1": "00000000-0000-0000-0000-000000000000"/gm' "$1"
    
            else
    
                echo "Your file MUST end on .postman_collection.json or else it will not be parsed! \"$1\" is not valid"
                exit 1
    
            fi
    
        fi
    fi

这种.git/hooks/pre-commit 方法的缺点是钩子不是存储库的一部分。我已将这些脚本上传到 github

试图通过研究这个答案来了解它的工作方式:“git commit”的意外行为。当预提交挂钩修改暂存文件时



#!/usr/bin/env bash

#get the changed files
files=`git diff --cached --name-status | awk '$1 != "D" { print $2 }'`
#set changed flag to false
CHANGED=false
for filename in $files; do
    if [ "${filename: -24}" == ".postman_collection.json" ]; then
        sed -i -r 's/"(_postman_id|id)": "([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})"/"\1": "00000000-0000-0000-0000-000000000000"/gm' "$filename"
        git add $filename
        # mark changed flag true
        CHANGED=true
    fi
done
# if files have been changed (potentially) display a message and abort the commit
if $CHANGED; then
    echo "PRE-COMMIT: Postman collection found. UUIDs have been sanitized. Please verify and recommit"
    exit 1
fi
于 2020-10-31T17:29:34.113 回答
0

我建议编写一个脚本,例如restore-existing-ids.sh,它会自动恢复磁盘上文件中的 id。

这将比必须模拟与 git 交互来回的程序更简单,
并且您仍然可以在别名中使用它:

add-postman = '! f () { bash restore-existing-ids.sh "$1" && git add "$1"; }; f'

在添加之前“清理”这样的文件。

注意:我暗示这个脚本应该是一个bash脚本,它显然可以用任何语言编写(python、ruby、node ......随便你的船)

于 2020-10-30T17:03:10.003 回答