Skip to content

Commit

Permalink
Add repo.isOid(value) method
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Oct 30, 2024
1 parent 32d176e commit 9cac976
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## next

- Added `repo.describeRef(ref)` method, which returns an information object about the reference
- Added `repo.isOid(value)` method to check if a value is an object ID

## 0.1.3 (2023-10-13)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ const info = repo.describeRef('origin/HEAD');
// oid: '7b84f676f2fbea2a3c6d83924fa63059c7bdfbe2'
// }
```

#### repo.isOid(value)

Checks if a `value` is a valid oid.

```js
repo.isOid('7b84f676f2fbea2a3c6d83924fa63059c7bdfbe2'); // true
repo.isOid('main'); // false
```

#### repo.listRemotes()
Expand Down
1 change: 1 addition & 0 deletions src/resolve-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export async function createRefIndex(gitdir: string) {
);

return {
isOid,
isRefExists: (ref: string) => expandRef(ref) !== null,
resolveRef,
expandRef: (ref: string) => (isOid(ref) ? ref : expandRef(ref)),
Expand Down
20 changes: 20 additions & 0 deletions test/resolve-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,24 @@ describe('resolve-ref', () => {
});
});
});

describe('isOid()', () => {
it('should return true for a valid OID (40-character hexadecimal string)', function () {
assert.strictEqual(repo.isOid('1234567890abcdef1234567890abcdef12345678'), true);
});

const badValues = [
'1234567890abcdef1234567890abcdef1234',
'1234567890abcdef1234567890abcdef12345678901234',
'1234567890abcdef1234567890abcdef1234567G',
1234567895678901,
''
];

for (const value of badValues) {
it(JSON.stringify(value), () => {
assert.strictEqual(repo.isOid('1234567890abcdef1234567890abcdef1234567G'), false);
});
}
});
});

0 comments on commit 9cac976

Please sign in to comment.