From 822fd0f042619a86cec62d314ee5df86dd87c05e Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 17:48:36 -0700 Subject: [PATCH 1/6] HYP-2624: hyp link should guide the user to create remote origin --- src/commands/link/index.ts | 18 +++++++++++++++++- src/util/index.ts | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index 0142896..377a5d4 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -13,10 +13,12 @@ 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"; -import { confirmExistingProjectLink, confirmOverwriteCiHypFile, fileExists, getCiHypFilePath, getSettingsFilePath, getGitConfigFilePath, getGitRemoteUrl, getGithubWorkflowDir, promptProjectLinkSelection, promptProjectName, readSettingsJson, writeGithubInstallationIdToSettingsFile } from "../../util/index.js"; +import { confirmExistingProjectLink, confirmOverwriteCiHypFile, fileExists, getCiHypFilePath, getSettingsFilePath, getGitConfigFilePath, getGitRemoteUrl, getGithubWorkflowDir, promptProjectLinkSelection, promptProjectName, readSettingsJson, writeGithubInstallationIdToSettingsFile, hasGitRemoteUrl } from "../../util/index.js"; export default class LinkIndex extends Command { static override args = {}; @@ -109,6 +111,20 @@ 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 hasRemoteOrigin = await hasGitRemoteUrl(gitConfigFilePath); + + if (!hasRemoteOrigin) { + 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(`> git remote add origin `); + this.log(`> git push -u origin main`); + + return; + } + const gitUrl = await getGitRemoteUrl(gitConfigFilePath); // check the .hypermode/settings.json and see if there is a installationId with a key for the github owner. if there is, diff --git a/src/util/index.ts b/src/util/index.ts index 3fc3546..09a85cd 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -125,6 +125,15 @@ export function getGitConfigFilePath(): string { return path.join(getGitDir(), "config"); } +export async function hasGitRemoteUrl(filePath: string): Promise { + const content = await fs.readFile(filePath, "utf8"); + const remoteMatch = content.match(/\[remote "origin"]\n\s+url = (.*)/); + if (!remoteMatch) { + return false; + } + + return true; +} export async function getGitRemoteUrl(filePath: string): Promise { const content = await fs.readFile(filePath, "utf8"); const remoteMatch = content.match(/\[remote "origin"]\n\s+url = (.*)/); From 0d973050dd787b9d438238c8f6ba07bc6dadd597 Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 17:57:33 -0700 Subject: [PATCH 2/6] HYP-2682: `hyp link` check branch name is `main` --- src/commands/link/index.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index 377a5d4..77fb149 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -111,6 +111,23 @@ 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`.")); } + // Check if the current branch is 'main' + let currentBranch = ""; + try { + currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim(); + } catch (error) { + throw new Error(chalk.red("Unable to determine the current branch. Are you inside a Git repository?")); + } + + 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(" > git checkout main"); + this.log("or rename your current branch to 'main'."); + this.log(" > git branch -m main"); + this.exit(1); + } + const hasRemoteOrigin = await hasGitRemoteUrl(gitConfigFilePath); if (!hasRemoteOrigin) { @@ -119,10 +136,10 @@ export default class LinkIndex extends Command { 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(`> git remote add origin `); - this.log(`> git push -u origin main`); + this.log(` > git remote add origin `); + this.log(` > git push -u origin main`); - return; + this.exit(1); } const gitUrl = await getGitRemoteUrl(gitConfigFilePath); From 3ca706f4d21421a5d1607b300c3d98c13fd1e6a7 Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 17:59:03 -0700 Subject: [PATCH 3/6] fix lint --- src/commands/link/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index 77fb149..dbdb956 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -116,7 +116,8 @@ export default class LinkIndex extends Command { try { currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim(); } catch (error) { - throw new Error(chalk.red("Unable to determine the current branch. Are you inside a Git repository?")); + this.log(chalk.red("Unable to determine the current branch. Are you inside a Git repository?")); + throw error; } if (currentBranch !== "main") { From e8355b33e6c30912df56d7e5563d90088499c1b6 Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 18:19:15 -0700 Subject: [PATCH 4/6] address feedback --- src/commands/link/index.ts | 12 +++++------- src/util/index.ts | 12 ++---------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index dbdb956..f305757 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -116,7 +116,7 @@ export default class LinkIndex extends Command { try { currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim(); } catch (error) { - this.log(chalk.red("Unable to determine the current branch. Are you inside a Git repository?")); + this.log(chalk.red("Unable to determine the current branch.")); throw error; } @@ -129,9 +129,9 @@ export default class LinkIndex extends Command { this.exit(1); } - const hasRemoteOrigin = await hasGitRemoteUrl(gitConfigFilePath); + const remoteUrl = await getGitRemoteUrl(gitConfigFilePath); - if (!hasRemoteOrigin) { + 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); @@ -143,8 +143,6 @@ export default class LinkIndex extends Command { this.exit(1); } - const gitUrl = await getGitRemoteUrl(gitConfigFilePath); - // 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 @@ -161,7 +159,7 @@ export default class LinkIndex extends Command { return; } - const { gitOwner, repoName } = parseGitUrl(gitUrl); + const { gitOwner, repoName } = parseGitUrl(remoteUrl); const repoFullName = `${gitOwner}/${repoName}`; @@ -175,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"); diff --git a/src/util/index.ts b/src/util/index.ts index 09a85cd..8c1706a 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -125,20 +125,12 @@ export function getGitConfigFilePath(): string { return path.join(getGitDir(), "config"); } -export async function hasGitRemoteUrl(filePath: string): Promise { +export async function getGitRemoteUrl(filePath: string): Promise { const content = await fs.readFile(filePath, "utf8"); const remoteMatch = content.match(/\[remote "origin"]\n\s+url = (.*)/); - if (!remoteMatch) { - return false; - } - return true; -} -export async function getGitRemoteUrl(filePath: string): Promise { - 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 `.")); + return null; } return remoteMatch[1]; From 439f41493c8e5ec7c602c40931ddc6cbb63280b8 Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 18:21:36 -0700 Subject: [PATCH 5/6] fix lint --- src/commands/link/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index f305757..fc5c009 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -18,7 +18,7 @@ import path from "path"; import { ciStr } from "../../util/ci.js"; import { getProjectsByOrgReq, sendMapRepoAndFinishProjectCreationReq, sendCreateProjectReq, sendGetRepoIdReq } from "../../util/graphql.js"; -import { confirmExistingProjectLink, confirmOverwriteCiHypFile, fileExists, getCiHypFilePath, getSettingsFilePath, getGitConfigFilePath, getGitRemoteUrl, getGithubWorkflowDir, promptProjectLinkSelection, promptProjectName, readSettingsJson, writeGithubInstallationIdToSettingsFile, hasGitRemoteUrl } from "../../util/index.js"; +import { confirmExistingProjectLink, confirmOverwriteCiHypFile, fileExists, getCiHypFilePath, getSettingsFilePath, getGitConfigFilePath, getGitRemoteUrl, getGithubWorkflowDir, promptProjectLinkSelection, promptProjectName, readSettingsJson, writeGithubInstallationIdToSettingsFile } from "../../util/index.js"; export default class LinkIndex extends Command { static override args = {}; From d4e3f67433504cfce3dd49c0b84025e6ec2a0764 Mon Sep 17 00:00:00 2001 From: Pine Date: Tue, 26 Nov 2024 18:26:18 -0700 Subject: [PATCH 6/6] output colors --- src/commands/link/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commands/link/index.ts b/src/commands/link/index.ts index fc5c009..89b30a4 100644 --- a/src/commands/link/index.ts +++ b/src/commands/link/index.ts @@ -123,9 +123,9 @@ export default class LinkIndex extends Command { 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(" > git checkout main"); + this.log(` > ${chalk.blue("git checkout main")}`); this.log("or rename your current branch to 'main'."); - this.log(" > git branch -m main"); + this.log(` > ${chalk.blue("git branch -m main")}`); this.exit(1); } @@ -137,8 +137,8 @@ export default class LinkIndex extends Command { 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(` > git remote add origin `); - this.log(` > git push -u origin main`); + this.log(` > ${chalk.blue("git remote add origin )")}`); + this.log(` > ${chalk.blue("git push -u origin main")}`); this.exit(1); }