mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-30 19:08:37 +00:00 
			
		
		
		
	Use db.WithTx/WithTx2 instead of TxContext when possible (#35428)
This commit is contained in:
		| @@ -334,122 +334,111 @@ func FindRenamedBranch(ctx context.Context, repoID int64, from string) (branch * | ||||
|  | ||||
| // RenameBranch rename a branch | ||||
| func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(ctx context.Context, isDefault bool) error) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	// check whether from branch exist | ||||
| 	var branch Branch | ||||
| 	exist, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, from).Get(&branch) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} else if !exist || branch.IsDeleted { | ||||
| 		return ErrBranchNotExist{ | ||||
| 			RepoID:     repo.ID, | ||||
| 			BranchName: from, | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// check whether to branch exist or is_deleted | ||||
| 	var dstBranch Branch | ||||
| 	exist, err = db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, to).Get(&dstBranch) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if exist { | ||||
| 		if !dstBranch.IsDeleted { | ||||
| 			return ErrBranchAlreadyExists{ | ||||
| 				BranchName: to, | ||||
| 		// check whether from branch exist | ||||
| 		var branch Branch | ||||
| 		exist, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, from).Get(&branch) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} else if !exist || branch.IsDeleted { | ||||
| 			return ErrBranchNotExist{ | ||||
| 				RepoID:     repo.ID, | ||||
| 				BranchName: from, | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if _, err := db.GetEngine(ctx).ID(dstBranch.ID).NoAutoCondition().Delete(&dstBranch); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 1. update branch in database | ||||
| 	if n, err := sess.Where("repo_id=? AND name=?", repo.ID, from).Update(&Branch{ | ||||
| 		Name: to, | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} else if n <= 0 { | ||||
| 		return ErrBranchNotExist{ | ||||
| 			RepoID:     repo.ID, | ||||
| 			BranchName: from, | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 2. update default branch if needed | ||||
| 	isDefault := repo.DefaultBranch == from | ||||
| 	if isDefault { | ||||
| 		repo.DefaultBranch = to | ||||
| 		_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo) | ||||
| 		// check whether to branch exist or is_deleted | ||||
| 		var dstBranch Branch | ||||
| 		exist, err = db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, to).Get(&dstBranch) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 		if exist { | ||||
| 			if !dstBranch.IsDeleted { | ||||
| 				return ErrBranchAlreadyExists{ | ||||
| 					BranchName: to, | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 	// 3. Update protected branch if needed | ||||
| 	protectedBranch, err := GetProtectedBranchRuleByName(ctx, repo.ID, from) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 			if _, err := db.GetEngine(ctx).ID(dstBranch.ID).NoAutoCondition().Delete(&dstBranch); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if protectedBranch != nil { | ||||
| 		// there is a protect rule for this branch | ||||
| 		protectedBranch.RuleName = to | ||||
| 		_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch) | ||||
| 		// 1. update branch in database | ||||
| 		if n, err := sess.Where("repo_id=? AND name=?", repo.ID, from).Update(&Branch{ | ||||
| 			Name: to, | ||||
| 		}); err != nil { | ||||
| 			return err | ||||
| 		} else if n <= 0 { | ||||
| 			return ErrBranchNotExist{ | ||||
| 				RepoID:     repo.ID, | ||||
| 				BranchName: from, | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// 2. update default branch if needed | ||||
| 		isDefault := repo.DefaultBranch == from | ||||
| 		if isDefault { | ||||
| 			repo.DefaultBranch = to | ||||
| 			_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// 3. Update protected branch if needed | ||||
| 		protectedBranch, err := GetProtectedBranchRuleByName(ctx, repo.ID, from) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} else { | ||||
| 		// some glob protect rules may match this branch | ||||
| 		protected, err := IsBranchProtected(ctx, repo.ID, from) | ||||
|  | ||||
| 		if protectedBranch != nil { | ||||
| 			// there is a protect rule for this branch | ||||
| 			protectedBranch.RuleName = to | ||||
| 			if _, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} else { | ||||
| 			// some glob protect rules may match this branch | ||||
| 			protected, err := IsBranchProtected(ctx, repo.ID, from) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			if protected { | ||||
| 				return ErrBranchIsProtected | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// 4. Update all not merged pull request base branch name | ||||
| 		_, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?", | ||||
| 			repo.ID, from, false). | ||||
| 			Update(map[string]any{"base_branch": to}) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if protected { | ||||
| 			return ErrBranchIsProtected | ||||
|  | ||||
| 		// 4.1 Update all not merged pull request head branch name | ||||
| 		if _, err = sess.Table("pull_request").Where("head_repo_id=? AND head_branch=? AND has_merged=?", | ||||
| 			repo.ID, from, false). | ||||
| 			Update(map[string]any{"head_branch": to}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// 4. Update all not merged pull request base branch name | ||||
| 	_, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?", | ||||
| 		repo.ID, from, false). | ||||
| 		Update(map[string]any{"base_branch": to}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 		// 5. insert renamed branch record | ||||
| 		if err = db.Insert(ctx, &RenamedBranch{ | ||||
| 			RepoID: repo.ID, | ||||
| 			From:   from, | ||||
| 			To:     to, | ||||
| 		}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	// 4.1 Update all not merged pull request head branch name | ||||
| 	if _, err = sess.Table("pull_request").Where("head_repo_id=? AND head_branch=? AND has_merged=?", | ||||
| 		repo.ID, from, false). | ||||
| 		Update(map[string]any{"head_branch": to}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// 5. insert renamed branch record | ||||
| 	renamedBranch := &RenamedBranch{ | ||||
| 		RepoID: repo.ID, | ||||
| 		From:   from, | ||||
| 		To:     to, | ||||
| 	} | ||||
| 	err = db.Insert(ctx, renamedBranch) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// 6. do git action | ||||
| 	if err = gitAction(ctx, isDefault); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		// 6. do git action | ||||
| 		return gitAction(ctx, isDefault) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type FindRecentlyPushedNewBranchesOptions struct { | ||||
|   | ||||
| @@ -180,29 +180,25 @@ func RemoveLFSMetaObjectByOidFn(ctx context.Context, repoID int64, oid string, f | ||||
| 		return 0, ErrLFSObjectNotExist | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (int64, error) { | ||||
| 		m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repoID} | ||||
| 		if _, err := db.DeleteByBean(ctx, m); err != nil { | ||||
| 			return -1, err | ||||
| 		} | ||||
|  | ||||
| 	m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repoID} | ||||
| 	if _, err := db.DeleteByBean(ctx, m); err != nil { | ||||
| 		return -1, err | ||||
| 	} | ||||
|  | ||||
| 	count, err := db.CountByBean(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) | ||||
| 	if err != nil { | ||||
| 		return count, err | ||||
| 	} | ||||
|  | ||||
| 	if fn != nil { | ||||
| 		if err := fn(count); err != nil { | ||||
| 		count, err := db.CountByBean(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) | ||||
| 		if err != nil { | ||||
| 			return count, err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return count, committer.Commit() | ||||
| 		if fn != nil { | ||||
| 			if err := fn(count); err != nil { | ||||
| 				return count, err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		return count, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetLFSMetaObjects returns all LFSMetaObjects associated with a repository | ||||
| @@ -243,56 +239,46 @@ func ExistsLFSObject(ctx context.Context, oid string) (bool, error) { | ||||
|  | ||||
| // LFSAutoAssociate auto associates accessible LFSMetaObjects | ||||
| func LFSAutoAssociate(ctx context.Context, metas []*LFSMetaObject, user *user_model.User, repoID int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		oids := make([]any, len(metas)) | ||||
| 		oidMap := make(map[string]*LFSMetaObject, len(metas)) | ||||
| 		for i, meta := range metas { | ||||
| 			oids[i] = meta.Oid | ||||
| 			oidMap[meta.Oid] = meta | ||||
| 		} | ||||
|  | ||||
| 	sess := db.GetEngine(ctx) | ||||
| 		if !user.IsAdmin { | ||||
| 			newMetas := make([]*LFSMetaObject, 0, len(metas)) | ||||
| 			cond := builder.In( | ||||
| 				"`lfs_meta_object`.repository_id", | ||||
| 				builder.Select("`repository`.id").From("repository").Where(repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)), | ||||
| 			) | ||||
| 			if err := db.GetEngine(ctx).Cols("oid").Where(cond).In("oid", oids...).GroupBy("oid").Find(&newMetas); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			if len(newMetas) != len(oidMap) { | ||||
| 				return fmt.Errorf("unable collect all LFS objects from database, expected %d, actually %d", len(oidMap), len(newMetas)) | ||||
| 			} | ||||
| 			for i := range newMetas { | ||||
| 				newMetas[i].Size = oidMap[newMetas[i].Oid].Size | ||||
| 				newMetas[i].RepositoryID = repoID | ||||
| 			} | ||||
| 			return db.Insert(ctx, newMetas) | ||||
| 		} | ||||
|  | ||||
| 	oids := make([]any, len(metas)) | ||||
| 	oidMap := make(map[string]*LFSMetaObject, len(metas)) | ||||
| 	for i, meta := range metas { | ||||
| 		oids[i] = meta.Oid | ||||
| 		oidMap[meta.Oid] = meta | ||||
| 	} | ||||
|  | ||||
| 	if !user.IsAdmin { | ||||
| 		newMetas := make([]*LFSMetaObject, 0, len(metas)) | ||||
| 		cond := builder.In( | ||||
| 			"`lfs_meta_object`.repository_id", | ||||
| 			builder.Select("`repository`.id").From("repository").Where(repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)), | ||||
| 		) | ||||
| 		err = sess.Cols("oid").Where(cond).In("oid", oids...).GroupBy("oid").Find(&newMetas) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if len(newMetas) != len(oidMap) { | ||||
| 			return fmt.Errorf("unable collect all LFS objects from database, expected %d, actually %d", len(oidMap), len(newMetas)) | ||||
| 		} | ||||
| 		for i := range newMetas { | ||||
| 			newMetas[i].Size = oidMap[newMetas[i].Oid].Size | ||||
| 			newMetas[i].RepositoryID = repoID | ||||
| 		} | ||||
| 		if err = db.Insert(ctx, newMetas); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} else { | ||||
| 		// admin can associate any LFS object to any repository, and we do not care about errors (eg: duplicated unique key), | ||||
| 		// even if error occurs, it won't hurt users and won't make things worse | ||||
| 		for i := range metas { | ||||
| 			p := lfs.Pointer{Oid: metas[i].Oid, Size: metas[i].Size} | ||||
| 			_, err = sess.Insert(&LFSMetaObject{ | ||||
| 			if err := db.Insert(ctx, &LFSMetaObject{ | ||||
| 				Pointer:      p, | ||||
| 				RepositoryID: repoID, | ||||
| 			}) | ||||
| 			if err != nil { | ||||
| 			}); err != nil { | ||||
| 				log.Warn("failed to insert LFS meta object %-v for repo_id: %d into database, err=%v", p, repoID, err) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // CopyLFS copies LFS data from one repo to another | ||||
|   | ||||
		Reference in New Issue
	
	Block a user