@@ -37,16 +37,16 @@ func (o GitObject) String() string {
37
37
return Stringify (o )
38
38
}
39
39
40
- // createRefRequest represents the payload for creating a reference.
41
- type createRefRequest struct {
42
- Ref * string `json:"ref"`
43
- SHA * string `json:"sha"`
40
+ // CreateRef represents the payload for creating a reference.
41
+ type CreateRef struct {
42
+ Ref string `json:"ref"`
43
+ SHA string `json:"sha"`
44
44
}
45
45
46
- // updateRefRequest represents the payload for updating a reference.
47
- type updateRefRequest struct {
48
- SHA * string `json:"sha"`
49
- Force * bool `json:"force"`
46
+ // UpdateRef represents the payload for updating a reference.
47
+ type UpdateRef struct {
48
+ SHA string `json:"sha"`
49
+ Force * bool `json:"force,omitempty "`
50
50
}
51
51
52
52
// GetRef fetches a single reference in a repository.
@@ -123,24 +123,26 @@ func (s *GitService) ListMatchingRefs(ctx context.Context, owner, repo string, o
123
123
}
124
124
125
125
// CreateRef creates a new ref in a repository.
126
+ // The ref field must include the 'refs/' prefix (e.g., 'refs/heads/branch-name').
126
127
//
127
128
// GitHub API docs: https://docs.github.com/rest/git/refs#create-a-reference
128
129
//
129
130
//meta:operation POST /repos/{owner}/{repo}/git/refs
130
- func (s * GitService ) CreateRef (ctx context.Context , owner , repo string , ref * Reference ) (* Reference , * Response , error ) {
131
- if ref == nil {
132
- return nil , nil , errors .New ("reference must be provided" )
133
- }
134
- if ref .Ref == nil {
131
+ func (s * GitService ) CreateRef (ctx context.Context , owner , repo string , ref CreateRef ) (* Reference , * Response , error ) {
132
+ if ref .Ref == "" {
135
133
return nil , nil , errors .New ("ref must be provided" )
136
134
}
137
135
136
+ if ref .SHA == "" {
137
+ return nil , nil , errors .New ("sha must be provided" )
138
+ }
139
+
140
+ if ! strings .HasPrefix (ref .Ref , "refs/" ) {
141
+ return nil , nil , errors .New ("ref must start with 'refs/' prefix" )
142
+ }
143
+
138
144
u := fmt .Sprintf ("repos/%v/%v/git/refs" , owner , repo )
139
- req , err := s .client .NewRequest ("POST" , u , & createRefRequest {
140
- // back-compat with previous behavior that didn't require 'refs/' prefix
141
- Ref : Ptr ("refs/" + strings .TrimPrefix (* ref .Ref , "refs/" )),
142
- SHA : ref .Object .SHA ,
143
- })
145
+ req , err := s .client .NewRequest ("POST" , u , ref )
144
146
if err != nil {
145
147
return nil , nil , err
146
148
}
@@ -159,20 +161,18 @@ func (s *GitService) CreateRef(ctx context.Context, owner, repo string, ref *Ref
159
161
// GitHub API docs: https://docs.github.com/rest/git/refs#update-a-reference
160
162
//
161
163
//meta:operation PATCH /repos/{owner}/{repo}/git/refs/{ref}
162
- func (s * GitService ) UpdateRef (ctx context.Context , owner , repo string , ref * Reference , force bool ) (* Reference , * Response , error ) {
163
- if ref == nil {
164
- return nil , nil , errors .New ("reference must be provided" )
165
- }
166
- if ref .Ref == nil {
164
+ func (s * GitService ) UpdateRef (ctx context.Context , owner , repo , ref string , updateRef UpdateRef ) (* Reference , * Response , error ) {
165
+ if ref == "" {
167
166
return nil , nil , errors .New ("ref must be provided" )
168
167
}
169
168
170
- refPath := strings .TrimPrefix (* ref .Ref , "refs/" )
169
+ if updateRef .SHA == "" {
170
+ return nil , nil , errors .New ("sha must be provided" )
171
+ }
172
+
173
+ refPath := strings .TrimPrefix (ref , "refs/" )
171
174
u := fmt .Sprintf ("repos/%v/%v/git/refs/%v" , owner , repo , refURLEscape (refPath ))
172
- req , err := s .client .NewRequest ("PATCH" , u , & updateRefRequest {
173
- SHA : ref .Object .SHA ,
174
- Force : & force ,
175
- })
175
+ req , err := s .client .NewRequest ("PATCH" , u , updateRef )
176
176
if err != nil {
177
177
return nil , nil , err
178
178
}
0 commit comments