Guilt v0.37-rc1
[guilt.git] / guilt-import-commit
blobd438067a8b92ca74828d1fa5c70c3ed4bc9f0e9d
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2007-2013
6 USAGE="[<hash> | <since>..[<until>] | ..<until>]"
7 if [ -z "$GUILT_VERSION" ]; then
8 echo "Invoking `basename "$0"` directly is no longer supported." >&2
9 exit 1
12 _main() {
14 while [ $# -gt 0 ]; do
15 case "$1" in
16 -h|--help)
17 usage ;;
18 -*)
19 usage ;;
21 break
22 esac
23 done
25 if [ $# -ne 1 ] || [ -z "$1" ]; then
26 die "You must specify a range of commits"
29 rhash=`munge_hash_range $1`
31 # make sure that there are no unapplied changes
32 if ! must_commit_first; then
33 die "Uncommited changes detected. Refresh first."
36 disp "About to begin conversion..." >&2
37 disp "Current head: `git rev-parse \`git_branch\``" >&2
39 for rev in `git rev-list $rhash`; do
40 s=`git log --no-decorate --pretty=oneline -1 $rev | cut -c 42-`
42 # Try to convert the first line of the commit message to a
43 # valid patch name.
44 fname=`printf %s "$s" | sed -e "s/&/and/g" -e "s/[ :]/_/g" -e "s,[/\\],-,g" \
45 -e "s/['\\[{}<>(),$]//g" -e 's/]//g' -e 's/\*/-/g' \
46 -e 's/\?/-/g' -e 's/\.\.\.*/./g' -e 's/^\.//' \
47 -e 's/\.patch$//' -e 's/\.$//' | tr A-Z a-z`
49 if [ `expr length "$fname"` -gt 80 ] ; then
50 # Limit the patch length to about 80 chars. While this is
51 # arbitrary, we have to have a limit to avoid trying to
52 # create filenames that are too long for the filesystem to
53 # handle.
54 fname=`expr substr "$fname" 1 80`
57 if ! valid_patchname "$fname"; then
58 # Try harder to make it a legal commit name by
59 # removing all but a few safe characters.
60 fname=`echo $fname|tr -d -c _a-zA-Z0-9---/\\n`
62 if ! valid_patchname "$fname"; then
63 # If we failed to derive a legal patch name, use the
64 # name "x". (If this happens, we likely have to
65 # append a suffix to make the name unique.)
66 fname=x
69 disp "Converting `echo $rev | cut -c 1-8` as $fname"
71 mangle_prefix=1
72 fname_base=$fname
73 while [ -f "$GUILT_DIR/$branch/$fname.patch" ]; do
74 fname="$fname_base-$mangle_prefix"
75 mangle_prefix=`expr $mangle_prefix + 1`
76 disp "Patch under that name exists...trying '$fname'"
77 done
78 fname="$fname".patch
81 do_make_header $rev
82 echo ""
83 git diff --binary $rev^..$rev
84 ) > "$GUILT_DIR/$branch/$fname"
86 # FIXME: grab the GIT_AUTHOR_DATE from the commit object and set the
87 # timestamp on the patch
89 # insert the patch name into the series file
90 series_insert_patch $fname
92 # Only reset if the commit was on this branch
93 if head_check $rev 2> /dev/null; then
94 # BEWARE: "git reset" ahead! Is there a way to verify that
95 # we really created a patch? - We don't want to lose any
96 # history.
97 git reset --hard $rev^ > /dev/null
98 elif [ -z "$warned" ]; then
99 disp "Warning: commit $rev is not the HEAD...preserving commit" >&2
100 disp "Warning: (this message is displayed only once)" >&2
101 warned=t
103 done
105 disp "Done." >&2
106 disp "Current head: `git rev-parse \`git_branch\``" >&2