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

问题描述

我在git中处理邮递员收藏。邮递员做得很好,但是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}并在ID更改的情况下为每行回答n

我看到Git command to programatically add a range of lines of a file to the index?Make git automatically remove trailing whitespace before committing的发展方向都正确

解决方法

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

这比必须使用git模拟交互式来回交互的程序更容易编写,
而且您仍然可以在别名中使用它:

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

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

note :我暗指此脚本应该是bash脚本,显然可以用任何语言(python,ruby,node ...随便什么,都可以写)>

,

这是我根据@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