(ctx context.Context, s ssh.Session, cmd []string, access AccessLevel)
| 31 | } |
| 32 | |
| 33 | func (serv *Server) cmdRepoAction(ctx context.Context, s ssh.Session, cmd []string, access AccessLevel) int { |
| 34 | if len(cmd) != 2 { |
| 35 | _ = writeStringFmt(s.Stderr(), "Missing repo name argument\r\n") |
| 36 | return 1 |
| 37 | } |
| 38 | |
| 39 | log, config, user := CtxExtract(ctx) |
| 40 | pk := CtxPublicKey(ctx) |
| 41 | |
| 42 | // Sanitize the repo name |
| 43 | // - Trim all slashes from beginning and end |
| 44 | // - Add a root slash (so path.Clean works correctly) |
| 45 | // - path.Clean |
| 46 | // - Remove the initial slash |
| 47 | // - Sanitize the name |
| 48 | repoName := sanitize(path.Clean("/" + strings.Trim(cmd[1], "/"))[1:]) |
| 49 | |
| 50 | // Repo does not exist and permission checks should give the same error, so |
| 51 | // information about what repos are defined is not leaked. |
| 52 | repo, err := config.LookupRepoAccess(user, repoName) |
| 53 | if err != nil { |
| 54 | _ = writeStringFmt(s.Stderr(), "Repo does not exist\r\n") |
| 55 | return -1 |
| 56 | } |
| 57 | |
| 58 | if repo.Access < access { |
| 59 | _ = writeStringFmt(s.Stderr(), "Repo does not exist\r\n") |
| 60 | return -1 |
| 61 | } |
| 62 | |
| 63 | // Because we check ImplicitRepos earlier, if they have admin access, it's |
| 64 | // safe to ensure this repo exists. |
| 65 | if repo.Access >= AccessLevelAdmin { |
| 66 | _, err = git.EnsureRepo(serv.config.fs, repo.Path()) |
| 67 | if err != nil { |
| 68 | return -1 |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | returnCode := runCommand(log, serv.fs.Root(), s, []string{cmd[0], repo.Path()}, []string{ |
| 73 | "GITDIR_BASE_DIR=" + serv.fs.Root(), |
| 74 | "GITDIR_HOOK_REPO_PATH=" + repoName, |
| 75 | "GITDIR_HOOK_PUBLIC_KEY=" + pk.String(), |
| 76 | "GITDIR_LOG_FORMAT=console", |
| 77 | }) |
| 78 | |
| 79 | // Reload the server config if a config repo was changed. |
| 80 | if access == AccessLevelWrite { |
| 81 | switch repo.Type { |
| 82 | case RepoTypeAdmin, RepoTypeOrgConfig, RepoTypeUserConfig: |
| 83 | err = serv.Reload() |
| 84 | if err != nil { |
| 85 | _ = writeStringFmt(s.Stderr(), "Error when reloading config: %s\r\n", err) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return returnCode |
no test coverage detected