Skip to content

Commit 51ffa0b

Browse files
committed
Add -C option to stg import and stg fold
This option maps to the -C option of `git apply` to select the amount of surrounding context to ensure when an external patch is applied. Resolves #18. Signed-off-by: Peter Grayson <[email protected]>
1 parent 4a64477 commit 51ffa0b

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

stgit/commands/common.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def address_or_alias(addr_pair):
230230
raise CmdException('unknown e-mail alias: %s' % addr)
231231

232232

233-
def apply_patch(stack, diff, base=None, reject=False, strip=None):
233+
def apply_patch(stack, diff, base=None, reject=False, strip=None, context_lines=None):
234234
iw = stack.repository.default_iw
235235
iw.refresh_index()
236236
if base:
@@ -239,7 +239,9 @@ def apply_patch(stack, diff, base=None, reject=False, strip=None):
239239
stack.set_head(base, msg='apply patch')
240240

241241
try:
242-
iw.apply(diff, quiet=False, reject=reject, strip=strip)
242+
iw.apply(
243+
diff, quiet=False, reject=reject, strip=strip, context_lines=context_lines
244+
)
243245
except MergeException:
244246
if base:
245247
iw.checkout_hard(orig_head.data.tree)

stgit/commands/fold.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
metavar='N',
6363
short='Remove N leading slashes from diff paths (default 1)',
6464
),
65+
opt(
66+
'-C',
67+
dest='context_lines',
68+
type='int',
69+
metavar='N',
70+
short='Ensure N lines of surrounding context for each change',
71+
),
6572
opt(
6673
'--reject',
6774
action='store_true',
@@ -113,6 +120,7 @@ def func(parser, options, args):
113120
base=stack.patches[current].data.parent,
114121
strip=options.strip,
115122
reject=options.reject,
123+
context_lines=options.context_lines,
116124
)
117125
elif options.base:
118126
apply_patch(
@@ -121,13 +129,15 @@ def func(parser, options, args):
121129
base=git_commit(options.base, repository),
122130
reject=options.reject,
123131
strip=options.strip,
132+
context_lines=options.context_lines,
124133
)
125134
else:
126135
apply_patch(
127136
stack,
128137
diff,
129138
strip=options.strip,
130139
reject=options.reject,
140+
context_lines=options.context_lines,
131141
)
132142

133143
out.done()

stgit/commands/imprt.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@
102102
action='store_true',
103103
short='Strip numbering and extension from patch name',
104104
),
105+
opt(
106+
'-C',
107+
dest='context_lines',
108+
type='int',
109+
metavar='N',
110+
short='Ensure N lines of surrounding context for each change',
111+
),
105112
opt(
106113
'-i',
107114
'--ignore',
@@ -203,7 +210,13 @@ def __create_patch(
203210
tree = stack.head.data.tree
204211
else:
205212
iw = stack.repository.default_iw
206-
iw.apply(diff, quiet=False, reject=options.reject, strip=options.strip)
213+
iw.apply(
214+
diff,
215+
quiet=False,
216+
reject=options.reject,
217+
strip=options.strip,
218+
context_lines=options.context_lines,
219+
)
207220
tree = iw.index.write_tree()
208221

209222
cd = CommitData(

stgit/lib/git/iw.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,15 @@ def diff(self, tree, diff_opts=(), pathlimits=(), binary=True, stat=False):
302302
cmd.extend(pathlimits)
303303
return self.run(cmd).decoding(None).raw_output()
304304

305-
def apply(self, patch_bytes, quiet, reject=False, strip=None):
305+
def apply(self, patch_bytes, quiet, reject=False, strip=None, context_lines=None):
306306
"""Apply patch to worktree."""
307307
cmd = ['git', 'apply', '--index']
308308
if reject:
309309
cmd.append('--reject')
310310
if strip is not None:
311311
cmd.append('-p%s' % (strip,))
312+
if context_lines is not None:
313+
cmd.append('-C%s' % (context_lines,))
312314
try:
313315
r = self.run(cmd).encoding(None).raw_input(patch_bytes)
314316
if quiet:

t/t1800-import.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ test_expect_success \
206206
--authemail "[email protected]" \
207207
--authdate 2005-04-07T22:13:13 \
208208
--stripname \
209+
-C5 \
209210
some.patch &&
210211
stg show | grep -e "Author: Some Author <[email protected]>" &&
211212
stg show | grep -E "Date: +Thu Apr 7 22:13:13 2005 \+0000" &&

t/t3600-fold.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ test_expect_success 'Attempt to fold conflicting patch with rejects' '
9393
rm foo.txt.rej
9494
'
9595

96+
test_expect_success 'Attempt to fold conflicting patch with -C0' '
97+
stg fold -C0 --reject fold1.diff &&
98+
stg status --porcelain foo.txt | grep -e "M foo.txt" &&
99+
test "$(tail -n 1 foo.txt)" = "and fold1" &&
100+
git reset -- foo.txt &&
101+
git checkout foo.txt
102+
'
103+
96104
test_expect_success 'Fold with base' '
97105
stg fold --base p1 threeway.diff &&
98106
test "preface hello from p2" = "$(echo $(cat foo.txt))" &&

0 commit comments

Comments
 (0)