Skip to content

Commit 1af76c3

Browse files
committed
Support comparison of Oid with text string
1 parent d210edf commit 1af76c3

23 files changed

+141
-151
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- New `push_options` optional argument in `Repository.push(...)`
66
[#1282](https://github.com/libgit2/pygit2/pull/1282)
77

8+
- New support comparison of `Oid` with text string
9+
810
- Remove setuptools runtime dependency
911
[#1281](https://github.com/libgit2/pygit2/pull/1281)
1012

src/oid.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,29 @@ Oid_hash(PyObject *oid)
198198

199199

200200
PyObject *
201-
Oid_richcompare(PyObject *o1, PyObject *o2, int op)
201+
Oid_richcompare(PyObject *self, PyObject *other, int op)
202202
{
203-
PyObject *res;
203+
git_oid *oid = &((Oid*)self)->oid;
204204
int cmp;
205205

206-
/* Comparing to something else than an Oid is not supported. */
207-
if (!PyObject_TypeCheck(o2, &OidType)) {
206+
// Can compare an oid against another oid or a unicode string
207+
if (PyObject_TypeCheck(other, &OidType)) {
208+
cmp = git_oid_cmp(oid, &((Oid*)other)->oid);
209+
}
210+
else if (PyObject_TypeCheck(other, &PyUnicode_Type)) {
211+
const char * str = PyUnicode_AsUTF8(other);
212+
if (str == NULL) {
213+
return NULL;
214+
}
215+
cmp = git_oid_strcmp(oid, str);
216+
}
217+
else {
208218
Py_INCREF(Py_NotImplemented);
209219
return Py_NotImplemented;
210220
}
211221

212-
/* Ok go. */
213-
cmp = git_oid_cmp(&((Oid*)o1)->oid, &((Oid*)o2)->oid);
222+
// Return boolean
223+
PyObject *res;
214224
switch (op) {
215225
case Py_LT:
216226
res = (cmp <= 0) ? Py_True: Py_False;

test/test_blob.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282

8383
def test_read_blob(testrepo):
8484
blob = testrepo[BLOB_SHA]
85-
assert str(blob.id) == BLOB_SHA
86-
assert str(blob.id) == BLOB_SHA
85+
assert blob.id == BLOB_SHA
86+
assert blob.id == BLOB_SHA
8787
assert isinstance(blob, pygit2.Blob)
8888
assert not blob.is_binary
8989
assert ObjectType.BLOB == blob.type
@@ -100,7 +100,7 @@ def test_create_blob(testrepo):
100100
assert ObjectType.BLOB == blob.type
101101

102102
assert blob_oid == blob.id
103-
assert utils.gen_blob_sha1(BLOB_NEW_CONTENT) == str(blob_oid)
103+
assert utils.gen_blob_sha1(BLOB_NEW_CONTENT) == blob_oid
104104

105105
assert BLOB_NEW_CONTENT == blob.data
106106
assert len(BLOB_NEW_CONTENT) == blob.size
@@ -124,7 +124,7 @@ def test_create_blob_fromworkdir(testrepo):
124124
assert ObjectType.BLOB == blob.type
125125

126126
assert blob_oid == blob.id
127-
assert utils.gen_blob_sha1(BLOB_FILE_CONTENT) == str(blob_oid)
127+
assert utils.gen_blob_sha1(BLOB_FILE_CONTENT) == blob_oid
128128

129129
assert BLOB_FILE_CONTENT == blob.data
130130
assert len(BLOB_FILE_CONTENT) == blob.size
@@ -163,7 +163,7 @@ def test_create_blob_fromiobase(testrepo):
163163
assert ObjectType.BLOB == blob.type
164164

165165
assert blob_oid == blob.id
166-
assert BLOB_SHA == str(blob_oid)
166+
assert BLOB_SHA == blob_oid
167167

168168

169169
def test_diff_blob(testrepo):

test/test_branch.py

+20-31
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040

4141
def test_branches_getitem(testrepo):
4242
branch = testrepo.branches['master']
43-
assert str(branch.target) == LAST_COMMIT
43+
assert branch.target == LAST_COMMIT
4444

4545
branch = testrepo.branches.local['i18n']
46-
assert str(branch.target) == I18N_LAST_COMMIT
46+
assert branch.target == I18N_LAST_COMMIT
4747
assert testrepo.branches.get('not-exists') is None
4848
with pytest.raises(KeyError):
4949
testrepo.branches['not-exists']
@@ -59,15 +59,15 @@ def test_branches_create(testrepo):
5959
reference = testrepo.branches.create('version1', commit)
6060
assert 'version1' in testrepo.branches
6161
reference = testrepo.branches['version1']
62-
assert str(reference.target) == LAST_COMMIT
62+
assert reference.target == LAST_COMMIT
6363

6464
# try to create existing reference
6565
with pytest.raises(ValueError):
6666
testrepo.branches.create('version1', commit)
6767

6868
# try to create existing reference with force
6969
reference = testrepo.branches.create('version1', commit, True)
70-
assert str(reference.target) == LAST_COMMIT
70+
assert reference.target == LAST_COMMIT
7171

7272

7373
def test_branches_delete(testrepo):
@@ -92,10 +92,10 @@ def test_branches_is_not_head(testrepo):
9292

9393
def test_branches_rename(testrepo):
9494
new_branch = testrepo.branches['i18n'].rename('new-branch')
95-
assert str(new_branch.target) == I18N_LAST_COMMIT
95+
assert new_branch.target == I18N_LAST_COMMIT
9696

9797
new_branch_2 = testrepo.branches.get('new-branch')
98-
assert str(new_branch_2.target) == I18N_LAST_COMMIT
98+
assert new_branch_2.target == I18N_LAST_COMMIT
9999

100100

101101
def test_branches_rename_error(testrepo):
@@ -107,7 +107,7 @@ def test_branches_rename_error(testrepo):
107107
def test_branches_rename_force(testrepo):
108108
original_branch = testrepo.branches.get('master')
109109
new_branch = original_branch.rename('i18n', True)
110-
assert str(new_branch.target) == LAST_COMMIT
110+
assert new_branch.target == LAST_COMMIT
111111

112112

113113
def test_branches_rename_invalid(testrepo):
@@ -153,15 +153,11 @@ def test_branches_with_commit(testrepo):
153153

154154

155155
def test_lookup_branch_local(testrepo):
156-
branch = testrepo.lookup_branch('master')
157-
assert str(branch.target) == LAST_COMMIT
158-
branch = testrepo.lookup_branch(b'master')
159-
assert str(branch.target) == LAST_COMMIT
156+
assert testrepo.lookup_branch('master').target == LAST_COMMIT
157+
assert testrepo.lookup_branch(b'master').target == LAST_COMMIT
160158

161-
branch = testrepo.lookup_branch('i18n', BranchType.LOCAL)
162-
assert str(branch.target) == I18N_LAST_COMMIT
163-
branch = testrepo.lookup_branch(b'i18n', BranchType.LOCAL)
164-
assert str(branch.target) == I18N_LAST_COMMIT
159+
assert testrepo.lookup_branch('i18n', BranchType.LOCAL).target == I18N_LAST_COMMIT
160+
assert testrepo.lookup_branch(b'i18n', BranchType.LOCAL).target == I18N_LAST_COMMIT
165161

166162
assert testrepo.lookup_branch('not-exists') is None
167163
assert testrepo.lookup_branch(b'not-exists') is None
@@ -179,21 +175,18 @@ def test_listall_branches(testrepo):
179175

180176
def test_create_branch(testrepo):
181177
commit = testrepo[LAST_COMMIT]
182-
reference = testrepo.create_branch('version1', commit)
178+
testrepo.create_branch('version1', commit)
183179
refs = testrepo.listall_branches()
184180
assert 'version1' in refs
185-
reference = testrepo.lookup_branch('version1')
186-
assert str(reference.target) == LAST_COMMIT
187-
reference = testrepo.lookup_branch(b'version1')
188-
assert str(reference.target) == LAST_COMMIT
181+
assert testrepo.lookup_branch('version1').target == LAST_COMMIT
182+
assert testrepo.lookup_branch(b'version1').target == LAST_COMMIT
189183

190184
# try to create existing reference
191185
with pytest.raises(ValueError):
192186
testrepo.create_branch('version1', commit)
193187

194188
# try to create existing reference with force
195-
reference = testrepo.create_branch('version1', commit, True)
196-
assert str(reference.target) == LAST_COMMIT
189+
assert testrepo.create_branch('version1', commit, True).target == LAST_COMMIT
197190

198191

199192
def test_delete(testrepo):
@@ -231,12 +224,9 @@ def test_branch_is_checked_out_returns_false_if_branch_is_not_checked_out(testre
231224

232225

233226
def test_branch_rename_succeeds(testrepo):
234-
original_branch = testrepo.lookup_branch('i18n')
235-
new_branch = original_branch.rename('new-branch')
236-
assert str(new_branch.target) == I18N_LAST_COMMIT
237-
238-
new_branch_2 = testrepo.lookup_branch('new-branch')
239-
assert str(new_branch_2.target) == I18N_LAST_COMMIT
227+
branch = testrepo.lookup_branch('i18n')
228+
assert branch.rename('new-branch').target == I18N_LAST_COMMIT
229+
assert testrepo.lookup_branch('new-branch').target == I18N_LAST_COMMIT
240230

241231

242232
def test_branch_rename_fails_if_destination_already_exists(testrepo):
@@ -246,9 +236,8 @@ def test_branch_rename_fails_if_destination_already_exists(testrepo):
246236

247237

248238
def test_branch_rename_not_fails_if_force_is_true(testrepo):
249-
original_branch = testrepo.lookup_branch('master')
250-
new_branch = original_branch.rename('i18n', True)
251-
assert str(new_branch.target) == LAST_COMMIT
239+
branch = testrepo.lookup_branch('master')
240+
assert branch.rename('i18n', True).target == LAST_COMMIT
252241

253242

254243
def test_branch_rename_fails_with_invalid_names(testrepo):

test/test_branch_empty.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def repo(emptyrepo):
3939

4040
def test_branches_remote_get(repo):
4141
branch = repo.branches.remote.get('origin/master')
42-
assert str(branch.target) == ORIGIN_MASTER_COMMIT
43-
42+
assert branch.target == ORIGIN_MASTER_COMMIT
4443
assert repo.branches.remote.get('origin/not-exists') is None
4544

4645

@@ -87,8 +86,7 @@ def test_branches_upstream_name(repo):
8786

8887
def test_lookup_branch_remote(repo):
8988
branch = repo.lookup_branch('origin/master', BranchType.REMOTE)
90-
assert str(branch.target) == ORIGIN_MASTER_COMMIT
91-
89+
assert branch.target == ORIGIN_MASTER_COMMIT
9290
assert repo.lookup_branch('origin/not-exists', BranchType.REMOTE) is None
9391

9492

test/test_commit.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def test_commit_refcount(barerepo):
5252

5353
def test_read_commit(barerepo):
5454
commit = barerepo[COMMIT_SHA]
55-
assert COMMIT_SHA == str(commit.id)
55+
assert COMMIT_SHA == commit.id
5656
parents = commit.parents
5757
assert 1 == len(parents)
58-
assert 'c2792cfa289ae6321ecf2cd5806c2194b0fd070c' == str(parents[0].id)
58+
assert 'c2792cfa289ae6321ecf2cd5806c2194b0fd070c' == parents[0].id
5959
assert commit.message_encoding is None
6060
assert commit.message == (
6161
'Second test data commit.\n\n' 'This commit has some additional text.\n'
@@ -68,7 +68,7 @@ def test_read_commit(barerepo):
6868
assert commit.author == Signature(
6969
'Dave Borowitz', '[email protected]', 1288477363, -420
7070
)
71-
assert '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' == str(commit.tree.id)
71+
assert '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' == commit.tree.id
7272

7373

7474
def test_new_commit(barerepo):
@@ -90,16 +90,16 @@ def test_new_commit(barerepo):
9090
commit = repo[sha]
9191

9292
assert ObjectType.COMMIT == commit.type
93-
assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == str(commit.id)
93+
assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == commit.id
9494
assert commit.message_encoding is None
9595
assert message == commit.message
9696
assert 12346 == commit.commit_time
9797
assert committer == commit.committer
9898
assert author == commit.author
99-
assert tree == str(commit.tree.id)
99+
assert tree == commit.tree.id
100100
assert Oid(hex=tree) == commit.tree_id
101101
assert 1 == len(commit.parents)
102-
assert COMMIT_SHA == str(commit.parents[0].id)
102+
assert COMMIT_SHA == commit.parents[0].id
103103
assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
104104

105105

@@ -124,10 +124,10 @@ def test_new_commit_encoding(barerepo):
124124
assert 12346 == commit.commit_time
125125
assert committer == commit.committer
126126
assert author == commit.author
127-
assert tree == str(commit.tree.id)
127+
assert tree == commit.tree.id
128128
assert Oid(hex=tree) == commit.tree_id
129129
assert 1 == len(commit.parents)
130-
assert COMMIT_SHA == str(commit.parents[0].id)
130+
assert COMMIT_SHA == commit.parents[0].id
131131
assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
132132

133133

@@ -268,13 +268,13 @@ def test_amend_commit_argument_types(barerepo):
268268
amended_oid = repo.amend_commit(alt_commit1, None, message='Hello')
269269
amended_commit = repo[amended_oid]
270270
assert ObjectType.COMMIT == amended_commit.type
271-
assert str(amended_oid) != COMMIT_SHA_TO_AMEND
271+
assert amended_oid != COMMIT_SHA_TO_AMEND
272272

273273
# Pass a str for the commit
274274
amended_oid = repo.amend_commit(alt_commit2, None, message='Hello', tree=alt_tree)
275275
amended_commit = repo[amended_oid]
276276
assert ObjectType.COMMIT == amended_commit.type
277-
assert str(amended_oid) != COMMIT_SHA_TO_AMEND
277+
assert amended_oid != COMMIT_SHA_TO_AMEND
278278
assert repo[COMMIT_SHA_TO_AMEND].tree != amended_commit.tree
279279
assert alt_tree.id == amended_commit.tree_id
280280

@@ -283,5 +283,5 @@ def test_amend_commit_argument_types(barerepo):
283283
amended_oid = repo.amend_commit(alt_commit2, alt_refname, message='Hello')
284284
amended_commit = repo[amended_oid]
285285
assert ObjectType.COMMIT == amended_commit.type
286-
assert str(amended_oid) != COMMIT_SHA_TO_AMEND
286+
assert amended_oid != COMMIT_SHA_TO_AMEND
287287
assert repo.head.target == amended_oid

test/test_commit_gpg.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ def test_commit_signing(gpgsigned):
120120

121121
# perform sanity checks
122122
assert ObjectType.COMMIT == commit.type
123-
assert '6569fdf71dbd99081891154641869c537784a3ba' == str(commit.id)
123+
assert '6569fdf71dbd99081891154641869c537784a3ba' == commit.id
124124
assert commit.message_encoding is None
125125
assert message == commit.message
126126
assert 1358451456 == commit.commit_time
127127
assert committer == commit.committer
128128
assert author == commit.author
129-
assert tree == str(commit.tree.id)
129+
assert tree == commit.tree.id
130130
assert Oid(hex=tree) == commit.tree_id
131131
assert 1 == len(commit.parents)
132-
assert parent == str(commit.parents[0].id)
132+
assert parent == commit.parents[0].id
133133
assert Oid(hex=parent) == commit.parent_ids[0]
134134

135135

test/test_diff.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,16 @@ def test_diff_ids(barerepo):
278278
commit_b = barerepo[COMMIT_SHA1_2]
279279
patch = commit_a.tree.diff_to_tree(commit_b.tree)[0]
280280
delta = patch.delta
281-
assert str(delta.old_file.id) == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
282-
assert str(delta.new_file.id) == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
281+
assert delta.old_file.id == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
282+
assert delta.new_file.id == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
283283

284284

285285
def test_diff_patchid(barerepo):
286286
commit_a = barerepo[COMMIT_SHA1_1]
287287
commit_b = barerepo[COMMIT_SHA1_2]
288288
diff = commit_a.tree.diff_to_tree(commit_b.tree)
289289
assert diff.patch == PATCH
290-
assert str(diff.patchid) == PATCHID
290+
assert diff.patchid == PATCHID
291291

292292

293293
def test_hunk_content(barerepo):

test/test_index.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,14 @@ def test_read_tree(testrepo):
147147
index.read_tree(tree_oid)
148148
assert len(index) == 1
149149
# Test read-write returns the same oid
150-
oid = index.write_tree()
151-
assert str(oid) == tree_oid
150+
assert index.write_tree() == tree_oid
152151
# Test the index is only modified in memory
153152
index.read()
154153
assert len(index) == 2
155154

156155

157156
def test_write_tree(testrepo):
158-
oid = testrepo.index.write_tree()
159-
assert str(oid) == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'
157+
assert testrepo.index.write_tree() == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'
160158

161159

162160
def test_iter(testrepo):
@@ -244,8 +242,7 @@ def test_create_entry(testrepo):
244242
hello_entry = index['hello.txt']
245243
entry = pygit2.IndexEntry('README.md', hello_entry.id, hello_entry.mode)
246244
index.add(entry)
247-
tree_id = index.write_tree()
248-
assert '60e769e57ae1d6a2ab75d8d253139e6260e1f912' == str(tree_id)
245+
assert '60e769e57ae1d6a2ab75d8d253139e6260e1f912' == index.write_tree()
249246

250247

251248
def test_create_entry_aspath(testrepo):

0 commit comments

Comments
 (0)