@@ -251,6 +251,43 @@ export default function RepoWikiPage() {
251
251
const [ authCode , setAuthCode ] = useState < string > ( '' ) ;
252
252
const [ isAuthLoading , setIsAuthLoading ] = useState < boolean > ( true ) ;
253
253
254
+ // Default branch state
255
+ const [ defaultBranch , setDefaultBranch ] = useState < string > ( 'main' ) ;
256
+
257
+ // Helper function to generate proper repository file URLs
258
+ const generateFileUrl = useCallback ( ( filePath : string ) : string => {
259
+ if ( effectiveRepoInfo . type === 'local' ) {
260
+ // For local repositories, we can't generate web URLs
261
+ return filePath ;
262
+ }
263
+
264
+ const repoUrl = effectiveRepoInfo . repoUrl ;
265
+ if ( ! repoUrl ) {
266
+ return filePath ;
267
+ }
268
+
269
+ try {
270
+ const url = new URL ( repoUrl ) ;
271
+ const hostname = url . hostname ;
272
+
273
+ if ( hostname === 'github.com' || hostname . includes ( 'github' ) ) {
274
+ // GitHub URL format: https://github.com/owner/repo/blob/branch/path
275
+ return `${ repoUrl } /blob/${ defaultBranch } /${ filePath } ` ;
276
+ } else if ( hostname === 'gitlab.com' || hostname . includes ( 'gitlab' ) ) {
277
+ // GitLab URL format: https://gitlab.com/owner/repo/-/blob/branch/path
278
+ return `${ repoUrl } /-/blob/${ defaultBranch } /${ filePath } ` ;
279
+ } else if ( hostname === 'bitbucket.org' || hostname . includes ( 'bitbucket' ) ) {
280
+ // Bitbucket URL format: https://bitbucket.org/owner/repo/src/branch/path
281
+ return `${ repoUrl } /src/${ defaultBranch } /${ filePath } ` ;
282
+ }
283
+ } catch ( error ) {
284
+ console . warn ( 'Error generating file URL:' , error ) ;
285
+ }
286
+
287
+ // Fallback to just the file path
288
+ return filePath ;
289
+ } , [ effectiveRepoInfo , defaultBranch ] ) ;
290
+
254
291
// Memoize repo info to avoid triggering updates in callbacks
255
292
256
293
// Add useEffect to handle scroll reset
@@ -367,7 +404,7 @@ Format it exactly like this:
367
404
Remember, do not provide any acknowledgements, disclaimers, apologies, or any other preface before the \`<details>\` block. JUST START with the \`<details>\` block.
368
405
The following files were used as context for generating this wiki page:
369
406
370
- ${ filePaths . map ( path => `- [${ path } ](${ path } )` ) . join ( '\n' ) }
407
+ ${ filePaths . map ( path => `- [${ path } ](${ generateFileUrl ( path ) } )` ) . join ( '\n' ) }
371
408
<!-- Add additional relevant files if fewer than 5 were provided -->
372
409
</details>
373
410
@@ -407,7 +444,7 @@ Based ONLY on the content of the \`[RELEVANT_SOURCE_FILES]\`:
407
444
* Configuration options, their types, and default values.
408
445
* Data model fields, types, constraints, and descriptions.
409
446
410
- 5. **Code Snippets:**
447
+ 5. **Code Snippets (ENTIRELY OPTIONAL) :**
411
448
* Include short, relevant code snippets (e.g., Python, Java, JavaScript, SQL, JSON, YAML) directly from the \`[RELEVANT_SOURCE_FILES]\` to illustrate key implementation details, data structures, or configurations.
412
449
* Ensure snippets are well-formatted within Markdown code blocks with appropriate language identifiers.
413
450
@@ -591,7 +628,7 @@ Remember:
591
628
setLoadingMessage ( undefined ) ; // Clear specific loading message
592
629
}
593
630
} ) ;
594
- } , [ generatedPages , currentToken , effectiveRepoInfo , selectedProviderState , selectedModelState , isCustomSelectedModelState , customSelectedModelState , modelExcludedDirs , modelExcludedFiles , language , activeContentRequests ] ) ;
631
+ } , [ generatedPages , currentToken , effectiveRepoInfo , selectedProviderState , selectedModelState , isCustomSelectedModelState , customSelectedModelState , modelExcludedDirs , modelExcludedFiles , language , activeContentRequests , generateFileUrl ] ) ;
595
632
596
633
// Determine the wiki structure from repository data
597
634
const determineWikiStructure = useCallback ( async ( fileTree : string , readme : string , owner : string , repo : string ) => {
@@ -1111,6 +1148,8 @@ IMPORTANT:
1111
1148
const data = await response . json ( ) ;
1112
1149
fileTreeData = data . file_tree ;
1113
1150
readmeContent = data . readme ;
1151
+ // For local repos, we can't determine the actual branch, so use 'main' as default
1152
+ setDefaultBranch ( 'main' ) ;
1114
1153
} catch ( err ) {
1115
1154
throw err ;
1116
1155
}
@@ -1145,7 +1184,30 @@ IMPORTANT:
1145
1184
1146
1185
const githubApiBaseUrl = getGithubApiUrl ( effectiveRepoInfo . repoUrl ) ;
1147
1186
1148
- for ( const branch of [ 'main' , 'master' ] ) {
1187
+ // First, try to get the default branch from the repository info
1188
+ let defaultBranchLocal = null ;
1189
+ try {
1190
+ const repoInfoResponse = await fetch ( `${ githubApiBaseUrl } /repos/${ owner } /${ repo } ` , {
1191
+ headers : createGithubHeaders ( currentToken )
1192
+ } ) ;
1193
+
1194
+ if ( repoInfoResponse . ok ) {
1195
+ const repoData = await repoInfoResponse . json ( ) ;
1196
+ defaultBranchLocal = repoData . default_branch ;
1197
+ console . log ( `Found default branch: ${ defaultBranchLocal } ` ) ;
1198
+ // Store the default branch in state
1199
+ setDefaultBranch ( defaultBranchLocal || 'main' ) ;
1200
+ }
1201
+ } catch ( err ) {
1202
+ console . warn ( 'Could not fetch repository info for default branch:' , err ) ;
1203
+ }
1204
+
1205
+ // Create list of branches to try, prioritizing the actual default branch
1206
+ const branchesToTry = defaultBranchLocal
1207
+ ? [ defaultBranchLocal , 'main' , 'master' ] . filter ( ( branch , index , arr ) => arr . indexOf ( branch ) === index )
1208
+ : [ 'main' , 'master' ] ;
1209
+
1210
+ for ( const branch of branchesToTry ) {
1149
1211
const apiUrl = `${ githubApiBaseUrl } /repos/${ owner } /${ repo } /git/trees/${ branch } ?recursive=1` ;
1150
1212
const headers = createGithubHeaders ( currentToken ) ;
1151
1213
@@ -1215,6 +1277,7 @@ IMPORTANT:
1215
1277
try {
1216
1278
// Step 1: Get project info to determine default branch
1217
1279
let projectInfoUrl : string ;
1280
+ let defaultBranchLocal = 'main' ; // fallback
1218
1281
try {
1219
1282
const validatedUrl = new URL ( projectDomain ?? '' ) ; // Validate domain
1220
1283
projectInfoUrl = `${ validatedUrl . origin } /api/v4/projects/${ encodedProjectPath } ` ;
@@ -1228,10 +1291,16 @@ IMPORTANT:
1228
1291
throw new Error ( `GitLab project info error: Status ${ projectInfoRes . status } , Response: ${ errorData } ` ) ;
1229
1292
}
1230
1293
1294
+ const projectInfo = await projectInfoRes . json ( ) ;
1295
+ defaultBranchLocal = projectInfo . default_branch || 'main' ;
1296
+ console . log ( `Found GitLab default branch: ${ defaultBranchLocal } ` ) ;
1297
+ // Store the default branch in state
1298
+ setDefaultBranch ( defaultBranchLocal ) ;
1299
+
1231
1300
// Step 2: Paginate to fetch full file tree
1232
1301
let page = 1 ;
1233
1302
let morePages = true ;
1234
-
1303
+
1235
1304
while ( morePages ) {
1236
1305
const apiUrl = `${ projectInfoUrl } /repository/tree?recursive=true&per_page=100&page=${ page } ` ;
1237
1306
const response = await fetch ( apiUrl , { headers } ) ;
@@ -1285,7 +1354,7 @@ IMPORTANT:
1285
1354
// Try to get the file tree for common branch names
1286
1355
let filesData = null ;
1287
1356
let apiErrorDetails = '' ;
1288
- let defaultBranch = '' ;
1357
+ let defaultBranchLocal = '' ;
1289
1358
const headers = createBitbucketHeaders ( currentToken ) ;
1290
1359
1291
1360
// First get project info to determine default branch
@@ -1297,9 +1366,11 @@ IMPORTANT:
1297
1366
1298
1367
if ( response . ok ) {
1299
1368
const projectData = JSON . parse ( responseText ) ;
1300
- defaultBranch = projectData . mainbranch . name ;
1369
+ defaultBranchLocal = projectData . mainbranch . name ;
1370
+ // Store the default branch in state
1371
+ setDefaultBranch ( defaultBranchLocal ) ;
1301
1372
1302
- const apiUrl = `https://api.bitbucket.org/2.0/repositories/${ encodedRepoPath } /src/${ defaultBranch } /?recursive=true&per_page=100` ;
1373
+ const apiUrl = `https://api.bitbucket.org/2.0/repositories/${ encodedRepoPath } /src/${ defaultBranchLocal } /?recursive=true&per_page=100` ;
1303
1374
try {
1304
1375
const response = await fetch ( apiUrl , {
1305
1376
headers
@@ -1314,7 +1385,7 @@ IMPORTANT:
1314
1385
apiErrorDetails = `Status: ${ response . status } , Response: ${ errorData } ` ;
1315
1386
}
1316
1387
} catch ( err ) {
1317
- console . error ( `Network error fetching Bitbucket branch ${ defaultBranch } :` , err ) ;
1388
+ console . error ( `Network error fetching Bitbucket branch ${ defaultBranchLocal } :` , err ) ;
1318
1389
}
1319
1390
} else {
1320
1391
const errorData = responseText ;
@@ -1342,7 +1413,7 @@ IMPORTANT:
1342
1413
try {
1343
1414
const headers = createBitbucketHeaders ( currentToken ) ;
1344
1415
1345
- const readmeResponse = await fetch ( `https://api.bitbucket.org/2.0/repositories/${ encodedRepoPath } /src/${ defaultBranch } /README.md` , {
1416
+ const readmeResponse = await fetch ( `https://api.bitbucket.org/2.0/repositories/${ encodedRepoPath } /src/${ defaultBranchLocal } /README.md` , {
1346
1417
headers
1347
1418
} ) ;
1348
1419
0 commit comments