Skip to content

Commit 8698989

Browse files
committed
Update Changelog
And small coding improvements related to git_strarray
1 parent 34b6875 commit 8698989

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Upgrade to libgit2 v1.8.0
44

5+
- New `push_options` optional argument in `Repository.push(...)`
6+
[#1282](https://github.com/libgit2/pygit2/pull/1282)
7+
58
- Remove setuptools runtime dependency
69
[#1281](https://github.com/libgit2/pygit2/pull/1281)
710

pygit2/callbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def _git_checkout_options(
699699
if paths:
700700
strarray = StrArray(paths)
701701
refs.append(strarray)
702-
opts.paths = strarray.array[0]
702+
opts.paths = strarray.ptr[0]
703703

704704
# If we want to receive any notifications, set up notify_cb in the options
705705
notify_flags = payload.checkout_notify_flags()

pygit2/remotes.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from .errors import check_error
3434
from .ffi import ffi, C
3535
from .refspec import Refspec
36+
from . import utils
3637
from .utils import maybe_string, to_bytes, strarray_to_strings, StrArray
3738

3839
# Need BaseRepository for type hints, but don't let it cause a circular dependency
@@ -299,13 +300,10 @@ def __init__(self, repo: BaseRepository):
299300
self._repo = repo
300301

301302
def __len__(self):
302-
names = ffi.new('git_strarray *')
303-
try:
303+
with utils.new_git_strarray() as names:
304304
err = C.git_remote_list(names, self._repo._repo)
305305
check_error(err)
306306
return names.count
307-
finally:
308-
C.git_strarray_dispose(names)
309307

310308
def __iter__(self):
311309
cremote = ffi.new('git_remote **')
@@ -326,14 +324,11 @@ def __getitem__(self, name):
326324
return Remote(self._repo, cremote[0])
327325

328326
def _ffi_names(self):
329-
names = ffi.new('git_strarray *')
330-
try:
327+
with utils.new_git_strarray() as names:
331328
err = C.git_remote_list(names, self._repo._repo)
332329
check_error(err)
333330
for i in range(names.count):
334331
yield names.strings[i]
335-
finally:
336-
C.git_strarray_dispose(names)
337332

338333
def names(self):
339334
"""An iterator over the names of the available remotes."""

pygit2/repository.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from time import time
3030
import tarfile
3131
import typing
32-
import warnings
3332

3433
# Import from pygit2
3534
from ._pygit2 import Repository as _Repository, init_file_backend
@@ -61,7 +60,7 @@
6160
from .packbuilder import PackBuilder
6261
from .references import References
6362
from .remotes import RemoteCollection
64-
from .submodules import Submodule, SubmoduleCollection
63+
from .submodules import SubmoduleCollection
6564
from .utils import to_bytes, StrArray
6665

6766

@@ -1073,7 +1072,7 @@ def stash(
10731072

10741073
if paths:
10751074
arr = StrArray(paths)
1076-
opts.paths = arr.array[0]
1075+
opts.paths = arr.ptr[0]
10771076

10781077
coid = ffi.new('git_oid *')
10791078
err = C.git_stash_save_with_opts(coid, self._repo, opts)

pygit2/utils.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26+
import contextlib
2627
import os
2728

2829
# Import from pygit2
@@ -72,6 +73,13 @@ def ptr_to_bytes(ptr_cdata):
7273
return bytes(ffi.buffer(pp)[:])
7374

7475

76+
@contextlib.contextmanager
77+
def new_git_strarray():
78+
strarray = ffi.new('git_strarray *')
79+
yield strarray
80+
C.git_strarray_dispose(strarray)
81+
82+
7583
def strarray_to_strings(arr):
7684
"""
7785
Return a list of strings from a git_strarray pointer.
@@ -108,7 +116,7 @@ class StrArray:
108116
def __init__(self, l):
109117
# Allow passing in None as lg2 typically considers them the same as empty
110118
if l is None:
111-
self.array = ffi.NULL
119+
self.__array = ffi.NULL
112120
return
113121

114122
if not isinstance(l, (list, tuple)):
@@ -122,9 +130,9 @@ def __init__(self, l):
122130

123131
strings[i] = ffi.new('char []', to_bytes(li))
124132

125-
self._arr = ffi.new('char *[]', strings)
126-
self._strings = strings
127-
self.array = ffi.new('git_strarray *', [self._arr, len(strings)])
133+
self.__arr = ffi.new('char *[]', strings)
134+
self.__strings = strings
135+
self.__array = ffi.new('git_strarray *', [self.__arr, len(strings)])
128136

129137
def __enter__(self):
130138
return self
@@ -134,15 +142,15 @@ def __exit__(self, type, value, traceback):
134142

135143
@property
136144
def ptr(self):
137-
return self.array
145+
return self.__array
138146

139147
def assign_to(self, git_strarray):
140-
if self.array == ffi.NULL:
148+
if self.__array == ffi.NULL:
141149
git_strarray.strings = ffi.NULL
142150
git_strarray.count = 0
143151
else:
144-
git_strarray.strings = self._arr
145-
git_strarray.count = len(self._strings)
152+
git_strarray.strings = self.__arr
153+
git_strarray.count = len(self.__strings)
146154

147155

148156
class GenericIterator:

0 commit comments

Comments
 (0)