Skip to content

Commit 89c7515

Browse files
committed
Rename flag to flags in Blob's diff/diff_to_buffer
Also now Tree.diff_to_workdir accepts keyword arguments.
1 parent 19e614f commit 89c7515

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

CHANGELOG.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1.17.1 (UNRELEASED)
1+
# 1.18.0 (UNRELEASED)
22

33
- Upgrade Linux Glibc wheels to `manylinux_2_28`
44

@@ -17,6 +17,12 @@
1717
- New `proxy` optional argument in `clone_repository(...)`
1818
[#1354](https://github.com/libgit2/pygit2/pull/1354)
1919

20+
- New optional arguments `context_lines` and `interhunk_lines` in `Blob.diff(...)` ; and
21+
now `Repository.diff(...)` honors these two arguments when the objects diffed are blobs.
22+
[#1360](https://github.com/libgit2/pygit2/pull/1360)
23+
24+
- Now `Tree.diff_to_workdir(...)` accepts keyword arguments, not just positional.
25+
2026
- Fix when a reference name has non UTF-8 chars
2127
[#1329](https://github.com/libgit2/pygit2/pull/1329)
2228

@@ -34,12 +40,20 @@
3440
- CI: Use ARM runner for tests and wheels
3541
[#1346](https://github.com/libgit2/pygit2/pull/1346)
3642

43+
- Build and CI updates
44+
[#1363](https://github.com/libgit2/pygit2/pull/1363)
45+
3746
Deprecations:
3847

3948
- Passing str to `Repository.merge(...)` is deprecated,
4049
instead pass an oid object (or a commit, or a reference)
4150
[#1349](https://github.com/libgit2/pygit2/pull/1349)
4251

52+
Breaking changes:
53+
54+
- Keyword argument `flag` has been renamed to `flags` in `Blob.diff(...)` and
55+
`Blob.diff_to_buffer(...)`
56+
4357

4458
# 1.17.0 (2025-01-08)
4559

pygit2/repository.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -521,29 +521,30 @@ def diff(
521521
a = self.__whatever_to_tree_or_blob(a)
522522
b = self.__whatever_to_tree_or_blob(b)
523523

524-
opt_keys = ['flags', 'context_lines', 'interhunk_lines']
525-
opt_values = [int(flags), context_lines, interhunk_lines]
524+
options = {
525+
'flags': int(flags),
526+
'context_lines': context_lines,
527+
'interhunk_lines': interhunk_lines,
528+
}
526529

527530
# Case 1: Diff tree to tree
528531
if isinstance(a, Tree) and isinstance(b, Tree):
529-
return a.diff_to_tree(b, **dict(zip(opt_keys, opt_values)))
532+
return a.diff_to_tree(b, **options)
530533

531534
# Case 2: Index to workdir
532535
elif a is None and b is None:
533-
return self.index.diff_to_workdir(*opt_values)
536+
return self.index.diff_to_workdir(**options)
534537

535538
# Case 3: Diff tree to index or workdir
536539
elif isinstance(a, Tree) and b is None:
537540
if cached:
538-
return a.diff_to_index(self.index, *opt_values)
541+
return a.diff_to_index(self.index, **options)
539542
else:
540-
return a.diff_to_workdir(*opt_values)
543+
return a.diff_to_workdir(**options)
541544

542545
# Case 4: Diff blob to blob
543546
if isinstance(a, Blob) and isinstance(b, Blob):
544-
opt_values.insert(1, 'file')
545-
opt_values.insert(1, 'file')
546-
return a.diff(b, *opt_values)
547+
return a.diff(b, **options)
547548

548549
raise ValueError('Only blobs and treeish can be diffed')
549550

src/blob.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern PyObject *GitError;
4141
extern PyTypeObject BlobType;
4242

4343
PyDoc_STRVAR(Blob_diff__doc__,
44-
"diff([blob: Blob, flag: int = GIT_DIFF_NORMAL, old_as_path: str, new_as_path: str]) -> Patch\n"
44+
"diff([blob: Blob, flags: int = GIT_DIFF_NORMAL, old_as_path: str, new_as_path: str]) -> Patch\n"
4545
"\n"
4646
"Directly generate a :py:class:`pygit2.Patch` from the difference\n"
4747
"between two blobs.\n"
@@ -53,8 +53,8 @@ PyDoc_STRVAR(Blob_diff__doc__,
5353
"blob : Blob\n"
5454
" The :py:class:`~pygit2.Blob` to diff.\n"
5555
"\n"
56-
"flag\n"
57-
" A GIT_DIFF_* constant.\n"
56+
"flags\n"
57+
" A combination of GIT_DIFF_* constant.\n"
5858
"\n"
5959
"old_as_path : str\n"
6060
" Treat old blob as if it had this filename.\n"
@@ -78,7 +78,7 @@ Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
7878
char *old_as_path = NULL, *new_as_path = NULL;
7979
Blob *other = NULL;
8080
int err;
81-
char *keywords[] = {"blob", "flag", "old_as_path", "new_as_path", "context_lines", "interhunk_lines", NULL};
81+
char *keywords[] = {"blob", "flags", "old_as_path", "new_as_path", "context_lines", "interhunk_lines", NULL};
8282

8383
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!IssHH", keywords,
8484
&BlobType, &other, &opts.flags,
@@ -100,7 +100,7 @@ Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
100100

101101

102102
PyDoc_STRVAR(Blob_diff_to_buffer__doc__,
103-
"diff_to_buffer(buffer: bytes = None, flag: int = GIT_DIFF_NORMAL[, old_as_path: str, buffer_as_path: str]) -> Patch\n"
103+
"diff_to_buffer(buffer: bytes = None, flags: int = GIT_DIFF_NORMAL[, old_as_path: str, buffer_as_path: str]) -> Patch\n"
104104
"\n"
105105
"Directly generate a :py:class:`~pygit2.Patch` from the difference\n"
106106
"between a blob and a buffer.\n"
@@ -112,8 +112,8 @@ PyDoc_STRVAR(Blob_diff_to_buffer__doc__,
112112
"buffer : bytes\n"
113113
" Raw data for new side of diff.\n"
114114
"\n"
115-
"flag\n"
116-
" A GIT_DIFF_* constant.\n"
115+
"flags\n"
116+
" A combination of GIT_DIFF_* constants.\n"
117117
"\n"
118118
"old_as_path : str\n"
119119
" Treat old blob as if it had this filename.\n"
@@ -130,8 +130,7 @@ Blob_diff_to_buffer(Blob *self, PyObject *args, PyObject *kwds)
130130
const char *buffer = NULL;
131131
Py_ssize_t buffer_len;
132132
int err;
133-
char *keywords[] = {"buffer", "flag", "old_as_path", "buffer_as_path",
134-
NULL};
133+
char *keywords[] = {"buffer", "flags", "old_as_path", "buffer_as_path", NULL};
135134

136135
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|z#Iss", keywords,
137136
&buffer, &buffer_len, &opts.flags,

src/tree.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,16 @@ PyDoc_STRVAR(Tree_diff_to_workdir__doc__,
222222
" the hunks will be merged into a one.\n");
223223

224224
PyObject *
225-
Tree_diff_to_workdir(Tree *self, PyObject *args)
225+
Tree_diff_to_workdir(Tree *self, PyObject *args, PyObject *kwds)
226226
{
227227
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
228228
git_diff *diff;
229229
int err;
230230

231-
if (!PyArg_ParseTuple(args, "|IHH", &opts.flags, &opts.context_lines,
232-
&opts.interhunk_lines))
231+
char *keywords[] = {"flags", "context_lines", "interhunk_lines", NULL};
232+
233+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|IHH", keywords, &opts.flags,
234+
&opts.context_lines, &opts.interhunk_lines))
233235
return NULL;
234236

235237
if (Object__load((Object*)self) == NULL) { return NULL; } // Lazy load
@@ -354,8 +356,7 @@ Tree_diff_to_tree(Tree *self, PyObject *args, PyObject *kwds)
354356
git_diff *diff;
355357
git_tree *from, *to = NULL, *tmp;
356358
int err, swap = 0;
357-
char *keywords[] = {"obj", "flags", "context_lines", "interhunk_lines",
358-
"swap", NULL};
359+
char *keywords[] = {"obj", "flags", "context_lines", "interhunk_lines", "swap", NULL};
359360

360361
Tree *other = NULL;
361362

@@ -406,7 +407,7 @@ PyMappingMethods Tree_as_mapping = {
406407

407408
PyMethodDef Tree_methods[] = {
408409
METHOD(Tree, diff_to_tree, METH_VARARGS | METH_KEYWORDS),
409-
METHOD(Tree, diff_to_workdir, METH_VARARGS),
410+
METHOD(Tree, diff_to_workdir, METH_VARARGS | METH_KEYWORDS),
410411
METHOD(Tree, diff_to_index, METH_VARARGS | METH_KEYWORDS),
411412
{NULL}
412413
};

0 commit comments

Comments
 (0)