@@ -13,6 +13,8 @@ import * as fs from "../../util/fs.js";
13
13
import * as http from "node:http" ;
14
14
import { URL } from "node:url" ;
15
15
import open from "open" ;
16
+ import { execSync } from "child_process" ;
17
+ import path from "path" ;
16
18
17
19
import { ciStr } from "../../util/ci.js" ;
18
20
import { getProjectsByOrgReq , sendMapRepoAndFinishProjectCreationReq , sendCreateProjectReq , sendGetRepoIdReq } from "../../util/graphql.js" ;
@@ -109,7 +111,37 @@ export default class LinkIndex extends Command {
109
111
throw new Error ( chalk . red ( "No .git found in this directory. Please initialize a git repository with `git init`." ) ) ;
110
112
}
111
113
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
+ }
113
145
114
146
// check the .hypermode/settings.json and see if there is a installationId with a key for the github owner. if there is,
115
147
// 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 {
127
159
return ;
128
160
}
129
161
130
- const { gitOwner, repoName } = parseGitUrl ( gitUrl ) ;
162
+ const { gitOwner, repoName } = parseGitUrl ( remoteUrl ) ;
131
163
132
164
const repoFullName = `${ gitOwner } /${ repoName } ` ;
133
165
@@ -141,7 +173,7 @@ export default class LinkIndex extends Command {
141
173
}
142
174
143
175
// 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 ) ;
145
177
146
178
if ( ! repoId ) {
147
179
throw new Error ( "No repoId found for the given installationId and gitUrl" ) ;
0 commit comments