| 277 | } |
| 278 | |
| 279 | func GitFetch(ctx context.Context, path string, config *Config) error { |
| 280 | repo, err := git.PlainOpen(path) |
| 281 | if err != nil { |
| 282 | return fmt.Errorf("failed to open git repository: %w", err) |
| 283 | } |
| 284 | if config == nil || config.Username == "" { |
| 285 | // uses default git configuration |
| 286 | // go-git does not support fetching from a private repo without auth |
| 287 | // so we will trigger the git command directly |
| 288 | return RunGitFetch(ctx, path, "origin") |
| 289 | } |
| 290 | err = repo.FetchContext(ctx, &git.FetchOptions{ |
| 291 | RemoteName: config.RemoteName(), |
| 292 | RemoteURL: config.Remote, |
| 293 | Auth: &githttp.BasicAuth{ |
| 294 | Username: config.Username, |
| 295 | Password: config.Password, |
| 296 | }, |
| 297 | }) |
| 298 | if err != nil { |
| 299 | if errors.Is(err, git.NoErrAlreadyUpToDate) { |
| 300 | // no new changes to fetch, this is not an error |
| 301 | return nil |
| 302 | } |
| 303 | return fmt.Errorf("failed to fetch from remote: %w", err) |
| 304 | } |
| 305 | return nil |
| 306 | } |
| 307 | |
| 308 | // SetRemote sets the remote by name Rill for the given repository to the provided remote URL. |
| 309 | func SetRemote(path string, config *Config) error { |