@@ -13,6 +13,8 @@ import * as fs from "../../util/fs.js";
1313import * as http from "node:http" ;
1414import { URL } from "node:url" ;
1515import open from "open" ;
16+ import { execSync } from "child_process" ;
17+ import path from "path" ;
1618
1719import { ciStr } from "../../util/ci.js" ;
1820import { 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" ) ;
0 commit comments