Automated git status summary - lmmx/devnotes GitHub Wiki

Uses the regular format from the --porcelain flag to produce a string summarising git status.

function gnews		  {
	gitnews="$(git status --porcelain)";
	export gitnews;
	gitreport="$(python -c 'from os import environ as e; gnew=e["gitnews"].split("\n");\
	mods=[stat[3:] for stat in gnew if stat[0]=="M"];\
	adds=[stat[3:] for stat in gnew if stat[0]=="A"];\
	dels=[stat[3:] for stat in gnew if stat[0]=="D"];\
	rens=[stat[3:] for stat in gnew if stat[0]=="R"];\
	cops=[stat[3:] for stat in gnew if stat[0]=="C"];\
	upds=[stat[3:] for stat in gnew if stat[0]=="U"];\
	modreport=["Changed ".join(["",", ".join(statuslist)]) for statuslist in [mods] if len(mods)>0];\
	addreport=["Added ".join(["",", ".join(statuslist)]) for statuslist in [adds] if len(adds)>0];\
	delreport=["Deleted ".join(["",", ".join(statuslist)]) for statuslist in [dels] if len(dels)>0];\
	renreport=["Renamed ".join(["",", ".join(statuslist)]) for statuslist in [rens] if len(rens)>0];\
	copreport=["Copied ".join(["",", ".join(statuslist)]) for statuslist in [cops] if len(cops)>0];\
	updreport=["Updated ".join(["",", ".join(statuslist)]) for statuslist in [upds] if len(upds)>0];\
	report=[". ".join(stats) for stats in [modreport,addreport,delreport,renreport,copreport,updreport] if len(stats)>0];\
	print ". ".join(report)')";
	unset gitnews;
	echo "$gitreport";
}
function getgot		  {
	git add .;
	commitment="$(gnews)";
	git commit -m "$commitment";
	git push origin master;
}

gist

gnews

gnews reports the git status --porcelain on a single line - this output uses the same syntax as the short format and will remain stable in the future of git (see man pages). Python is run inline to parse the format neatly, but since regular indent-parsing can't be used, it's done with iterable lists (feel free to make an issue if you see a more concise way to write the above). The output is a 'report' string such as

  • Changed myfile.txt
  • Changed myfile.txt, myotherfile.txt
  • Changed myfile.txt. Added myotherfile.txt

and so on, for all possible variations of git-monitored activity. This is similar to the feature seen on GitHub, when modifying a file and allowing the web interface to make an automatic commit message.

getgot

This wrapper for gnews runs the git add command on the current directory, makes a commit with the gnews-generated report and pushes to origin master.

Disclaimer Odd function names are for ease of tab completion unique to my .bashrc and installed programs, feel free to rename for your own system :smile: