Skip to content

Commit 82d8819

Browse files
committed
credentials: use more ducks
Instead of making everyone inherit from our credential types, use an interface with two attributes, which makes the C code much shorter and simpler.
1 parent 77acb11 commit 82d8819

File tree

7 files changed

+69
-283
lines changed

7 files changed

+69
-283
lines changed

pygit2/credentials.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,52 @@
2626
# Boston, MA 02110-1301, USA.
2727

2828
# Import from pygit2
29-
from _pygit2 import CredUsernamePassword, CredSshKey
30-
from _pygit2 import GIT_CREDTYPE_USERPASS_PLAINTEXT
29+
from _pygit2 import GIT_CREDTYPE_USERPASS_PLAINTEXT, GIT_CREDTYPE_SSH_KEY
3130

32-
class UserPass(CredUsernamePassword):
31+
class UserPass:
3332
"""Username/Password credentials
3433
3534
This is an object suitable for passing to a remote's credentials
3635
callback and for returning from said callback.
3736
3837
"""
3938

39+
def __init__(self, username, password):
40+
self._username = username
41+
self._password = password
42+
43+
@property
44+
def credential_type(self):
45+
return GIT_CREDTYPE_USERPASS_PLAINTEXT
46+
47+
@property
48+
def credential_tuple(self):
49+
return (self._username, self._password)
50+
4051
def __call__(self, _url, _username, _allowed):
4152
return self
4253

43-
class Keypair(CredSshKey):
54+
class Keypair:
4455
"""SSH key pair credentials
4556
4657
This is an object suitable for passing to a remote's credentials
4758
callback and for returning from said callback.
4859
4960
"""
5061

62+
def __init__(self, username, pubkey, privkey, passphrase):
63+
self._username = username
64+
self._pubkey = pubkey
65+
self._privkey = privkey
66+
self._passphrase = passphrase
67+
68+
@property
69+
def credential_type(self):
70+
return GIT_CREDTYPE_SSH_KEY
71+
72+
@property
73+
def credential_tuple(self):
74+
return (self._username, self._pubkey, self._privkey, self._passphrase)
75+
5176
def __call__(self, _url, _username, _allowed):
5277
return self

src/credentials.c

-225
This file was deleted.

src/pygit2.c

-6
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ extern PyTypeObject RefLogEntryType;
7474
extern PyTypeObject BranchType;
7575
extern PyTypeObject SignatureType;
7676
extern PyTypeObject RemoteType;
77-
extern PyTypeObject CredUsernamePasswordType;
78-
extern PyTypeObject CredSshKeyType;
7977
extern PyTypeObject RefspecType;
8078
extern PyTypeObject TransferProgressType;
8179
extern PyTypeObject NoteType;
@@ -460,13 +458,9 @@ moduleinit(PyObject* m)
460458

461459
/* Remotes */
462460
INIT_TYPE(RemoteType, NULL, NULL)
463-
INIT_TYPE(CredUsernamePasswordType, NULL, PyType_GenericNew)
464-
INIT_TYPE(CredSshKeyType, NULL, PyType_GenericNew)
465461
INIT_TYPE(RefspecType, NULL, NULL)
466462
INIT_TYPE(TransferProgressType, NULL, NULL)
467463
ADD_TYPE(m, Remote)
468-
ADD_TYPE(m, CredUsernamePassword)
469-
ADD_TYPE(m, CredSshKey)
470464
ADD_TYPE(m, Refspec)
471465
ADD_TYPE(m, TransferProgress)
472466
/* Direction for the refspec */

src/remote.c

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
extern PyObject *GitError;
4040
extern PyTypeObject RepositoryType;
4141
extern PyTypeObject TransferProgressType;
42-
extern PyTypeObject CredUsernamePasswordType;
43-
extern PyTypeObject CredSshKeyType;
4442

4543
PyObject *
4644
wrap_transfer_progress(const git_transfer_progress *stats)

src/types.h

-19
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,6 @@ typedef struct {
207207
PyObject *update_tips;
208208
} Remote;
209209

210-
typedef struct {
211-
PyObject_HEAD
212-
git_credtype_t credtype;
213-
} Cred;
214-
215-
typedef struct {
216-
Cred parent;
217-
char *username;
218-
char *password;
219-
} CredUsernamePassword;
220-
221-
typedef struct {
222-
Cred parent;
223-
char *username;
224-
char *pubkey;
225-
char *privkey;
226-
char *passphrase;
227-
} CredSshKey;
228-
229210
/* git_refspec */
230211
typedef struct {
231212
PyObject_HEAD

0 commit comments

Comments
 (0)