git:以编程方式撤消修改后的邮递员集合中的特定更改

git: Undo specific changes in modified postman collection programmatically

我在 git 从事邮递员收件工作。 Postman 很多事情做得很好,但是 the id regeneration that occur when you import 并不理想。

本质上是导入邮递员集合,然后再次导出它会导致每个 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 更改,但保留所有其他更改。

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

我也能看到 is going the right way as well as Make git automatically remove trailing whitespace before committing

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

这比必须模拟与 git、
来回交互的程序更容易编写 你仍然可以在别名中使用它:

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

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

note : 我暗示这个脚本应该是一个 bash 脚本,它显然可以用任何语言编写 (python, ruby, node ... 随心所欲)

这是我根据@LeGEC 的建议提出的解决方案,放在这里供其他前来寻找实际解决方案的人使用。


    #!/usr/bin/env bash
    
    # Check the command line argument value exists or not
    if [  != "" ]; then
    
        if [  == "--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 "
                # stat -c %y "" # 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})"/"": "00000000-0000-0000-0000-000000000000"/gm' ""
    
            else
    
                echo "Your file MUST end on .postman_collection.json or else it will not be parsed! \"\" is not valid"
                exit 1
    
            fi
    
        fi
    fi

.git/hooks/pre-commit的版本 这种方法的缺点是挂钩不是存储库的一部分。 我有 uploaded these scripts to github

试图通过研究这个答案来了解它的工作方式:



#!/usr/bin/env bash

#get the changed files
files=`git diff --cached --name-status | awk ' != "D" { print  }'`
#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})"/"": "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