Search code examples
gitpostmanundogit-patch

git: Undo specific changes in modified postman collection programmatically


I work with postman collections in git. Postman does a lot of things well, but the id regeneration that occur when you import are not ideal.

Essentially importing a postman collection, and exporting it again results in a change for every id

e.g. output from 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);",

I want to go through the changes in the file and undo all the id changes, but preserve all the others.

Essentially I want to automate running git add -p {my_postman.collection.json} answering n to each line with a change to id.

I can see that Git command to programatically add a range of lines of a file to the index? is going the right way as well as Make git automatically remove trailing whitespace before committing as well


Solution

  • I would suggest to write a script, e.g. restore-existing-ids.sh, which would automate restoring the ids in your file on disk.

    This would be more straightforward to write than a program which has to simulate an interactive back & forth with git,
    and you could still use it in an alias :

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

    to "clean" such a file before adding it.

    note : I implied this script should be a bash script, it can obviously be written in any language (python, ruby, node ... whatever floats your boat)