Created
May 15, 2013 08:34
-
-
Save aperezdc/5582469 to your computer and use it in GitHub Desktop.
Reads a list of patch file names from standard input and uses “git apply” to get them added as individual commits into a Git repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
set -e | |
declare -r NL=' | |
' | |
# extract_patch_info filename.patch | |
# | |
# Sets the PATCH_{AUTHOR,DATE,MESSAGE,GIT_ID} globals according to the | |
# information extracted from a given patch. Unknown fields are left | |
# empty. | |
# | |
extract_patch_info () | |
{ | |
PATCH_AUTHOR='' | |
PATCH_DATE='' | |
PATCH_MESSAGE='' | |
PATCH_GIT_ID='' | |
local -r S_HEADERS=1 | |
local -r S_MESSAGE=2 | |
local -r S_EMPTY=3 | |
local state=${S_HEADERS} | |
local line | |
while read -r line ; do | |
if [[ ${state} -eq ${S_HEADERS} ]] ; then | |
if [[ ${line} = commit\ * ]] ; then | |
read -r line PATCH_GIT_ID <<< "${line}" | |
elif [[ ${line} = Author:* ]] ; then | |
read -r line PATCH_AUTHOR <<< "${line}" | |
elif [[ ${line} = Date:* ]] ; then | |
read -r line PATCH_DATE <<< "${line}" | |
elif [[ ${line} = diff\ * ]] ; then | |
# Diff reached, no more info to extract | |
return | |
elif [[ ${line} =~ ^[[:space:]]*$ ]] ; then | |
# Empty line reached, start reading commit message | |
state=${S_MESSAGE} | |
fi | |
elif [[ ${state} -eq ${S_MESSAGE} ]] ; then | |
if [[ ${line} =~ ^[[:space:]]*$ ]] ; then | |
# Got an empty line | |
state=${S_EMPTY} | |
else | |
# Part of the message+ | |
PATCH_MESSAGE="${PATCH_MESSAGE}${line}${NL}" | |
fi | |
elif [[ ${state} -eq ${S_EMPTY} ]] ; then | |
if [[ ${line} = diff\ * ]] ; then | |
# Diff reached, no more info to extract | |
return | |
else | |
# Still reading part of the message | |
PATCH_MESSAGE="${PATCH_MESSAGE}${NL}${line}${NL}" | |
state=${S_MESSAGE} | |
fi | |
else | |
# XXX This is kind of an invalid state... bail out, maybe a warning | |
# should also be displayed, or maybe the entire operation aborted. | |
return | |
fi | |
done < "$1" | |
} | |
while read -r patch ; do | |
extract_patch_info "${patch}" | |
echo "[1;1m${patch} (${PATCH_GIT_ID})[0;0m" | |
git apply --index "${patch}" "$@" | |
git commit --author="${PATCH_AUTHOR}" \ | |
--date="${PATCH_DATE}" \ | |
--message="${PATCH_MESSAGE}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment