@@ -35,9 +35,20 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
3535 throw new InvalidOperationException ( "GitHub client is not configured." ) ;
3636 }
3737
38- var fileContent = await gitHubClient . Repository . Content . GetRawContent ( owner , repo , path ) ;
39- var fileContentString = System . Text . Encoding . UTF8 . GetString ( fileContent ) ;
40- return fileContentString ;
38+ try
39+ {
40+ var fileContent = await gitHubClient . Repository . Content . GetRawContent ( owner , repo , path ) ;
41+ var fileContentString = System . Text . Encoding . UTF8 . GetString ( fileContent ) ;
42+ return fileContentString ;
43+ }
44+ catch ( NotFoundException ex )
45+ {
46+ throw new FileNotFoundException ( $ "File not found at path '{ path } ' in repository '{ owner } /{ repo } '.", ex ) ;
47+ }
48+ catch ( Exception ex )
49+ {
50+ throw new InvalidOperationException ( $ "Failed to retrieve file content from '{ path } ' in repository '{ owner } /{ repo } ': { ex . Message } ", ex ) ;
51+ }
4152 }
4253
4354 public static async Task < RepositoryContentChangeSet ? > SaveFileAsync (
@@ -121,12 +132,26 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
121132 }
122133 catch ( NotFoundException )
123134 {
124- // File does not exist at 'path' (GetAllContentsByRef threw NotFoundException)
125- // Create the file at 'pathForCreationOrNonExistent'
135+ // If no file was found, it was probably already moved using the newPath logic above
136+ if ( newPath != null && pathForCreationOrNonExistent == newPath )
137+ {
138+ var existingFile = await gitHubClient . Repository . Content . GetAllContentsByRef ( owner , repo , pathForCreationOrNonExistent , branch ) ;
139+
140+ if ( existingFile != null && existingFile . Count > 0 )
141+ {
142+ var updateFileRequest = new UpdateFileRequest (
143+ $ "Updating file { pathForCreationOrNonExistent } ",
144+ content ,
145+ existingFile [ 0 ] . Sha ,
146+ branch ) ;
147+ return await gitHubClient . Repository . Content . UpdateFile ( owner , repo , pathForCreationOrNonExistent , updateFileRequest ) ;
148+ }
149+ }
150+
126151 var createFileRequest = new CreateFileRequest (
127- $ "Adding file { pathForCreationOrNonExistent } ",
128- content ,
129- branch ) ;
152+ $ "Adding file { pathForCreationOrNonExistent } ",
153+ content ,
154+ branch ) ;
130155 return await gitHubClient . Repository . Content . CreateFile ( owner , repo , pathForCreationOrNonExistent , createFileRequest ) ;
131156 }
132157 }
@@ -206,4 +231,27 @@ public static async Task<Dictionary<string, string>> GetCommitFilesWithContentAs
206231
207232 return fileContents ;
208233 }
234+
235+ public static async Task < string > GetDefaultBranchAsync ( this IGitHubClient gitHubClient , string owner , string repo )
236+ {
237+ if ( gitHubClient == null )
238+ {
239+ throw new InvalidOperationException ( "GitHub client is not configured." ) ;
240+ }
241+
242+ try
243+ {
244+ // Fetch repository information which includes the default branch
245+ var repository = await gitHubClient . Repository . Get ( owner , repo ) ;
246+ return repository . DefaultBranch ;
247+ }
248+ catch ( NotFoundException )
249+ {
250+ throw new InvalidOperationException ( $ "Repository '{ owner } /{ repo } ' not found or not accessible.") ;
251+ }
252+ catch ( Exception ex )
253+ {
254+ throw new InvalidOperationException ( $ "Failed to retrieve default branch for repository '{ owner } /{ repo } ': { ex . Message } ", ex ) ;
255+ }
256+ }
209257}
0 commit comments