@@ -279,6 +279,37 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
279279 return "" , err
280280 }
281281
282+ // Create the branch on GitHub using REST API
283+ ctx := context .Background ()
284+ ts := oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : githubToken })
285+ tc := oauth2 .NewClient (ctx , ts )
286+ restClient := github .NewClient (tc )
287+
288+ owner , repoName , _ , err := GetOwnerRepoDefaultBranchFromLocalRepo (repoPath )
289+ if err != nil {
290+ return "" , fmt .Errorf ("failed to get repo info: %w" , err )
291+ }
292+
293+ // Get the reference of the current HEAD (which should be the base branch)
294+ headRef , err := repo .Head ()
295+ if err != nil {
296+ return "" , fmt .Errorf ("failed to get HEAD reference: %w" , err )
297+ }
298+ baseSHA := headRef .Hash ().String ()
299+
300+ // Create new branch reference on GitHub
301+ newRef := & github.Reference {
302+ Ref : github .Ptr ("refs/heads/" + branch ),
303+ Object : & github.GitObject {
304+ SHA : github .Ptr (baseSHA ),
305+ },
306+ }
307+
308+ _ , _ , err = restClient .Git .CreateRef (ctx , owner , repoName , newRef )
309+ if err != nil {
310+ return "" , fmt .Errorf ("failed to create branch on GitHub: %w" , err )
311+ }
312+
282313 // Inspired by https://github.com/planetscale/ghcommit/tree/main
283314
284315 // process added / modified files:
@@ -358,17 +389,8 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
358389 body = splitMsg [1 ]
359390 }
360391
361- owner , repoName , _ , err := GetOwnerRepoDefaultBranchFromLocalRepo (repoPath )
362- if err != nil {
363- return "" , err
364- }
365-
366- // Get HEAD reference to get the current commit hash
367- headRef , err := repo .Head ()
368- if err != nil {
369- return "" , fmt .Errorf ("failed to get HEAD reference: %w" , err )
370- }
371- expectedHeadOid := headRef .Hash ().String ()
392+ // Use the baseSHA from the branch creation as the expected head OID
393+ expectedHeadOid := baseSHA
372394 // create the $input struct for the graphQL createCommitOnBranch mutation request:
373395 input := githubv4.CreateCommitOnBranchInput {
374396 Branch : githubv4.CommittableBranch {
@@ -387,7 +409,7 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
387409 }
388410
389411 if err := graphqlClient .Mutate (context .Background (), & m , input , nil ); err != nil {
390- return "" , err
412+ return "" , fmt . Errorf ( "failed to create commit: %w" , err )
391413 }
392414
393415 return m .CreateCommitOnBranch .Commit .OID , nil
0 commit comments