commitToDefaultBranch auto-commits any current changes to the default branch of the repository. This is only allowed if editable is true. This is done to checkpoint progress when the handle is closed.
(ctx context.Context, message string, force bool)
| 230 | // commitToDefaultBranch auto-commits any current changes to the default branch of the repository. This is only allowed if editable is true. |
| 231 | // This is done to checkpoint progress when the handle is closed. |
| 232 | func (r *gitRepo) commitToDefaultBranch(ctx context.Context, message string, force bool) error { |
| 233 | if !r.editable() { |
| 234 | return fmt.Errorf("cannot commit to the default branch because it is not configured") |
| 235 | } |
| 236 | |
| 237 | r.h.logger.Info("commitToDefaultBranch", observability.ZapCtx(ctx)) |
| 238 | |
| 239 | _, err := gitutil.CommitAll(ctx, r.repoDir, r.subpath, message, "Rill", "noreply@rilldata.com") |
| 240 | if err != nil { |
| 241 | if !errors.Is(err, gitutil.ErrEmptyCommit) { |
| 242 | return fmt.Errorf("failed to commit changes to edit branch: %w", err) |
| 243 | } |
| 244 | // continue to push existing commits, if any |
| 245 | } |
| 246 | |
| 247 | err = r.fetchCurrentBranch(ctx) |
| 248 | if err != nil { |
| 249 | return fmt.Errorf("failed to fetch default branch: %w", err) |
| 250 | } |
| 251 | |
| 252 | if !force { |
| 253 | err = gitutil.MergeWithStrategy(r.repoDir, fmt.Sprintf("%s/%s", "origin", r.defaultBranch), "") |
| 254 | if err != nil { |
| 255 | return fmt.Errorf("local is behind remote and failed to sync with remote: %w", err) |
| 256 | } |
| 257 | err = r.pushBranch(ctx, r.defaultBranch) |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | return nil |
| 262 | } |
| 263 | |
| 264 | // Instead of a force push, we do a merge with 'ours' strategy to ensure we don't lose history. |
| 265 | // This is not equivalent to a force push but is safer for users. |
| 266 | if r.subpath != "" { |
| 267 | // force pushing in a monorepo can overwrite other subpaths so just try with normal push |
| 268 | // we can check for changes in other subpaths but it is tricky and error prone |
| 269 | // monorepo setups are advanced use cases and we can require users to manually resolve remote changes |
| 270 | err = r.pushBranch(ctx, r.defaultBranch) |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | return nil |
| 275 | } |
| 276 | err = gitutil.MergeWithStrategy(r.repoDir, fmt.Sprintf("%s/%s", "origin", r.defaultBranch), "ours") |
| 277 | if err != nil { |
| 278 | return fmt.Errorf("local is behind remote and failed to sync with remote: %w", err) |
| 279 | } |
| 280 | err = r.pushBranch(ctx, r.defaultBranch) |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | // mergeToBranch commits changes to the repository and merges them into the specified branch. |
| 288 | func (r *gitRepo) mergeToBranch(ctx context.Context, branch string, force bool) (resErr error) { |