Skip to content

Instantly share code, notes, and snippets.

@scottrogowski
Created March 26, 2015 16:08
Show Gist options
  • Save scottrogowski/6f855fb25d2660d54aa0 to your computer and use it in GitHub Desktop.
Save scottrogowski/6f855fb25d2660d54aa0 to your computer and use it in GitHub Desktop.

Revisions

  1. scottrogowski created this gist Mar 26, 2015.
    53 changes: 53 additions & 0 deletions safepush
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env python

    # Do some cursory checks before pushing the branch to `origin <branch_name>`
    # Checks that no debuggers, TODOs, or print statements are in the branch diff before pushing
    # Also checks that there are no saved but uncommitted files in git status

    import subprocess
    import sys

    def execute(cmd):
    return subprocess.check_output(cmd.split(' ')).strip()

    BAD_STRS = ['pdb', 'TODO', 'pprint', 'console.log', 'debugger']

    if __name__ == "__main__":
    lca = execute("git merge-base HEAD upstream/master")
    diff = execute("git diff %s" % lca)
    diff = diff.split('\n')
    diff = filter(lambda line: line.startswith('+'), diff)

    loose_statements = []
    for i, line in enumerate(diff):
    line = line[1:].strip()
    if ( line.startswith('print') or
    any(s in line for s in BAD_STRS)):
    while not diff[i].startswith("+++"):
    i -= 1
    filename = diff[i].split('/')[-1]
    loose_statements += [filename, line, '']

    if loose_statements:
    print "LOOSE STATEMENTS:"
    for s in loose_statements:
    print s
    print "continue?"
    if raw_input() != 'yes':
    exit()

    status = execute("git status")
    if 'modified' in status:
    print "non-empty git status"
    print "continue?"
    if raw_input() != 'yes':
    exit()

    print "Looks safe (I think...) pushing it"
    branch = execute("git rev-parse --abbrev-ref HEAD")

    push_cmd = "git push origin %s" % branch
    if len(sys.argv) > 1 and sys.argv[1] == '-f':
    push_cmd += " -f"
    print push_cmd
    print execute(push_cmd)