repository.go

41 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package models

import "congo.gg/pkg/database"

// Repository represents a git repository on disk.
type Repository struct {
	database.Model
	Name         string
	URL          string
	Path         string // On-disk path
	Branch       string
	Status       string // cloned/discovered/missing/error
	StatusDetail string // clean/modified/ahead
}

// ShortURL returns a shortened display version of the git URL.
func (r *Repository) ShortURL() string {
	u := r.URL
	if u == "" {
		return ""
	}
	// Remove protocol prefix
	for _, prefix := range []string{"https://", "http://", "git@"} {
		if len(u) > len(prefix) && u[:len(prefix)] == prefix {
			u = u[len(prefix):]
			break
		}
	}
	// Remove trailing .git
	if len(u) > 4 && u[len(u)-4:] == ".git" {
		u = u[:len(u)-4]
	}
	// Convert SSH colon to slash
	for i, c := range u {
		if c == ':' && i > 0 {
			u = u[:i] + "/" + u[i+1:]
			break
		}
	}
	return u
}