@@ -39,6 +39,7 @@ import type {
39
39
ScmRepository ,
40
40
} from '../../../../git/gitProvider' ;
41
41
import { GitUri } from '../../../../git/gitUri' ;
42
+ import { decodeRemoteHubAuthority } from '../../../../git/gitUri.authority' ;
42
43
import type { GitBlame , GitBlameAuthor , GitBlameLine , GitBlameLines } from '../../../../git/models/blame' ;
43
44
import type { BranchSortOptions } from '../../../../git/models/branch' ;
44
45
import { getBranchId , getBranchNameWithoutRemote , GitBranch , sortBranches } from '../../../../git/models/branch' ;
@@ -103,7 +104,7 @@ import { serializeWebviewItemContext } from '../../../../system/webview';
103
104
import type { CachedBlame , CachedLog , TrackedGitDocument } from '../../../../trackers/trackedDocument' ;
104
105
import { GitDocumentState } from '../../../../trackers/trackedDocument' ;
105
106
import type { GitHubAuthorityMetadata , Metadata , RemoteHubApi } from '../../../remotehub' ;
106
- import { getRemoteHubApi , HeadType } from '../../../remotehub' ;
107
+ import { getRemoteHubApi , HeadType , RepositoryRefType } from '../../../remotehub' ;
107
108
import type {
108
109
GraphBranchContextValue ,
109
110
GraphItemContext ,
@@ -116,6 +117,7 @@ import type {
116
117
} from '../../authentication/integrationAuthentication' ;
117
118
import { HostingIntegrationId } from '../models' ;
118
119
import type { GitHubApi } from './github' ;
120
+ import type { GitHubBranch } from './models' ;
119
121
import { fromCommitFileStatus } from './models' ;
120
122
121
123
const doubleQuoteRegex = / " / g;
@@ -924,7 +926,36 @@ export class GitHubGitProvider implements GitProvider, Disposable {
924
926
const {
925
927
values : [ branch ] ,
926
928
} = await this . getBranches ( repoPath , { filter : b => b . current } ) ;
927
- return branch ;
929
+ if ( branch != null ) return branch ;
930
+
931
+ try {
932
+ const { metadata } = await this . ensureRepositoryContext ( repoPath ! ) ;
933
+
934
+ const revision = await metadata . getRevision ( ) ;
935
+ switch ( revision . type ) {
936
+ case HeadType . Tag :
937
+ case HeadType . Commit :
938
+ return new GitBranch (
939
+ this . container ,
940
+ repoPath ! ,
941
+ revision . name ,
942
+ false ,
943
+ true ,
944
+ undefined ,
945
+ revision . revision ,
946
+ undefined ,
947
+ undefined ,
948
+ undefined ,
949
+ true ,
950
+ ) ;
951
+ }
952
+
953
+ return undefined ;
954
+ } catch ( ex ) {
955
+ debugger ;
956
+ Logger . error ( ex , getLogScope ( ) ) ;
957
+ return undefined ;
958
+ }
928
959
}
929
960
930
961
@log ( { args : { 1 : false } } )
@@ -946,11 +977,39 @@ export class GitHubGitProvider implements GitProvider, Disposable {
946
977
try {
947
978
const { metadata, github, session } = await this . ensureRepositoryContext ( repoPath ! ) ;
948
979
949
- const revision = await metadata . getRevision ( ) ;
950
- const current = revision . type === 0 /* HeadType.Branch */ ? revision . name : undefined ;
951
-
952
980
const branches : GitBranch [ ] = [ ] ;
953
981
982
+ function addBranches ( container : Container , branch : GitHubBranch , current : boolean ) {
983
+ const date = new Date (
984
+ configuration . get ( 'advanced.commitOrdering' ) === 'author-date'
985
+ ? branch . target . authoredDate
986
+ : branch . target . committedDate ,
987
+ ) ;
988
+ const ref = branch . target . oid ;
989
+
990
+ branches . push (
991
+ new GitBranch ( container , repoPath ! , branch . name , false , current , date , ref , {
992
+ name : `origin/${ branch . name } ` ,
993
+ missing : false ,
994
+ } ) ,
995
+ new GitBranch ( container , repoPath ! , `origin/${ branch . name } ` , true , false , date , ref ) ,
996
+ ) ;
997
+ }
998
+
999
+ let currentBranch : string | undefined ;
1000
+
1001
+ const revision = await metadata . getRevision ( ) ;
1002
+ switch ( revision . type ) {
1003
+ case HeadType . Branch :
1004
+ currentBranch = revision . name ;
1005
+ break ;
1006
+ case HeadType . RemoteBranch : {
1007
+ const index = revision . name . indexOf ( ':' ) ;
1008
+ currentBranch = index === - 1 ? revision . name : revision . name . substring ( index + 1 ) ;
1009
+ break ;
1010
+ }
1011
+ }
1012
+
954
1013
let cursor = options ?. paging ?. cursor ;
955
1014
const loadAll = cursor == null ;
956
1015
@@ -963,37 +1022,7 @@ export class GitHubGitProvider implements GitProvider, Disposable {
963
1022
) ;
964
1023
965
1024
for ( const branch of result . values ) {
966
- const date = new Date (
967
- configuration . get ( 'advanced.commitOrdering' ) === 'author-date'
968
- ? branch . target . authoredDate
969
- : branch . target . committedDate ,
970
- ) ;
971
- const ref = branch . target . oid ;
972
-
973
- branches . push (
974
- new GitBranch (
975
- this . container ,
976
- repoPath ! ,
977
- branch . name ,
978
- false ,
979
- branch . name === current ,
980
- date ,
981
- ref ,
982
- {
983
- name : `origin/${ branch . name } ` ,
984
- missing : false ,
985
- } ,
986
- ) ,
987
- new GitBranch (
988
- this . container ,
989
- repoPath ! ,
990
- `origin/${ branch . name } ` ,
991
- true ,
992
- false ,
993
- date ,
994
- ref ,
995
- ) ,
996
- ) ;
1025
+ addBranches ( this . container , branch , branch . name === currentBranch ) ;
997
1026
}
998
1027
999
1028
if ( ! result . paging ?. more || ! loadAll ) return { ...result , values : branches } ;
@@ -3436,6 +3465,18 @@ export class GitHubGitProvider implements GitProvider, Disposable {
3436
3465
throw new OpenVirtualRepositoryError ( repoPath , OpenVirtualRepositoryErrorReason . NotAGitHubRepository ) ;
3437
3466
}
3438
3467
3468
+ const data = decodeRemoteHubAuthority < GitHubAuthorityMetadata > ( uri . authority ) ;
3469
+ // If the virtual repository is opened to a PR, then we need to ensure the owner is the owner of the current branch
3470
+ if ( data . metadata ?. ref ?. type === RepositoryRefType . PullRequest ) {
3471
+ const revision = await metadata . getRevision ( ) ;
3472
+ if ( revision . type === HeadType . RemoteBranch ) {
3473
+ const [ remote ] = revision . name . split ( ':' ) ;
3474
+ if ( remote !== metadata . repo . owner ) {
3475
+ metadata . repo . owner = remote ;
3476
+ }
3477
+ }
3478
+ }
3479
+
3439
3480
let github ;
3440
3481
let session ;
3441
3482
try {
0 commit comments