Skip to content

[WIP] An http.Handler type thing that works well with go-git.

License

Notifications You must be signed in to change notification settings

crhntr/gitsmart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitSmart (very early WIP)

An http.Handler type thing that works well with go-git. The name is based on the name of the more robust/modern HTTP protocal used in the Git plumbing/tranfer stuff.

Notes

Design

I would like the API to be something like this.

package gitsmart

import (
	"net/http"
	
    "github.com/go-git/go-git/v5/plumbing"
    
    "github.com/crhntr/gitsmart"
)

type Database interface {
    Session() interface {
        StartTransaction()
        AbortTransaction()
        CommitTransaction()
    }   
}

func MyGitHandler(db Database) http.HandlerFunc {
	return func(res http.ResponseWriter, req *http.Request) {
        s := db.Session()
        	s.StartTransaction()
        
        	userAuth, err := loadUserAuthorizationFromWebToken(req)
        	if err != nil {
        		http.Error(res, "repository not found", http.StatusNotAuthorized)
        		return
        	}
        
        	// note any store that implements github.com/go-git/go-git/v5/plumbing/storer would be permitted.
        	store, err := loadGitStore(db, req.URL.Path)
        	if err != nil {
        		http.Error(res, "repository not found", http.StatusNotFound)
        		return
        	}
        
        	err = Handle(res, req, store,
        		// Handler functionality can be modified with functional options
        		// see: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
        		//
        		// For example, we can implement our own access control for individual branches...
        		// or maybe even more fine grained control over git objects
        		gitsmart.BeforeBranchUpdate(CheckIfUserCanUpdateBranch(&userAuth)),
        		//
        		// Another example, because we know our store can handle transactional updates, the
        		// Handler can be notified to broadcast that functionality to clients.
        		gitsmart.BroadcastTransactionalCapability,
        	)
        
        	if err != nil {
        		s.AbortTransaction()
        		return
        	}
        
        	s.CommitTransaction()
    }
}

func CheckIfUserCanUpdateBranch(user *UserAuth) gitsmart.BeforeBranchUpdateFunc {
	return func(ref *plumbing.Reference) error {
		return nil /* check if user allowed to edit reference otherwise return an error */
	}
}

About

[WIP] An http.Handler type thing that works well with go-git.

Topics

Resources

License

Stars

Watchers

Forks

Languages