Skip to content

Commit 72544d6

Browse files
committed
Move redirection handling to private serv router
1 parent d56d255 commit 72544d6

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

cmd/serv.go

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
git_model "code.gitea.io/gitea/models/git"
2121
"code.gitea.io/gitea/models/perm"
2222
"code.gitea.io/gitea/models/repo"
23-
user_model "code.gitea.io/gitea/models/user"
2423
"code.gitea.io/gitea/modules/git"
2524
"code.gitea.io/gitea/modules/json"
2625
"code.gitea.io/gitea/modules/lfstransfer"
@@ -228,50 +227,8 @@ func runServ(ctx context.Context, c *cli.Command) error {
228227
}
229228

230229
username := repoPathFields[0]
231-
var userID int64
232-
233-
if err := initDB(ctx); err != nil {
234-
return fail(ctx, "DB initialization error", "Error while initializing Gitea database")
235-
}
236-
237-
redirectedUserID, err := user_model.LookupUserRedirect(ctx, username)
238-
if err == nil {
239-
userID = redirectedUserID
240-
} else {
241-
user, err := user_model.GetUserByName(ctx, username)
242-
if err == nil {
243-
userID = user.ID
244-
} else {
245-
return fail(ctx, "Invalid username", "Could not find user or org: %s", username)
246-
}
247-
}
248-
249-
// We need the uid for repo redirect lookup
250-
real_user, err := user_model.GetUserByID(ctx, userID)
251-
if err == nil {
252-
username = real_user.Name
253-
} else {
254-
return fail(ctx, "User ID lookup failed", "Could not find user with ID: %d", userID)
255-
}
256-
257230
reponame := strings.TrimSuffix(repoPathFields[1], ".git") // “the-repo-name" or "the-repo-name.wiki"
258231

259-
redirectedRepoID, err := repo.LookupRedirect(ctx, userID, reponame)
260-
if err == nil {
261-
redirectedRepo, err := repo.GetRepositoryByID(ctx, redirectedRepoID)
262-
if err == nil {
263-
reponame = redirectedRepo.Name
264-
username = redirectedRepo.OwnerName
265-
} else {
266-
return fail(ctx, "Repo ID lookup failed", "Could not find repo with ID: %d", redirectedRepoID)
267-
}
268-
}
269-
270-
// LowerCase and trim the repoPath as that's how they are stored.
271-
// This should be done after splitting the repoPath into username and reponame
272-
// so that username and reponame are not affected.
273-
repoPath = strings.ToLower(username + "/" + reponame)
274-
275232
if !repo.IsValidSSHAccessRepoName(reponame) {
276233
return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
277234
}
@@ -318,6 +275,11 @@ func runServ(ctx context.Context, c *cli.Command) error {
318275
return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
319276
}
320277

278+
// LowerCase and trim the repoPath as that's how they are stored.
279+
// This should be done after splitting the repoPath into username and reponame
280+
// so that username and reponame are not affected.
281+
repoPath = strings.ToLower(results.OwnerName + "/" + results.RepoName)
282+
321283
// LFS SSH protocol
322284
if verb == git.CmdVerbLfsTransfer {
323285
token, err := getLFSAuthToken(ctx, lfsVerb, results)

routers/private/serv.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
asymkey_model "code.gitea.io/gitea/models/asymkey"
1212
"code.gitea.io/gitea/models/perm"
1313
access_model "code.gitea.io/gitea/models/perm/access"
14+
"code.gitea.io/gitea/models/repo"
1415
repo_model "code.gitea.io/gitea/models/repo"
1516
"code.gitea.io/gitea/models/unit"
1617
user_model "code.gitea.io/gitea/models/user"
@@ -108,6 +109,18 @@ func ServCommand(ctx *context.PrivateContext) {
108109
results.RepoName = repoName[:len(repoName)-5]
109110
}
110111

112+
// Check if there is a user redirect for the requested owner
113+
redirectedUserID, err := user_model.LookupUserRedirect(ctx, results.OwnerName)
114+
if err == nil {
115+
owner, err := user_model.GetUserByID(ctx, redirectedUserID)
116+
if err == nil {
117+
log.Info("User %s has been redirected to %s", results.OwnerName, owner.Name)
118+
results.OwnerName = owner.Name
119+
} else {
120+
log.Warn("User %s has a redirect to user with ID %d, but no user with this ID could be found. Trying without redirect...", results.OwnerName, redirectedUserID)
121+
}
122+
}
123+
111124
owner, err := user_model.GetUserByName(ctx, results.OwnerName)
112125
if err != nil {
113126
if user_model.IsErrUserNotExist(err) {
@@ -131,6 +144,19 @@ func ServCommand(ctx *context.PrivateContext) {
131144
return
132145
}
133146

147+
redirectedRepoID, err := repo_model.LookupRedirect(ctx, owner.ID, results.RepoName)
148+
if err == nil {
149+
redirectedRepo, err := repo.GetRepositoryByID(ctx, redirectedRepoID)
150+
if err == nil {
151+
log.Info("Repository %s/%s has been redirected to %s/%s", results.OwnerName, results.RepoName, redirectedRepo.OwnerName, redirectedRepo.Name)
152+
results.RepoName = redirectedRepo.Name
153+
results.OwnerName = redirectedRepo.OwnerName
154+
owner.ID = redirectedRepo.OwnerID
155+
} else {
156+
log.Warn("Repo %s/%s has a redirect to repo with ID %d, but no repo with this ID could be found. Trying without redirect...", results.OwnerName, results.RepoName, redirectedRepoID)
157+
}
158+
}
159+
134160
// Now get the Repository and set the results section
135161
repoExist := true
136162
repo, err := repo_model.GetRepositoryByName(ctx, owner.ID, results.RepoName)

0 commit comments

Comments
 (0)