Skip to content

Commit

Permalink
fix: ensure default branch on http push
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jul 26, 2023
1 parent d42799e commit e4408d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
5 changes: 1 addition & 4 deletions server/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,12 @@ func (d *GitDaemon) handleClient(conn net.Conn) {
return
}

var handler git.ServiceHandler
var counter *prometheus.CounterVec
service := git.Service(split[0])
switch service {
case git.UploadPackService:
handler = git.UploadPack
counter = uploadPackGitCounter
case git.UploadArchiveService:
handler = git.UploadArchive
counter = uploadArchiveGitCounter
default:
d.fatal(c, git.ErrInvalidRequest)
Expand Down Expand Up @@ -289,7 +286,7 @@ func (d *GitDaemon) handleClient(conn net.Conn) {
Dir: filepath.Join(reposDir, repo),
}

if err := handler(ctx, cmd); err != nil {
if err := service.Handler(ctx, cmd); err != nil {

Check warning on line 289 in server/daemon/daemon.go

View check run for this annotation

Codecov / codecov/patch

server/daemon/daemon.go#L289

Added line #L289 was not covered by tests
d.logger.Debugf("git: error handling request: %v", err)
d.fatal(c, err)
return
Expand Down
2 changes: 1 addition & 1 deletion server/ssh/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func handleGit(s ssh.Session) {
createRepoCounter.WithLabelValues(name).Inc()
}

if err := git.ReceivePack(ctx, cmd); err != nil {
if err := service.Handler(ctx, cmd); err != nil {
sshFatal(s, git.ErrSystemMalfunction)
}

Expand Down
45 changes: 25 additions & 20 deletions server/web/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,10 @@ func withAccess(next http.Handler) http.HandlerFunc {
be := backend.FromContext(ctx)

// Store repository in context
// We're not checking for errors here because we want to allow
// repo creation on the fly.

Check warning on line 226 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L225-L226

Added lines #L225 - L226 were not covered by tests
repoName := pat.Param(r, "repo")
repo, err := be.Repository(ctx, repoName)
if err != nil {
if !errors.Is(err, proto.ErrRepoNotFound) {
logger.Error("failed to get repository", "err", err)
}
renderNotFound(w)
return
}

repo, _ := be.Repository(ctx, repoName)

Check warning on line 228 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L228

Added line #L228 was not covered by tests
ctx = proto.WithRepositoryContext(ctx, repo)
r = r.WithContext(ctx)

Expand Down Expand Up @@ -285,6 +279,16 @@ func withAccess(next http.Handler) http.HandlerFunc {
renderUnauthorized(w)
return
}

// Create the repo if it doesn't exist.
if repo == nil {
repo, err = be.CreateRepository(ctx, repoName, proto.RepositoryOptions{})
if err != nil {
logger.Error("failed to create repository", "repo", repoName, "err", err)
renderInternalServerError(w)
return
}

Check warning on line 290 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L284-L290

Added lines #L284 - L290 were not covered by tests
}
case gitLfsService:
switch {
case strings.HasPrefix(file, "info/lfs/locks"):
Expand Down Expand Up @@ -330,6 +334,12 @@ func withAccess(next http.Handler) http.HandlerFunc {
return

Check warning on line 334 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L333-L334

Added lines #L333 - L334 were not covered by tests
}

// If the repo doesn't exist, return 404
if repo == nil {
renderNotFound(w)
return
}

Check warning on line 341 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L338-L341

Added lines #L338 - L341 were not covered by tests

if accessLevel < access.ReadOnlyAccess {
askCredentials(w, r)
renderUnauthorized(w)
Expand All @@ -354,17 +364,6 @@ func serviceRpc(w http.ResponseWriter, r *http.Request) {

if service == git.ReceivePackService {
gitHttpReceiveCounter.WithLabelValues(repoName)

// Create the repo if it doesn't exist.
be := backend.FromContext(ctx)
repo := proto.RepositoryFromContext(ctx)
if repo == nil {
if _, err := be.CreateRepository(ctx, repoName, proto.RepositoryOptions{}); err != nil {
logger.Error("failed to create repository", "repo", repoName, "err", err)
renderInternalServerError(w)
return
}
}
}

w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-result", service))
Expand Down Expand Up @@ -452,6 +451,12 @@ func serviceRpc(w http.ResponseWriter, r *http.Request) {
}
flusher.Flush()
}

if service == git.ReceivePackService {
if err := git.EnsureDefaultBranch(ctx, cmd); err != nil {
logger.Errorf("failed to ensure default branch: %s", err)
}

Check warning on line 458 in server/web/git.go

View check run for this annotation

Codecov / codecov/patch

server/web/git.go#L455-L458

Added lines #L455 - L458 were not covered by tests
}
}

func getInfoRefs(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit e4408d3

Please sign in to comment.