MCPcopy Create free account
hub / github.com/gogs/gogs / DeleteRepoFile

Method DeleteRepoFile

internal/database/repo_editor.go:319–388  ·  view source on GitHub ↗
(doer *User, opts DeleteRepoFileOptions)

Source from the content-addressed store, hash-verified

317}
318
319func (r *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
320 // 🚨 SECURITY: Prevent uploading files into the ".git" directory.
321 if isRepositoryGitPath(opts.TreePath) {
322 return errors.Errorf("bad tree path %q", opts.TreePath)
323 }
324
325 repoWorkingPool.CheckIn(com.ToStr(r.ID))
326 defer repoWorkingPool.CheckOut(com.ToStr(r.ID))
327
328 if err = r.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
329 return errors.Newf("discard local r branch[%s] changes: %v", opts.OldBranch, err)
330 } else if err = r.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
331 return errors.Newf("update local copy branch[%s]: %v", opts.OldBranch, err)
332 }
333
334 localPath := r.LocalCopyPath()
335
336 // 🚨 SECURITY: Prevent touching files in surprising places, reject operations
337 // that involve symlinks.
338 if hasSymlinkInPath(localPath, opts.TreePath) {
339 return errors.New("cannot update file with symbolic link in path")
340 }
341
342 if opts.OldBranch != opts.NewBranch {
343 if err := r.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
344 return errors.Newf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
345 }
346 }
347
348 filePath := path.Join(localPath, opts.TreePath)
349 if err = os.Remove(filePath); err != nil {
350 return errors.Newf("remove file %q: %v", opts.TreePath, err)
351 }
352
353 if err = git.Add(localPath, git.AddOptions{All: true}); err != nil {
354 return errors.Newf("git add --all: %v", err)
355 }
356
357 err = git.CreateCommit(
358 localPath,
359 &git.Signature{
360 Name: doer.DisplayName(),
361 Email: doer.Email,
362 When: time.Now(),
363 },
364 opts.Message,
365 )
366 if err != nil {
367 return errors.Newf("commit changes to %q: %v", localPath, err)
368 }
369
370 err = git.Push(localPath, "origin", opts.NewBranch,
371 git.PushOptions{
372 CommandOptions: git.CommandOptions{
373 Envs: ComposeHookEnvs(ComposeHookEnvsOptions{
374 AuthUser: doer,
375 OwnerName: r.MustOwner().Name,
376 OwnerSalt: r.MustOwner().Salt,

Callers 1

DeleteFilePostFunction · 0.80

Implementers 2

Repositoryinternal/database/repo.go
mailerRepointernal/database/issue_mail.go

Calls 15

UpdateLocalCopyBranchMethod · 0.95
LocalCopyPathMethod · 0.95
CheckoutNewBranchMethod · 0.95
MustOwnerMethod · 0.95
RepoPathMethod · 0.95
isRepositoryGitPathFunction · 0.85
hasSymlinkInPathFunction · 0.85
ComposeHookEnvsFunction · 0.85
CheckInMethod · 0.80
CheckOutMethod · 0.80
RemoveMethod · 0.80

Tested by

no test coverage detected