1

If I need some extra code to run something on my local server but need to remember to remove it before pushing to git or merging into protected branches.

Is there a piece of code (or vscode extension) that can notify git or notify me while pushing if I forgot to remove that code. Like //TODO: or something that git can recognize?

Lennert Hofman
  • 585
  • 6
  • 21
  • 2
    yes you can use static code analysis / a lint tool, but without knowing what language you are working with it's hard to give any further advice. You can set up rules in your linter and use a pre-push hook to run the tool – andy mccullough May 07 '20 at 09:38
  • @andymccullough, I'm using php mostly – Lennert Hofman May 07 '20 at 09:48
  • have a look at https://stackoverflow.com/questions/378959/how-to-perform-static-code-analysis-in-php – andy mccullough May 07 '20 at 09:53
  • I was not really looking for code smell checkers, but you did point me in the right direction. I can set up a simple git hook to check for "//TODO: remove code" – Lennert Hofman May 07 '20 at 10:18

2 Answers2

1

On the git side : you can add a pre-push hook in your local repo to scan for a forbidden word in your code, and cancel the push if word is found.

Here is for example some code that looks for dontpush (it uses git grep -i, any casing of "DoNtPuSh" will work) and will not push if it is found in your local commit :

# file '.git/hooks/pre-push' :
#!/bin/bash

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Delete: nothing to do on deleted branches
    else
        # Check if "dontpush" appears in the local commit :
        dontpush=`git grep -n -i dontpush $local_sha -- $files`
        if [ -n "$dontpush" ]
        then
            # print 'git grep' output on STDERR,
            # remove the leading "sha:" on each line
            (echo $dontpush | sed -e 's/^[^:]*://') | cat >&2
            echo >&2 "*** Found 'dontpush' tag in $local_ref, not pushing"
            exit 1
        fi
    fi
done

exit 0
LeGEC
  • 46,477
  • 5
  • 57
  • 104
0
  1. You can work on separate branch and merge later with analysing the changes and remove what not necessary.
  2. You can use git in VS code when ever you click on the file to be staged you can easily see the changes which you had done in the code, so using this u can see that extra part as well which u wanna remove.
raven404
  • 1,039
  • 9
  • 14