-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
305 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def fibonacci_memoization(n, memo={}):\n", | ||
" if n <= 0:\n", | ||
" raise ValueError(\"Input should be a positive integer.\")\n", | ||
" if n in memo:\n", | ||
" return memo[n]\n", | ||
" if n == 1:\n", | ||
" return 0\n", | ||
" elif n == 2:\n", | ||
" return 1\n", | ||
" else:\n", | ||
" memo[n] = fibonacci_memoization(n - 1, memo) + fibonacci_memoization(n - 2, memo)\n", | ||
" return memo[n]\n", | ||
"\n", | ||
"# Example usage:\n", | ||
"print(fibonacci_memoization(10))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { go } from 'fuzzysort' | ||
import gitUrlParse from 'git-url-parse' | ||
import type { GitRepository } from 'tabby-chat-panel' | ||
|
||
export const findClosestRepositoryMatch = ( | ||
target: string, | ||
gitUrls: string[] | ||
) => { | ||
const results = go(target.replace(/\.git$/, ''), gitUrls) | ||
return results.length > 0 ? results[0].target : null | ||
export function findClosestGitRepository( | ||
repositories: GitRepository[], | ||
gitUrl: string | ||
): GitRepository | undefined { | ||
const gitSearch = gitUrlParse(gitUrl) | ||
if (!gitSearch) { | ||
return undefined | ||
} | ||
|
||
const repos = repositories.filter(repo => { | ||
const search = gitUrlParse(repo.url) | ||
return search.name === gitSearch.name | ||
}) | ||
// If there're multiple matches, we pick the one with highest alphabetical order | ||
return repos.sort((a, b) => b.url.localeCompare(a.url))[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { findClosestGitRepository } from '../lib/utils/repository' | ||
import type { GitRepository } from 'tabby-chat-panel' | ||
|
||
describe('findClosestGitRepository', () => { | ||
it('should match .git suffix', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/example/test' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://github.com/example/test.git') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
|
||
it('should match auth in URL', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/example/test' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://[email protected]/example/test') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
|
||
it('should not match different names', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/example/anoth-repo' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://github.com/example/another-repo') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should not match repositories with a common prefix', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/registry-tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://github.com/TabbyML/tabby') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should not match entirely different repository names', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/uptime' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://github.com/TabbyML/tabby') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should not match URL without repository name', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://github.com') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should not match different host', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://bitbucket.com/TabbyML/tabby') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should not match multiple close matches', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://bitbucket.com/CrabbyML/crabby' }, | ||
{ url: 'https://gitlab.com/TabbyML/registry-tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby') | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should match different protocol and suffix', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby.git') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
|
||
it('should match different protocol', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://github.com/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
|
||
it('should match URL without organization', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'https://custom-git.com/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, 'https://custom-git.com/tabby') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
|
||
it('should match local URL', () => { | ||
const repositories: GitRepository[] = [ | ||
{ url: 'file:///home/TabbyML/tabby' }, | ||
] | ||
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby.git') | ||
expect(result).toEqual(repositories[0]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.