Skip to content

Commit a53eb62

Browse files
authored
HYP-2624: hyp link should guide the user to create remote origin (#57)
* HYP-2624: hyp link should guide the user to create remote origin * HYP-2682: `hyp link` check branch name is `main`
1 parent 942cd77 commit a53eb62

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/commands/link/index.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as fs from "../../util/fs.js";
1313
import * as http from "node:http";
1414
import { URL } from "node:url";
1515
import open from "open";
16+
import { execSync } from "child_process";
17+
import path from "path";
1618

1719
import { ciStr } from "../../util/ci.js";
1820
import { getProjectsByOrgReq, sendMapRepoAndFinishProjectCreationReq, sendCreateProjectReq, sendGetRepoIdReq } from "../../util/graphql.js";
@@ -109,7 +111,37 @@ export default class LinkIndex extends Command {
109111
throw new Error(chalk.red("No .git found in this directory. Please initialize a git repository with `git init`."));
110112
}
111113

112-
const gitUrl = await getGitRemoteUrl(gitConfigFilePath);
114+
// Check if the current branch is 'main'
115+
let currentBranch = "";
116+
try {
117+
currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim();
118+
} catch (error) {
119+
this.log(chalk.red("Unable to determine the current branch."));
120+
throw error;
121+
}
122+
123+
if (currentBranch !== "main") {
124+
this.log(chalk.red("You must be on the 'main' branch to link your repository."));
125+
this.log("Please switch to the 'main' branch:");
126+
this.log(` > ${chalk.blue("git checkout main")}`);
127+
this.log("or rename your current branch to 'main'.");
128+
this.log(` > ${chalk.blue("git branch -m main")}`);
129+
this.exit(1);
130+
}
131+
132+
const remoteUrl = await getGitRemoteUrl(gitConfigFilePath);
133+
134+
if (!remoteUrl) {
135+
this.log(chalk.red("`hyp link` requires a git remote to work"));
136+
const gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
137+
const projectName = path.basename(gitRoot);
138+
this.log(`Please create a GitHub repository: https://github.com/new?name=${projectName}`);
139+
this.log(`And push your code:`);
140+
this.log(` > ${chalk.blue("git remote add origin <GIT_URL>)")}`);
141+
this.log(` > ${chalk.blue("git push -u origin main")}`);
142+
143+
this.exit(1);
144+
}
113145

114146
// check the .hypermode/settings.json and see if there is a installationId with a key for the github owner. if there is,
115147
// continue, if not send them to github app installation page, and then go to callback server, and add installation id to settings.json
@@ -127,7 +159,7 @@ export default class LinkIndex extends Command {
127159
return;
128160
}
129161

130-
const { gitOwner, repoName } = parseGitUrl(gitUrl);
162+
const { gitOwner, repoName } = parseGitUrl(remoteUrl);
131163

132164
const repoFullName = `${gitOwner}/${repoName}`;
133165

@@ -141,7 +173,7 @@ export default class LinkIndex extends Command {
141173
}
142174

143175
// call hypermode getRepoId with the installationId and the git url, if it returns a repoId, continue, if not, throw an error
144-
const repoId = await sendGetRepoIdReq(settings.jwt, installationId, gitUrl);
176+
const repoId = await sendGetRepoIdReq(settings.jwt, installationId, remoteUrl);
145177

146178
if (!repoId) {
147179
throw new Error("No repoId found for the given installationId and gitUrl");

src/util/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ export function getGitConfigFilePath(): string {
125125
return path.join(getGitDir(), "config");
126126
}
127127

128-
export async function getGitRemoteUrl(filePath: string): Promise<string> {
128+
export async function getGitRemoteUrl(filePath: string): Promise<string | null> {
129129
const content = await fs.readFile(filePath, "utf8");
130130
const remoteMatch = content.match(/\[remote "origin"]\n\s+url = (.*)/);
131+
131132
if (!remoteMatch) {
132-
throw new Error(chalk.red("No remote origin found in .git/config, please set up a remote origin with `git remote add origin <url>`."));
133+
return null;
133134
}
134135

135136
return remoteMatch[1];

0 commit comments

Comments
 (0)