Skip to content

Commit

Permalink
HYP-2624: hyp link should guide the user to create remote origin (#57)
Browse files Browse the repository at this point in the history
* HYP-2624: hyp link should guide the user to create remote origin

* HYP-2682: `hyp link` check branch name is `main`
  • Loading branch information
octref authored Nov 27, 2024
1 parent 942cd77 commit a53eb62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
38 changes: 35 additions & 3 deletions src/commands/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import * as fs from "../../util/fs.js";
import * as http from "node:http";
import { URL } from "node:url";
import open from "open";
import { execSync } from "child_process";
import path from "path";

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

const gitUrl = await getGitRemoteUrl(gitConfigFilePath);
// Check if the current branch is 'main'
let currentBranch = "";
try {
currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim();
} catch (error) {
this.log(chalk.red("Unable to determine the current branch."));
throw error;
}

if (currentBranch !== "main") {
this.log(chalk.red("You must be on the 'main' branch to link your repository."));
this.log("Please switch to the 'main' branch:");
this.log(` > ${chalk.blue("git checkout main")}`);
this.log("or rename your current branch to 'main'.");
this.log(` > ${chalk.blue("git branch -m main")}`);
this.exit(1);
}

const remoteUrl = await getGitRemoteUrl(gitConfigFilePath);

if (!remoteUrl) {
this.log(chalk.red("`hyp link` requires a git remote to work"));
const gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
const projectName = path.basename(gitRoot);
this.log(`Please create a GitHub repository: https://github.com/new?name=${projectName}`);
this.log(`And push your code:`);
this.log(` > ${chalk.blue("git remote add origin <GIT_URL>)")}`);
this.log(` > ${chalk.blue("git push -u origin main")}`);

this.exit(1);
}

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

const { gitOwner, repoName } = parseGitUrl(gitUrl);
const { gitOwner, repoName } = parseGitUrl(remoteUrl);

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

Expand All @@ -141,7 +173,7 @@ export default class LinkIndex extends Command {
}

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

if (!repoId) {
throw new Error("No repoId found for the given installationId and gitUrl");
Expand Down
5 changes: 3 additions & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ export function getGitConfigFilePath(): string {
return path.join(getGitDir(), "config");
}

export async function getGitRemoteUrl(filePath: string): Promise<string> {
export async function getGitRemoteUrl(filePath: string): Promise<string | null> {
const content = await fs.readFile(filePath, "utf8");
const remoteMatch = content.match(/\[remote "origin"]\n\s+url = (.*)/);

if (!remoteMatch) {
throw new Error(chalk.red("No remote origin found in .git/config, please set up a remote origin with `git remote add origin <url>`."));
return null;
}

return remoteMatch[1];
Expand Down

0 comments on commit a53eb62

Please sign in to comment.