swadhinbiswas ✨ feat(cli): harden auth, TLS, retries, and repo parsing · 1/22/2026
416b898
63 lines · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { simpleGit, type SimpleGit } from "simple-git";

export interface RepoInfo {
  owner: string;
  repo: string;
}

function stripGitSuffix(value: string): string {
  return value.replace(/\.git$/i, "");
}

export function parseRepoFromRemoteUrl(url: string): RepoInfo | null {
  try {
    if (!url) return null;

    let path = "";

    if (
      url.startsWith("http://") ||
      url.startsWith("https://") ||
      url.startsWith("ssh://")
    ) {
      const parsed = new URL(url);
      path = parsed.pathname;
    } else if (url.includes(":")) {
      const parts = url.split(":");
      path = parts.slice(1).join(":");
    } else {
      path = url;
    }

    const segments = path.replace(/^\/+/, "").split("/").filter(Boolean);
    if (segments.length < 2) return null;

    const repo = stripGitSuffix(segments.pop() || "");
    const owner = segments.pop() || "";

    if (!owner || !repo) return null;

    return { owner, repo };
  } catch {
    return null;
  }
}

export async function getRepoInfoFromGit(
  git?: SimpleGit,
): Promise<RepoInfo | null> {
  try {
    const client = git ?? simpleGit();
    const remotes = await client.getRemotes(true);
    const origin = remotes.find((r) => r.name === "origin");
    if (!origin?.refs?.push && !origin?.refs?.fetch) return null;

    const remoteUrl = origin.refs.push || origin.refs.fetch;
    if (!remoteUrl) return null;

    return parseRepoFromRemoteUrl(remoteUrl);
  } catch {
    return null;
  }
}