facebookincubator / fastmod

A fast partial replacement for the codemod tool
Apache License 2.0
1.66k stars 41 forks source link

Feature request: flags to exclude directories / respect .gitignore #37

Closed kkom closed 1 year ago

kkom commented 1 year ago

Problem

This came up when I was trying to do a version bump this way:

fastmod --hidden --ignore-case '(poetry.*)1\.2\.2' '${1}1.3.2'

I set the --hidden flag in order to also match asdf's .tool-versions file (needs to be included in the version bump).

However, I found fastmod matching files in the .git/ folder - which is something I definitely do not want to mess with (it was matching some of the previous commits where I performed the version bump to 1.2.2):

./.git/.graphite_cache_persist:128
        }
      ],
      [
        "poetry",
        {
          "validationResult": "VALID",
          "parentBranchName": "main",
          "parentBranchRevision": "3deb75c540a0daeb29986e3ef3cc36e07b8f07b0",
          "branchRevision": "fce724219a729bfb03b8bdd93035872c56f26dfd",
          "children": [],
          "prInfo": {
            "title": "upgrade poetry to 1.2.2",
-           "body": "Run this to catch places where our current poetry version is mentioned after the string \"poetry\":\n\n```\nfastmod --hidden --ignore-case '(poetry.*)1\\.2\\.1' '${1}1.2.2'\n```\n",
+           "body": "Run this to catch places where our current poetry version is mentioned after the string \"poetry\":\n\n```\nfastmod --hidden --ignore-case '(poetry.*)1\\.2\\.1' '${1}1.3.2'\n```\n",
            "number": 280,
            "url": "https://app.graphite.dev/github/pr/exponential-hq/expo/280",
            "base": "main",
            "state": "OPEN",
            "reviewDecision": "REVIEW_REQUIRED",
            "draft": false
          }
        }
      ]
    ]
  }
Accept change (y = yes [default], n = no, e = edit, A = yes to all, E = yes+edit, q = quit)?

(In this case it was a graphite file, rather than a vanilla git file, but I don't think it matters.)

I suspect that I could somehow use the --glob flag to exclude the directories I wanted, but it's not obvious to me how to do it in an elegant and reliable way.

Potential solution 1

Wdyt about having a flag to explicitly exclude some directories?

Similar to how comby has --exclude and --exclude-dirs: https://comby.dev/docs/cheat-sheet

Potential solution 2

Or have a mode where only files tracked by git would be modified by fastmod? I presume that .git is implicitly ignored by git when doing source control, so that could be also implicitly assumed as well.

I know that Facebook uses Eden now, but maybe you can consider being aware of git too, as nod to the open source community.

swolchok commented 1 year ago

I just checked, and prefixing globs with a ! to invert them works with fastmod as it does with ripgrep, so something like fastmod --hidden --glob '!.git' should help.

kkom commented 1 year ago

Thank you @swolchok – that works beautifully! I am personally very happy now with the solution – so obviously feel free to close this task.