Skip to content

Commit e77c890

Browse files
author
nessie
committed
fix main/master
1 parent 00633ee commit e77c890

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

frontend/src/components/git/BranchSelect.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ interface BranchSelectProps {
1919
size?: 'small' | 'medium'
2020
}
2121

22+
const getFallbackBranch = (defaultBranch: string | undefined, branches: string[]): string => {
23+
if (branches.length === 0) {
24+
return ''
25+
}
26+
27+
if (branches.includes('main')) {
28+
return 'main'
29+
}
30+
if (branches.includes('master')) {
31+
return 'master'
32+
}
33+
34+
if (defaultBranch && branches.includes(defaultBranch)) {
35+
return defaultBranch
36+
}
37+
38+
return branches[0]
39+
}
40+
2241
const BranchSelect: FC<BranchSelectProps> = ({
2342
repository,
2443
currentBranch,
@@ -29,6 +48,8 @@ const BranchSelect: FC<BranchSelectProps> = ({
2948
onNewBranchClick,
3049
size = 'small',
3150
}) => {
51+
const fallbackBranch = getFallbackBranch(repository?.default_branch, branches)
52+
3253
const handleChange = (value: string) => {
3354
setCurrentBranch(value)
3455
if (onBranchChange) {
@@ -48,15 +69,12 @@ const BranchSelect: FC<BranchSelectProps> = ({
4869
renderValue={(value) => (
4970
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
5071
<GitBranch size={14} />
51-
<span>{value || repository?.default_branch || 'main'}</span>
72+
<span>{value || fallbackBranch}</span>
5273
</Box>
5374
)}
5475
sx={{ fontWeight: 500 }}
5576
>
56-
<MenuItem value="">
57-
{repository?.default_branch || 'main'}
58-
</MenuItem>
59-
{branches.filter(b => b !== repository?.default_branch).map((branch) => (
77+
{branches.map((branch) => (
6078
<MenuItem key={branch} value={branch}>
6179
{branch}
6280
</MenuItem>

frontend/src/components/git/CodeTab.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ import {
4444
import useSnackbar from '../../hooks/useSnackbar'
4545
import BranchSelect from './BranchSelect'
4646

47+
const getFallbackBranch = (defaultBranch: string | undefined, branches: string[]): string => {
48+
if (branches.length === 0) {
49+
return ''
50+
}
51+
52+
if (branches.includes('main')) {
53+
return 'main'
54+
}
55+
if (branches.includes('master')) {
56+
return 'master'
57+
}
58+
59+
if (defaultBranch && branches.includes(defaultBranch)) {
60+
return defaultBranch
61+
}
62+
63+
return branches[0]
64+
}
65+
4766
interface CodeTabProps {
4867
repository: any
4968
enrichments: any[]
@@ -132,6 +151,7 @@ const CodeTab: FC<CodeTabProps> = ({
132151
const createPullRequestMutation = useCreateGitRepositoryPullRequest()
133152
const { data: pullRequests = [] } = useListRepositoryPullRequests(repository?.id || '')
134153
const snackbar = useSnackbar()
154+
const fallbackBranch = getFallbackBranch(repository?.default_branch, branches)
135155

136156
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null)
137157
const openMenu = Boolean(anchorEl)
@@ -166,7 +186,7 @@ const CodeTab: FC<CodeTabProps> = ({
166186
request: {
167187
title: `Pull Request from ${currentBranch}`,
168188
source_branch: currentBranch,
169-
target_branch: repository.default_branch || 'main',
189+
target_branch: fallbackBranch,
170190
}
171191
})
172192
setCreatePRDialogOpen(false)
@@ -259,7 +279,7 @@ const CodeTab: FC<CodeTabProps> = ({
259279
>
260280
<MenuItem
261281
disabled={
262-
currentBranch === (repository?.default_branch || 'main') ||
282+
currentBranch === fallbackBranch ||
263283
currentBranch === 'master'
264284
}
265285
onClick={() => {
@@ -277,7 +297,7 @@ const CodeTab: FC<CodeTabProps> = ({
277297
onClick={() => {
278298
handleMenuClose()
279299
setNewBranchName('')
280-
setNewBranchBase(currentBranch || repository?.default_branch || 'main')
300+
setNewBranchBase(currentBranch || fallbackBranch)
281301
setCreateBranchDialogOpen(true)
282302
}}
283303
>
@@ -578,7 +598,7 @@ const CodeTab: FC<CodeTabProps> = ({
578598
Are you sure you want to create a pull request for branch <strong>{currentBranch}</strong>?
579599
</Typography>
580600
<Typography variant="body2" color="text.secondary">
581-
This will create a pull request to merge changes from <strong>{currentBranch}</strong> into <strong>{repository?.default_branch || 'main'}</strong>.
601+
This will create a pull request to merge changes from <strong>{currentBranch}</strong> into <strong>{fallbackBranch}</strong>.
582602
</Typography>
583603
</Box>
584604
)}

frontend/src/pages/GitRepoDetail.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ const getTabName = (name: string | undefined): TabName => {
9696
return TAB_NAMES[0]
9797
}
9898

99+
const getFallbackBranch = (defaultBranch: string | undefined, branches: string[]): string => {
100+
if (branches.length === 0) {
101+
return ''
102+
}
103+
104+
if (branches.includes('main')) {
105+
return 'main'
106+
}
107+
if (branches.includes('master')) {
108+
return 'master'
109+
}
110+
111+
if (defaultBranch && branches.includes(defaultBranch)) {
112+
return defaultBranch
113+
}
114+
115+
return branches[0]
116+
}
117+
99118
const GitRepoDetail: FC = () => {
100119
const router = useRouter()
101120
const repoId = router.params.repoId
@@ -229,11 +248,11 @@ const GitRepoDetail: FC = () => {
229248

230249
// Auto-select default branch when repository loads and no branch is specified
231250
React.useEffect(() => {
232-
if (repository && !branchFromQuery) {
233-
const defaultBranch = repository.default_branch || 'main'
251+
if (repository && !branchFromQuery && branches.length > 0) {
252+
const defaultBranch = getFallbackBranch(repository.default_branch, branches)
234253
setCurrentBranch(defaultBranch)
235254
}
236-
}, [repository, branchFromQuery])
255+
}, [repository, branchFromQuery, branches])
237256

238257
// Auto-load README.md when repository loads
239258
React.useEffect(() => {
@@ -1100,8 +1119,8 @@ const GitRepoDetail: FC = () => {
11001119
</Box>
11011120
)}
11021121
>
1103-
<MenuItem value={repository?.default_branch || 'main'}>
1104-
{repository?.default_branch || 'main'}
1122+
<MenuItem value={getFallbackBranch(repository?.default_branch, branches)}>
1123+
{getFallbackBranch(repository?.default_branch, branches)}
11051124
</MenuItem>
11061125
{branches.filter(b => b !== repository?.default_branch).map((branch) => (
11071126
<MenuItem key={branch} value={branch}>

0 commit comments

Comments
 (0)