Skip to content

Automated git status summary

Louis edited this page Apr 25, 2015 · 8 revisions

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)')";
	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, and outputs a 'report' 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.

Clone this wiki locally