Repository: go-gitea/gitea
File: routers/web/repo/githttp.go (Go)

Code before:
	switch ctx.Req.FormValue("service") {
	case "git-" + ServiceTypeUploadPack:
		return ServiceTypeUploadPack
	case "git-" + ServiceTypeReceivePack:
		return ServiceTypeReceivePack
	}
	return ""
}

func packetWrite(str string) []byte {
	s := strconv.FormatInt(int64(len(str)+4), 16)
	if len(s)%4 != 0 {
		s = strings.Repeat("0", 4-len(s)%4) + s
	}
	return []byte(s + str)
}

// GetInfoRefs implements Git dumb HTTP
func GetInfoRefs(ctx *context.Context) {
	h := httpBase(ctx)
	if h == nil {
		return
	}
	setHeaderNoCache(ctx)
	service := getServiceType(ctx)
	if service == "upload-pack" || service == "receive-pack" {
		if protocol := ctx.Req.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
			h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
		}

		refs, err := gitrepo.StatelessRPCAdvertiseRefs(ctx, h.getStorageRepo(), service, h.environ)
		if err != nil {
			log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
		}

		ctx.Resp.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
		ctx.Resp.WriteHeader(http.StatusOK)
		_, _ = ctx.Resp.Write(packetWrite("# service=git-" + service + "\n"))
		_, _ = ctx.Resp.Write([]byte("0000"))
		_, _ = ctx.Resp.Write(refs)
	} else {
		if err := gitrepo.UpdateServerInfo(ctx, h.getStorageRepo()); err != nil {
			log.Error("Failed to update server info: %v", err)
		}
		h.sendFile(ctx, "text/plain; charset=utf-8", "info/refs")
	}
}

// GetTextFile implements Git dumb HTTP
func GetTextFile(p string) func(*context.Context) {
	return func(ctx *context.Context) {

Review comment (suggestion):
This is not maintainable, please add & use `isVaildServiceType`

Code after:
	case "git-" + ServiceTypeUploadPack:
		return ServiceTypeUploadPack
	case "git-" + ServiceTypeReceivePack:
		return ServiceTypeReceivePack
	}
	return ""
}

func packetWrite(str string) []byte {
	s := strconv.FormatInt(int64(len(str)+4), 16)
	if len(s)%4 != 0 {
		s = strings.Repeat("0", 4-len(s)%4) + s
	}
	return []byte(s + str)
}

// GetInfoRefs implements Git dumb HTTP
func GetInfoRefs(ctx *context.Context) {
	h := httpBase(ctx)
	if h == nil {
		return
	}
	setHeaderNoCache(ctx)
	service := getServiceType(ctx)
	cmd, err := prepareGitCmdWithAllowedService(service)
	if err == nil {
		if protocol := ctx.Req.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
			h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
		}
		h.environ = append(os.Environ(), h.environ...)

		refs, _, err := gitrepo.RunCmdBytes(ctx, h.getStorageRepo(), cmd.AddArguments("--stateless-rpc", "--advertise-refs", ".").
			WithEnv(h.environ))
		if err != nil {
			log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
		}

		ctx.Resp.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
		ctx.Resp.WriteHeader(http.StatusOK)
		_, _ = ctx.Resp.Write(packetWrite("# service=git-" + service + "\n"))
		_, _ = ctx.Resp.Write([]byte("0000"))
		_, _ = ctx.Resp.Write(refs)
	} else {
		if err := gitrepo.UpdateServerInfo(ctx, h.getStorageRepo()); err != nil {
			log.Error("Failed to update server info: %v", err)
		}
		h.sendFile(ctx, "text/plain; charset=utf-8", "info/refs")
	}
}

// GetTextFile implements Git dumb HTTP