-
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
3 changed files
with
13 additions
and
57 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
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,38 +1,24 @@ | ||
import gitUrlParse from 'git-url-parse' | ||
import type { GitRepository } from 'tabby-chat-panel' | ||
|
||
export function findClosestGitRepository( | ||
repositories: GitRepository[], | ||
export function findClosestGitRepository<T extends GitRepository>( | ||
repositories: T[], | ||
gitUrl: string | ||
): GitRepository | undefined { | ||
): T | undefined { | ||
const targetSearch = gitUrlParse(gitUrl) | ||
if (!targetSearch) { | ||
return undefined | ||
} | ||
|
||
const filteredRepos = repositories.filter(repo => { | ||
const search = gitUrlParse(repo.url) | ||
const isSameResource = | ||
search.resource === targetSearch.resource || search.protocol === 'file' | ||
return isSameResource && search.name === targetSearch.name | ||
return search.name === targetSearch.name | ||
}) | ||
|
||
if (filteredRepos.length === 0) { | ||
return undefined | ||
} else { | ||
// If there're multiple matches, we pick the one with highest alphabetical order | ||
return filteredRepos.reduce((min, current) => { | ||
const minUrl = canonicalizeUrl(min.url) | ||
const currentUrl = canonicalizeUrl(current.url) | ||
return minUrl > currentUrl ? min : current | ||
}) | ||
return filteredRepos.sort((a, b) => a.url.localeCompare(b.url))[0] | ||
} | ||
} | ||
|
||
export function canonicalizeUrl(url: string): string { | ||
const strippedUrl = url.replace(/\.git$/, '') | ||
const parsedUrl = new URL(strippedUrl) | ||
parsedUrl.username = '' | ||
parsedUrl.password = '' | ||
return parsedUrl.toString() | ||
} |
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