refactor(repository): only retrieve local branches (i.e; not remotes/* ones) by default in getRepositoryBranches@@ -12,10 +12,10 @@ import { RepositoryServiceDeps } from "./types";
const makeGetRepositoryBranches: ServiceMethodFactory<
RepositoryServiceDeps,
- [Repository],
+ [Repository, undefined | boolean],
Promise<string[]>
> = ({ request }) => {
- return async (repo) => {
+ return async (repo, onlyLocalBranches = true) => {
const parentOrg = await request.prisma.organization.findUnique({
where: {
id: repo.organizationId,
@@ -28,14 +28,12 @@ const makeGetRepositoryBranches: ServiceMethodFactory<
);
}
- try {
- const repoPath = `${Env.GIT_REPOSITORIES_ROOT}/${parentOrg.slug}/${repo.slug}.git`;
- if (existsSync(repoPath) === false) {
- throw new Error(
- `Could not find a valid git repository at: ${repoPath}`
- );
- }
+ const repoPath = `${Env.GIT_REPOSITORIES_ROOT}/${parentOrg.slug}/${repo.slug}.git`;
+ if (existsSync(repoPath) === false) {
+ throw new Error(`Could not find a valid git repository at: ${repoPath}`);
+ }
+ try {
const gitBranchProcess = spawn("git", ["branch", "-a"], {
cwd: repoPath,
});
@@ -53,8 +51,9 @@ const makeGetRepositoryBranches: ServiceMethodFactory<
const branches = gitBranchResult
.split("\n")
- .map((branch) => branch.trim().replace(/^\* /i, ""))
- .filter((x) => x != null && x.trim() !== "");
+ .map((b) => b.trim().replace(/^\* /i, ""))
+ .filter((b) => b != null && b.trim() !== "")
+ .filter((b) => onlyLocalBranches && b.startsWith("remotes/") === false);
return branches;
} catch (_) {
@@ -58,7 +58,10 @@ export interface RepositoryServiceAPI extends ServiceApiContract {
getCurrentUserRepositoryForks(
repository: Repository
): Promise<RepositoryWithParentAndForkedFromRepos[]>;
- getRepositoryBranches(repository: Repository): Promise<string[]>;
+ getRepositoryBranches(
+ repository: Repository,
+ onlyLocalBranches?: boolean
+ ): Promise<string[]>;
getRepositoryCommitLog(
repository: Repository,
path?: string,