fastmod

repository·main·Indexed 23 days ago

https://github.com/facebookincubator/fastmod

A high-performance, interactive find-and-replace tool for large-scale codebase refactoring using regular expressions. Written in Rust, fastmod allows users to review, accept, reject, or edit substitutions match-by-match with colored diffs. It supports multiline regex, file extension filtering, and custom external diff tools.

Tokens
1.8K
Snippets
2
Records
11
Agent score
34%

What's inside fastmod

  1. Requirements and Platform Support

    main

    Supported Platforms

    • macOS: Fully supported.
    • Linux: Fully supported.
    • Windows: Reported to work reasonably well, though users may encounter issues with the console UI or the $EDITOR fallback.

    Dependencies

    • Rust: Requires a stable Rust toolchain for building.
    • $EDITOR: fastmod relies on the $EDITOR environment variable for interactive line editing.
  2. Build fastmod from source

    main

    Since fastmod is written in Rust, you can build it using cargo. Ensure you have a stable Rust toolchain installed.

    1. Clone the repository.
    2. Navigate to the directory.
    3. Run cargo build --release.
    4. The binary will be located in ./target/release/fastmod.
    $ git clone https://github.com/facebookincubator/fastmod.git
    $ cd fastmod
    $ cargo build --release
    $ ./target/release/fastmod --help
  3. Use fastmod for interactive regex refactoring

    main

    Use fastmod to perform large-scale codebase refactors using regular expressions. The tool is optimized for an interactive workflow where you review changes match-by-match.

    Interactive Mode

    Use the -m flag to enter interactive mode. For every match found, fastmod will show a colored diff and prompt you to:

    • Accept the change.
    • Reject the change.
    • Edit the line using your configured $EDITOR.

    Regex Syntax and Capture Groups

    fastmod uses the Rust regex crate. Note these specific syntax requirements:

    • Capture Groups: Use ${1}, ${2}, etc., in the replacement string instead of Python-style \1 or \2.
    • Literal Dollar Signs: Use $$ to represent a literal $ in the replacement string.
    • Shell Escaping: It is highly recommended to wrap your replacement string in single quotes ('...') to prevent your shell (like bash) from interpreting the $ characters before they reach fastmod.

    Specifying Targets

    You can specify files or directories to process as positional arguments after the regex and substitution strings. You can also limit processing to specific file extensions using the --extensions flag.

    # Example: Replacing <font color="..."> with <span style="color: ..."> across a directory
    fastmod -m --extensions php,html \
        '<font *color="?(.*?)"?>(.*?)</font>' \
        '<span style="color: ${1};">${2}</span>' \
        /home/jrosenstein/www
  4. Use fastmod for large-scale codebase refactors

    main

    fastmod is a tool designed for large-scale codebase refactors that require human oversight. It uses regular expressions to find and replace text, providing a colored diff and interactive prompts for each match.

    Regex Syntax Note: fastmod uses the Rust regex crate. Unlike Python regexes used in codemod:

    • It does not support look-around or backreferences.
    • Use ${1} instead of \1 to access the first capture group in the replacement string.
    • Use $$ to write a literal $ in the replacement string.
    • It is recommended to use single quotes around the replacement text in your shell to avoid shell expansion of $ characters.
  5. Interactive vs Fast mode in fastmod

    main

    fastmod operates in two primary modes based on the --accept-all flag:

    1. Interactive Mode (Default): For each match, fastmod shows a colored diff and prompts the user for action:

      • y: Accept the change.
      • n: Reject the change.
      • e: Edit the line in the user's $EDITOR.
      • A: Accept this change and all subsequent changes (switches to Fast mode).
      • E: Accept this change and open the editor.
      • q: Quit.
    2. Fast Mode (--accept-all): Automatically applies all replacements without prompting. This is significantly faster for large-scale, confident refactors.

  6. Use an external diff tool with fastmod

    main

    By default, fastmod uses a built-in diff viewer. You can specify an external tool to display diffs instead. The tool will be invoked with two temporary file paths as arguments (the old content and the new content).

    Examples:

    --diff-tool difft
    --diff-tool delta
    --diff-tool 'difft --color always'
  7. Use the Fastmod API for programmatic replacements

    main

    The Fastmod struct can be used programmatically to apply patches to files. The method present_and_apply_patches allows you to apply a regex-based replacement to a specific file path.

    Note that Fastmod can be initialized with various boolean flags (likely controlling behavior like accepting all, printing changes, etc., though exact constructor signatures depend on the full implementation).

  8. fastmod CLI Flags Reference

    main

    Refer to this list for all available fastmod command-line flags:

    FlagLong FlagDescription
    -m--multilineHave regex work over multiple lines (dot matches newlines).
    -d--dir DIRThe path whose descendant files are to be explored.
    -i--ignore-casePerform case-insensitive search.
    -e--extensions EXTComma-delimited list of file extensions to process (e.g., php,html).
    -g--glob GLOBSpace-delimited list of globs to process.
    --hidden--hiddenSearch hidden files.
    -u--no-ignoreAlso search ignored files (ignores .gitignore, etc.).
    --iglob--iglob IGLOBSpace-delimited list of case-insensitive globs to process.
    --accept-all--accept-allAutomatically accept all changes (use with caution).
    --print-changed-files--print-changed-filesPrint the paths of changed files. (Recommended with --accept-all).
    -F--fixed-stringsTreat REGEX as a literal string.
    --diff-tool--diff-tool COMMANDUse an external diff tool (e.g., delta, difftastic).

    Conflicts:

    • --extensions conflicts with --glob and --iglob.
    • --glob conflicts with --iglob.
  9. fastmod CLI Reference

    main

    The fastmod command-line interface accepts a regex to match and a substitution string.

    Positional Arguments:

    1. REGEX: The regular expression to match.
    2. SUBST: The substitution string to replace matches with.
    3. FILE OR DIR: (Optional) Paths whose descendant files are to be explored.

    Flags:

  10. Use CLI flags to control fastmod execution

    main

    The fastmod CLI provides several flags to control how replacements are applied and how files are discovered. Key flags include:

    • --accept-all: Automatically accepts all proposed changes without prompting for user confirmation.
    • --print-changed-files: Prints the paths of all files that were modified during the operation.
    • --dir <PATH>: Specifies the directory in which to search for files.
    • --hidden: Includes hidden files (files starting with a dot on Unix systems) in the search.
    • --no-ignore: Ignores ignore files (like .ignore), ensuring all files in the directory are processed.
    • -F: Used to treat the search string as a fixed string rather than a regular expression.