diff --git a/utils/git-whatsnew b/utils/git-whatsnew new file mode 100755 index 00000000..4a07018a --- /dev/null +++ b/utils/git-whatsnew @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import subprocess +import sendmail +import os +import argparse +import socket + +DEFAULTS = {"mail_from" : "root@crans.org", + "mail_to" : "root@crans.org", + "repo" : None} + +ADDITIONAL_HEADERS = {"X-Mailer" : "/usr/scripts/utils/git-whatsnew", + "X-CVSinfo" : "git", + "X-GitInfo" : "CRANS"} + +class GitError(Exception): + def __init__(self, msg): + Exception.__init__(self, msg) + + +def get_status(): + """Récupère le status du dépôt courant. Échoue si on n'est pas dans un dépôt git.""" + proc = subprocess.Popen(["git", "status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = proc.communicate() + if proc.returncode != 0: + raise GitError(err) + return out.decode("utf-8") + +def get_status_silentfail(): + """Récupère le status, mais ne renvoie rien si le ``git status`` est essentiellement vide.""" + out = get_status() + if not u"nothing to commit (working directory clean)" in out: + return out + +def get_repo(): + """Récupère le nom du dossier courant.""" + return os.getcwd() + +def send(out, mail_from, mail_to, debug=False): + """Envoie le résultat par mail.""" + repo = get_repo() + host = socket.gethostname() + ADDITIONAL_HEADERS["X-Git-Repository"] = "%s:%s" % (host, repo) + subject = u"[git] Whatsnew dans %s sur %s" % (repo, host) + sendmail.sendmail(mail_from, [mail_to], subject, out, more_headers=ADDITIONAL_HEADERS, debug=debug) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(usage="./git-whatsnew --to=") + parser.add_argument("-f", "--from", action="store", type=str, dest="mail_from", help="Expéditeur du mail") + parser.add_argument("-t", "--to", action="store", type=str, dest="mail_to", help="Destinataire du mail") + parser.add_argument("-r", "--repository", action="store", type=str, dest="repo", help="Path du dépôt") + parser.add_argument("-d", "--debug", action="store_true", dest="debug", help="Affiche le mail au lieu de l'envoyer") + parser.set_defaults(**DEFAULTS) + args = parser.parse_args() + # Si un path de dépôt est fourni, on s'y cd + if args.repo: + os.chdir(args.repo) + out = get_status_silentfail() + if out: + send(out, args.mail_from, args.mail_to, args.debug)