CreateObject creates a new Git object, of the specified type, in the repository at `repoPath`. `writer` is a function that generates the object contents in `git hash-object` input format.
( t *testing.T, otype git.ObjectType, writer func(io.Writer) error, )
| 170 | // the repository at `repoPath`. `writer` is a function that generates |
| 171 | // the object contents in `git hash-object` input format. |
| 172 | func (repo *TestRepo) CreateObject( |
| 173 | t *testing.T, otype git.ObjectType, writer func(io.Writer) error, |
| 174 | ) git.OID { |
| 175 | t.Helper() |
| 176 | |
| 177 | cmd := repo.GitCommand(t, "hash-object", "-w", "-t", string(otype), "--stdin") |
| 178 | in, err := cmd.StdinPipe() |
| 179 | require.NoError(t, err) |
| 180 | |
| 181 | out, err := cmd.StdoutPipe() |
| 182 | require.NoError(t, err) |
| 183 | cmd.Stderr = os.Stderr |
| 184 | |
| 185 | err = cmd.Start() |
| 186 | require.NoError(t, err) |
| 187 | |
| 188 | err = writer(in) |
| 189 | err2 := in.Close() |
| 190 | if !assert.NoError(t, err) { |
| 191 | _ = cmd.Wait() |
| 192 | t.FailNow() |
| 193 | } |
| 194 | if !assert.NoError(t, err2) { |
| 195 | _ = cmd.Wait() |
| 196 | t.FailNow() |
| 197 | } |
| 198 | |
| 199 | output, err := io.ReadAll(out) |
| 200 | err2 = cmd.Wait() |
| 201 | require.NoError(t, err) |
| 202 | require.NoError(t, err2) |
| 203 | |
| 204 | oid, err := git.NewOID(string(bytes.TrimSpace(output))) |
| 205 | require.NoError(t, err) |
| 206 | return oid |
| 207 | } |
| 208 | |
| 209 | // AddFile adds and stages a file in `repo` at path `relativePath` |
| 210 | // with the specified `contents`. This must be run in a non-bare |