【问题标题】:git: Undo specific changes in modified postman collection programmaticallygit:以编程方式撤消修改后的邮递员集合中的特定更改
【发布时间】:2021-02-13 02:27:06
【问题描述】:

我在 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。

我可以看到Git command to programatically add a range of lines of a file to the index?Make git automatically remove trailing whitespace before committing 一样正确

【问题讨论】:

    标签: git postman undo git-patch


    【解决方案1】:

    这是我根据@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 的版本 这种方法的缺点是钩子不是存储库的一部分。 我有uploaded these scripts to github

    试图通过研究这个答案来了解它的工作方式:Unexpected behavior with "git commit ." when pre-commit hook modifies staged files


    
    #!/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
    

    【讨论】:

      【解决方案2】:

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

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

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

      在添加之前“清理”此类文件。

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

      【讨论】:

      • 我不知道邮递员是如何工作的,如果邮递员总是重新生成这些id,那么脚本可以像sed -e 's/"id": "[^"]*",$/"id": "00000000-0000-0000-0000-000000000000",/'一样简单(系统地将这些GUID替换为固定值)
      • 我没有考虑给它们分配一个固定值——好点
      • 如果它们仍然重新生成,你甚至可以考虑删除它们
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 2021-02-17
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多