akaros/scripts/git/mbox-to-patches.sh
<<
>>
Prefs
   1#!/bin/bash
   2# Barret Rhoden (brho@cs.berkeley.edu)
   3# Copyright 2016 Google Inc
   4#
   5# Converts an mbox to a patch set.  It's like git am, but it generates patches
   6# instead of applying them to your tree.
   7#
   8# Mostly, it is just git mailsplit and mailinfo, but it also handles rewritten
   9# 'From' fields that Gmail or Google Groups seem to generate.
  10
  11PATCHDIR="${PATCHDIR:-../patches}"
  12
  13usage()
  14{
  15        echo "$0 <mbox>"
  16        exit -1
  17}
  18
  19if [ $# -ne 1 ]
  20then
  21        usage
  22fi
  23
  24MBOX=$1
  25
  26ls $PATCHDIR/*.patch 2>/dev/null
  27
  28if [ $? -eq 0 ]
  29then
  30        echo "$PATCHDIR has patches, remove and try again"
  31        exit -1
  32fi
  33
  34git mailsplit -o$PATCHDIR $MBOX > /dev/null
  35cd $PATCHDIR
  36for i in `ls`
  37do
  38        # Gmail rewrites some Froms.  We'll catch it and replace From with
  39        # X-Original-From
  40        ORIGFROM=`grep "^X-Original-From" $i`
  41        if [ $? -eq 0 ]
  42        then
  43                ORIGFROM=`echo $ORIGFROM | sed 's/^X-Original-From:/From:/g'`
  44                sed -i "/^From:.*/c$ORIGFROM" $i
  45        fi
  46
  47        git mailinfo MI_msg MI_patch < $i > MI_header
  48
  49        # We need a From: field, synthesized from Author and Email
  50        AUTHOR=`grep "^Author:" MI_header | cut -f 2- -d' '`
  51        EMAIL=`grep "^Email:" MI_header | cut -f 2- -d' '`
  52
  53        # Determine the subject for naming the patch, replace spaces and weird
  54        # chars
  55        SUBJECT=`grep "^Subject:" MI_header | cut -f 2- -d' ' |
  56                 sed 's/[^[:alnum:]]/-/g'`
  57
  58        echo "From: $AUTHOR <$EMAIL>" > $i-$SUBJECT.patch
  59        cat MI_header MI_msg MI_patch >> $i-$SUBJECT.patch
  60
  61        rm MI_header MI_msg MI_patch $i
  62done
  63cd - > /dev/null
  64