Skip to content

Commit b47dfe9

Browse files
committed
git-am: add --directory=<dir> option
Thanks to a200337 (git-am: propagate -C<n>, -p<n> options as well, 2008-12-04) and commits around it, "git am" is equipped to correctly propagate the command line flags such as -C/-p/-whitespace across a patch failure and restart. It is trivial to support --directory option now, resurrecting previous attempts by Kevin and Simon. Signed-off-by: Junio C Hamano <[email protected]>
1 parent d727f67 commit b47dfe9

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

Documentation/git-am.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git am' [--signoff] [--keep] [--utf8 | --no-utf8]
1313
[--3way] [--interactive]
14-
[--whitespace=<option>] [-C<n>] [-p<n>]
14+
[--whitespace=<option>] [-C<n>] [-p<n>] [--directory=<dir>]
1515
[<mbox> | <Maildir>...]
1616
'git am' (--skip | --resolved | --abort)
1717

@@ -60,12 +60,9 @@ default. You could use `--no-utf8` to override this.
6060
available locally.
6161

6262
--whitespace=<option>::
63-
This flag is passed to the 'git-apply' (see linkgit:git-apply[1])
64-
program that applies
65-
the patch.
66-
6763
-C<n>::
6864
-p<n>::
65+
--directory=<dir>::
6966
These flags are passed to the 'git-apply' (see linkgit:git-apply[1])
7067
program that applies
7168
the patch.

git-am.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ s,signoff add a Signed-off-by line to the commit message
1616
u,utf8 recode into utf8 (default)
1717
k,keep pass -k flag to git-mailinfo
1818
whitespace= pass it through git-apply
19+
directory= pass it through git-apply
1920
C= pass it through git-apply
2021
p= pass it through git-apply
2122
resolvemsg= override error message when patch failure occurs
@@ -33,6 +34,14 @@ cd_to_toplevel
3334
git var GIT_COMMITTER_IDENT >/dev/null ||
3435
die "You need to set your committer info first"
3536

37+
sq () {
38+
for sqarg
39+
do
40+
printf "%s" "$sqarg" |
41+
sed -e 's/'\''/'\''\'\'''\''/g' -e 's/.*/ '\''&'\''/'
42+
done
43+
}
44+
3645
stop_here () {
3746
echo "$1" >"$dotest/next"
3847
exit 1
@@ -155,10 +164,10 @@ do
155164
;;
156165
--resolvemsg)
157166
shift; resolvemsg=$1 ;;
158-
--whitespace)
159-
git_apply_opt="$git_apply_opt $1=$2"; shift ;;
167+
--whitespace|--directory)
168+
git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
160169
-C|-p)
161-
git_apply_opt="$git_apply_opt $1$2"; shift ;;
170+
git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
162171
--)
163172
shift; break ;;
164173
*)
@@ -459,7 +468,7 @@ do
459468

460469
case "$resolved" in
461470
'')
462-
git apply $git_apply_opt --index "$dotest/patch"
471+
eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
463472
apply_status=$?
464473
;;
465474
t)

t/t4252-am-options.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@ test_expect_success 'interrupted am -C1 -p2' '
5050
grep "^Three$" file-2
5151
'
5252

53+
test_expect_success 'interrupted am --directory="frotz nitfol"' '
54+
rm -rf .git/rebase-apply &&
55+
git reset --hard initial &&
56+
test_must_fail git am --directory="frotz nitfol" "$tm"/am-test-5-? &&
57+
git am --skip &&
58+
grep One "frotz nitfol/file-5"
59+
'
60+
5361
test_done

t/t4252/am-test-5-1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From: A U Thor <[email protected]>
2+
Date: Thu Dec 4 16:00:00 2008 -0800
3+
Subject: Six
4+
5+
Applying this patch with --directory='frotz nitfol' should fail
6+
7+
diff --git i/junk/file-2 w/junk/file-2
8+
index 06e567b..b6f3a16 100644
9+
--- i/junk/file-2
10+
+++ w/junk/file-2
11+
@@ -1,7 +1,7 @@
12+
One
13+
2
14+
-3
15+
+Three
16+
4
17+
5
18+
-6
19+
+Six
20+
7

t/t4252/am-test-5-2

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
From: A U Thor <[email protected]>
2+
Date: Thu Dec 4 16:00:00 2008 -0800
3+
Subject: Six
4+
5+
Applying this patch with --directory='frotz nitfol' should succeed
6+
7+
diff --git i/file-5 w/file-5
8+
new file mode 100644
9+
index 000000..1d6ed9f
10+
--- /dev/null
11+
+++ w/file-5
12+
@@ -0,0 +1,3 @@
13+
+One
14+
+two
15+
+three

0 commit comments

Comments
 (0)