PythonUtilities

NoGit

A repetition free CLI for git, NoGit is a Python script that prevents unnecessary repetition of the "git" keyword.

Introduction

NoGit is a Python script that prevents the unnecessary repetition of the git keyword. It opens a shell CLI where you can input git commands without typing the git keyword before every instruction. I wrote this script as an answer to a question on StackOverflow.

After calling ./nogit, a CLI (similar to the MySQL shell) should appear:

git>

Note: Given that NoGit is a wrapper around Git, it provides all of the features of the Git VCS.

Documentation

Demonstration

  1. init
  2. add -A
  3. stage -A
  4. status
  5. commit -m "initial commit"
  6. %run; %exit

Installation

To run NoGit, you need Python 3 installed on your system. You can download it from the official Github Repository or copy the source code below.

Note: The script depends on the sys, os, signal, atexit, readline and subprocess modules.

Installation notes (Linux)

If you want you can remove the .py extension and convert it into an executable:

mv nogit.py nogit
chmod +x ./nogit
./nogit # open the NoGit CLI

You can also move this script to your ./bin/ directory and create an alias for it to run it without a ./:

sudo cp ./nogit /bin/nogit
sudo chmod +x /bin/nogit
alias nogit="/bin/nogit"

Alternatively you can copy the following command into your CLI:

sudo cp ./nogit /bin/nogit && sudo chmod +x /bin/nogit && alias nogit='/bin/nogit'

Source code

To install NoGit locally, copy and paste the following source code into a file named nogit.py.

#!/usr/bin/env python
import sys, os, signal, atexit, readline, subprocess

commands, stop, history_file = [], False, os.path.join(os.getcwd(), "nogit.history")

def run_commands():
  stop = True
  for cmd in commands:
    command = ["git" if not cmd.startswith("git ") else ""]
    command = [cmd] if command[0] == "" else [command[0], cmd]
    subprocess.Popen(command).communicate()
    commands = []

def signal_handler(sig, frame):
  run_commands()
  sys.exit(0)

try:
  readline.read_history_file(history_file)
  signal.signal(signal.SIGINT, signal_handler)
  while True:
    if stop == True:
      break
    command = input("git> ")
    if command == "%undo":
      commands.pop()
    elif command == "%run":
      run_commands()
    elif command == "%exit":
      sys.exit(0)
    else:
      commands += [cmd.strip() for cmd in command.split(";")]
  signal.pause()
  readline.set_history_length(-1)
except IOError:
  pass

atexit.register(readline.write_history_file, history_file)