Skip to content

Instantly share code, notes, and snippets.

@Nurzzzone
Forked from aryelgois/git-ignore-line.sh
Created October 21, 2021 10:54
Show Gist options
  • Save Nurzzzone/4b19e370abb29e082eb3331cfdf7811f to your computer and use it in GitHub Desktop.
Save Nurzzzone/4b19e370abb29e082eb3331cfdf7811f to your computer and use it in GitHub Desktop.

Revisions

  1. @aryelgois aryelgois revised this gist Nov 3, 2019. 2 changed files with 87 additions and 42 deletions.
    87 changes: 87 additions & 0 deletions git-ignore-line.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    #!/bin/sh
    # Git filter to ignore lines in your files.
    #
    # Copyright (c) 2017-2019 Aryel Mota Góis <[email protected]>
    #
    # MIT License
    #
    #
    # SETUP:
    #
    # cp git-ignore-line.sh ~/bin/git-ignore-line
    #
    #
    # USAGE:
    #
    # Mark single lines you want to ignore with 'GITIGNORE'. It ignores
    # the whole line. It is recommended to be inside a comment in your code.
    #
    # Mark multiple lines surrounding them with 'GITIGNORE START' and
    # 'GITIGNORE END'. It can not be nested.
    #
    # NOTE: Ignored lines might be lost on checkout.
    #
    #
    # Add files to ignore:
    #
    # git ignore-line <pattern>
    #
    # Remove patterns with:
    #
    # git ignore-line -r <pattern>
    #
    # List configured patterns:
    #
    # git ignore-line -l
    #
    # PATTERN can be a file or a glob pattern: '*.html'. Remember to
    # escape the `*`.
    #
    #
    # TODO:
    #
    # - Stash the lines ignored by this filter before a git checkout.


    set -eu


    # Check if stdin is not tty and remove ignored lines from it.

    [ -t 0 ] || exec sed \
    '/GITIGNORE START/,/GITIGNORE END/d; /GITIGNORE/d' \
    /dev/stdin


    # Running from tty.

    program=$(basename "$0")

    # Find git repository.
    git_dir=$(git rev-parse --git-dir)

    # Path to attributes file.
    attr_file=$git_dir/info/attributes

    # Check arguments.
    if [ $# -eq 2 ] && [ "$1" = '-r' ]; then
    # Remove filter for pattern.
    sed "s|^$2 filter=$program||" "$attr_file" > "$attr_file.tmp"
    mv -- "$attr_file.tmp" "$attr_file"
    elif [ $# -eq 1 ]; then
    if [ "$1" = '-l' ]; then
    # List patterns.
    grep "filter=$program" "$attr_file" || true
    else
    # Add filter for pattern.
    echo "$1 filter=$program" >> "$attr_file"
    # Configure filter.
    git config --global "filter.$program.clean" "$program"
    git config --global "filter.$program.smudge" cat
    fi
    else
    # Show help.
    >&2 echo "Usage: $program [-r] <pattern>"
    >&2 echo " or: $program -l"
    exit 1
    fi
    42 changes: 0 additions & 42 deletions gitignore_line.php
    Original file line number Diff line number Diff line change
    @@ -1,42 +0,0 @@
    #!/usr/bin/env php
    <?php
    /**
    * Git filter to ignore lines in your files
    *
    * SETUP:
    *
    * git config --global filter.gitignore_line.clean /path/to/gitignore_line.php
    * git config --global filter.gitignore_line.smudge cat
    *
    * USAGE:
    *
    * 1. Add `[some file matching pattern] filter=gitignore_line` in .gitattributes
    *
    * 2. Mark single lines you want to ignore with 'GITIGNORE'
    * - case-sensitive
    * - ignores the whole line
    * - recommended to be in a comment for your code
    *
    * 3. Mark multiple lines between 'GITIGNORE START' and 'GITIGNORE END'
    * - can not be nested, matches first occurrences
    * - case-sensitive
    * - ignores all lines between the marker lines, inclusive
    * - recommended to be in comments for your code
    *
    * NOTE:
    * - Ignored lines will be lost on checkout
    * - Remember to chmod +x gitignore_line.php
    *
    * @todo Figure out a way to stash the lines ignored by this filter before
    * a git checkout
    *
    * @author Aryel Mota Góis
    * @license MIT
    */

    // remove multiline and inline gitignore from stdin
    echo preg_replace(
    '/[^\n]*?(GITIGNORE START.*?GITIGNORE END|GITIGNORE)[^\n]*\n/s',
    '',
    stream_get_contents(fopen('php://stdin', 'r'))
    );
  2. @aryelgois aryelgois revised this gist Mar 6, 2018. 1 changed file with 0 additions and 0 deletions.
    Empty file modified gitignore_line.php
    100644 → 100755
    Empty file.
  3. @aryelgois aryelgois revised this gist Mar 6, 2018. 1 changed file with 8 additions and 15 deletions.
    23 changes: 8 additions & 15 deletions gitignore_line.php
    Original file line number Diff line number Diff line change
    @@ -18,11 +18,9 @@
    * - recommended to be in a comment for your code
    *
    * 3. Mark multiple lines between 'GITIGNORE START' and 'GITIGNORE END'
    * - can not be nested
    * - can not be nested, matches first occurrences
    * - case-sensitive
    * - ignores all lines between the marker lines
    * - ignores line with GITIGNORE START
    * - ignores until END
    * - ignores all lines between the marker lines, inclusive
    * - recommended to be in comments for your code
    *
    * NOTE:
    @@ -36,14 +34,9 @@
    * @license MIT
    */

    // read stdin
    $file = stream_get_contents(fopen('php://stdin', 'r'));

    // remove multiline gitignore
    $clean = preg_replace('/\n[^\n]*?GITIGNORE START.*?GITIGNORE END/s', '', $file);

    // remove inline gitignore
    $clean = preg_replace('/\n.*GITIGNORE.*/', '', $clean);

    // output
    echo $clean;
    // remove multiline and inline gitignore from stdin
    echo preg_replace(
    '/[^\n]*?(GITIGNORE START.*?GITIGNORE END|GITIGNORE)[^\n]*\n/s',
    '',
    stream_get_contents(fopen('php://stdin', 'r'))
    );
  4. @aryelgois aryelgois revised this gist Mar 6, 2018. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions gitignore_line.php
    Original file line number Diff line number Diff line change
    @@ -15,20 +15,19 @@
    * 2. Mark single lines you want to ignore with 'GITIGNORE'
    * - case-sensitive
    * - ignores the whole line
    * - recommended to be in a comment for your language
    * - recommended to be in a comment for your code
    *
    * 3. Mark multiple lines you want to ignore with a preceding 'GITIGNORE START'
    * and a following 'GITIGNORE END'
    * 3. Mark multiple lines between 'GITIGNORE START' and 'GITIGNORE END'
    * - can not be nested
    * - case-sensitive
    * - ignores all lines between the marker lines
    * - ignores line with GITIGNORE START
    * - ignores until END
    * - recommended to be in a comment for your language
    * - recommended to be in comments for your code
    *
    * NOTE:
    * - Ignored lines will be lost on checkout
    * - Remember to chmod a+x gitignore_line.php
    * - Remember to chmod +x gitignore_line.php
    *
    * @todo Figure out a way to stash the lines ignored by this filter before
    * a git checkout
  5. @aryelgois aryelgois created this gist Nov 15, 2017.
    50 changes: 50 additions & 0 deletions gitignore_line.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/usr/bin/env php
    <?php
    /**
    * Git filter to ignore lines in your files
    *
    * SETUP:
    *
    * git config --global filter.gitignore_line.clean /path/to/gitignore_line.php
    * git config --global filter.gitignore_line.smudge cat
    *
    * USAGE:
    *
    * 1. Add `[some file matching pattern] filter=gitignore_line` in .gitattributes
    *
    * 2. Mark single lines you want to ignore with 'GITIGNORE'
    * - case-sensitive
    * - ignores the whole line
    * - recommended to be in a comment for your language
    *
    * 3. Mark multiple lines you want to ignore with a preceding 'GITIGNORE START'
    * and a following 'GITIGNORE END'
    * - can not be nested
    * - case-sensitive
    * - ignores all lines between the marker lines
    * - ignores line with GITIGNORE START
    * - ignores until END
    * - recommended to be in a comment for your language
    *
    * NOTE:
    * - Ignored lines will be lost on checkout
    * - Remember to chmod a+x gitignore_line.php
    *
    * @todo Figure out a way to stash the lines ignored by this filter before
    * a git checkout
    *
    * @author Aryel Mota Góis
    * @license MIT
    */

    // read stdin
    $file = stream_get_contents(fopen('php://stdin', 'r'));

    // remove multiline gitignore
    $clean = preg_replace('/\n[^\n]*?GITIGNORE START.*?GITIGNORE END/s', '', $file);

    // remove inline gitignore
    $clean = preg_replace('/\n.*GITIGNORE.*/', '', $clean);

    // output
    echo $clean;