@@ -26,10 +26,12 @@ import type { GitRepository } from '../models/git-repository';
2626import { isWorkspace , type WorkspaceScope , WorkspaceScopeKeys } from '../models/workspace' ;
2727import { fsClient } from '../sync/git/fs-client' ;
2828import GitVCS , {
29+ fetchRemoteBranches ,
2930 GIT_CLONE_DIR ,
3031 GIT_INSOMNIA_DIR ,
3132 GIT_INSOMNIA_DIR_NAME ,
3233 GIT_INTERNAL_DIR ,
34+ type GitCredentials ,
3335 MergeConflictError ,
3436} from '../sync/git/git-vcs' ;
3537import { MemClient } from '../sync/git/mem-client' ;
@@ -189,6 +191,7 @@ export async function loadGitRepository({ projectId, workspaceId }: { projectId:
189191 try {
190192 const gitRepository = await getGitRepository ( { workspaceId, projectId } ) ;
191193
194+ const bufferId = await database . bufferChanges ( ) ;
192195 const fsClient = await getGitFSClient ( { gitRepositoryId : gitRepository . _id , projectId, workspaceId } ) ;
193196
194197 if ( GitVCS . isInitializedForRepo ( gitRepository . _id ) && ! gitRepository . needsFullClone ) {
@@ -241,6 +244,8 @@ export async function loadGitRepository({ projectId, workspaceId }: { projectId:
241244 legacyInsomniaWorkspace = await containsLegacyInsomniaDir ( { fsClient } ) ;
242245 }
243246
247+ await database . flushChanges ( bufferId ) ;
248+
244249 return {
245250 branch : await GitVCS . getCurrentBranch ( ) ,
246251 branches : await GitVCS . listBranches ( ) ,
@@ -591,6 +596,7 @@ export const initGitRepoCloneAction = async ({
591596 token,
592597 username,
593598 oauth2format,
599+ ref,
594600} : {
595601 organizationId : string ;
596602 uri : string ;
@@ -599,6 +605,7 @@ export const initGitRepoCloneAction = async ({
599605 token : string ;
600606 username : string ;
601607 oauth2format ?: string ;
608+ ref ?: string ;
602609} ) : Promise <
603610 | {
604611 files : {
@@ -647,6 +654,7 @@ export const initGitRepoCloneAction = async ({
647654
648655 try {
649656 await shallowClone ( {
657+ ref,
650658 fsClient : inMemoryFsClient ,
651659 gitRepository : repoSettingsPatch as GitRepository ,
652660 } ) ;
@@ -701,6 +709,7 @@ export const cloneGitRepoAction = async ({
701709 token,
702710 username,
703711 oauth2format,
712+ ref,
704713} : {
705714 organizationId : string ;
706715 projectId ?: string ;
@@ -712,6 +721,7 @@ export const cloneGitRepoAction = async ({
712721 token : string ;
713722 username : string ;
714723 oauth2format ?: string ;
724+ ref ?: string ;
715725} ) => {
716726 try {
717727 if ( ! projectId ) {
@@ -750,6 +760,7 @@ export const cloneGitRepoAction = async ({
750760
751761 try {
752762 await shallowClone ( {
763+ ref,
753764 fsClient : inMemoryFsClient ,
754765 gitRepository : repoSettingsPatch as GitRepository ,
755766 } ) ;
@@ -822,6 +833,7 @@ export const cloneGitRepoAction = async ({
822833 directory : GIT_CLONE_DIR ,
823834 fs : fsClient ,
824835 gitDirectory : GIT_INTERNAL_DIR ,
836+ ref,
825837 } ) ;
826838
827839 await models . gitRepository . update ( gitRepository , {
@@ -846,7 +858,10 @@ export const cloneGitRepoAction = async ({
846858 await migrateLegacyInsomniaFolderToFile ( { projectId : project . _id } ) ;
847859 }
848860
849- await models . gitRepository . update ( gitRepository , {
861+ const updateRepository = await models . gitRepository . getById ( gitRepository . _id ) ;
862+ invariant ( updateRepository , 'Git Repository not found' ) ;
863+
864+ await models . gitRepository . update ( updateRepository , {
850865 cachedGitLastCommitTime : Date . now ( ) ,
851866 cachedGitRepositoryBranch : await GitVCS . getCurrentBranch ( ) ,
852867 } ) ;
@@ -900,6 +915,7 @@ export const cloneGitRepoAction = async ({
900915 const providerName = getOauth2FormatName ( repoSettingsPatch . credentials ) ;
901916 try {
902917 await shallowClone ( {
918+ ref,
903919 fsClient : inMemoryFsClient ,
904920 gitRepository : repoSettingsPatch as GitRepository ,
905921 } ) ;
@@ -1089,6 +1105,7 @@ export const updateGitRepoAction = async ({
10891105 oauth2format,
10901106 username,
10911107 token,
1108+ ref,
10921109} : {
10931110 projectId : string ;
10941111 workspaceId ?: string ;
@@ -1098,6 +1115,7 @@ export const updateGitRepoAction = async ({
10981115 oauth2format ?: string ;
10991116 username : string ;
11001117 token : string ;
1118+ ref ?: string ;
11011119} ) => {
11021120 try {
11031121 let gitRepositoryId : string | null | undefined = null ;
@@ -1178,6 +1196,7 @@ export const updateGitRepoAction = async ({
11781196 gitDirectory : GIT_INTERNAL_DIR ,
11791197 gitCredentials : gitRepository . credentials ,
11801198 legacyDiff : Boolean ( workspaceId ) ,
1199+ ref,
11811200 } ) ;
11821201
11831202 await GitVCS . setAuthor ( ) ;
@@ -1704,6 +1723,26 @@ export const pushToGitRemoteAction = async ({
17041723 } ;
17051724} ;
17061725
1726+ export async function fetchGitRemoteBranches ( {
1727+ uri,
1728+ credentials,
1729+ } : {
1730+ uri : string ;
1731+ credentials ?: GitCredentials ;
1732+ } ) : Promise < { branches : string [ ] ; errors ?: string [ ] } > {
1733+ try {
1734+ const branches = await fetchRemoteBranches ( {
1735+ uri : parseGitToHttpsURL ( uri ) ,
1736+ credentials,
1737+ } ) ;
1738+
1739+ return { branches } ;
1740+ } catch ( err ) {
1741+ const errorMessage = err instanceof Error ? err . message : 'Error while fetching remote branches' ;
1742+ return { branches : [ ] , errors : [ errorMessage ] } ;
1743+ }
1744+ }
1745+
17071746export async function pullFromGitRemote ( { projectId, workspaceId } : { projectId : string ; workspaceId ?: string } ) {
17081747 try {
17091748 const gitRepository = await getGitRepository ( { projectId, workspaceId } ) ;
@@ -2472,7 +2511,7 @@ export interface GitServiceAPI {
24722511 diffFileLoader : typeof diffFileLoader ;
24732512 getRepositoryDirectoryTree : typeof getRepositoryDirectoryTree ;
24742513 migrateLegacyInsomniaFolderToFile : typeof migrateLegacyInsomniaFolderToFile ;
2475-
2514+ fetchGitRemoteBranches : typeof fetchGitRemoteBranches ;
24762515 initSignInToGitHub : typeof initSignInToGitHub ;
24772516 completeSignInToGitHub : typeof completeSignInToGitHub ;
24782517 signOutOfGitHub : typeof signOutOfGitHub ;
@@ -2489,6 +2528,9 @@ export const registerGitServiceAPI = () => {
24892528 loadGitRepository ( options ) ,
24902529 ) ;
24912530 ipcMainHandle ( 'git.getGitBranches' , ( _ , options : Parameters < typeof getGitBranches > [ 0 ] ) => getGitBranches ( options ) ) ;
2531+ ipcMainHandle ( 'git.fetchGitRemoteBranches' , ( _ , options : Parameters < typeof fetchGitRemoteBranches > [ 0 ] ) =>
2532+ fetchGitRemoteBranches ( options ) ,
2533+ ) ;
24922534 ipcMainHandle ( 'git.gitFetchAction' , ( _ , options : Parameters < typeof gitFetchAction > [ 0 ] ) => gitFetchAction ( options ) ) ;
24932535 ipcMainHandle ( 'git.gitLogLoader' , ( _ , options : Parameters < typeof gitLogLoader > [ 0 ] ) => gitLogLoader ( options ) ) ;
24942536 ipcMainHandle ( 'git.gitChangesLoader' , ( _ , options : Parameters < typeof gitChangesLoader > [ 0 ] ) =>
0 commit comments