Lenient VCS repository URL parsing library for Go

Related tags

go-vcsurl
Overview

============================================= go-vcsurl - Lenient VCS repository URL parser

status

go-vcsurl parses VCS repository URLs in many common formats.

Note: the public API is experimental and subject to change until further notice.

Usage

Documentation: go-vcsurl on Sourcegraph.

Example: example_test.go (Sourcegraph):

package vcsurl_test

import (
	"fmt"
	"gopkg.in/sourcegraph/go-vcsurl.v1"
)

func ExampleParse() {
	urls := []string{
		"github.com/alice/libfoo",
		"git://github.com/bob/libbar",
		"code.google.com/p/libqux",
		"https://code.google.com/p/libbaz",
	}
	for i, url := range urls {
		if info, err := vcsurl.Parse(url); err == nil {
			fmt.Printf("%d. %s %s\n", i+1, info.VCS, info.CloneURL)
			fmt.Printf("   name: %s\n", info.Name)
			fmt.Printf("   host: %s\n", info.RepoHost)
		} else {
			fmt.Printf("error parsing %s\n")
		}
	}

	// output:
	// 1. git git://github.com/alice/libfoo.git
	//    name: libfoo
	//    host: github.com
	// 2. git git://github.com/bob/libbar.git
	//    name: libbar
	//    host: github.com
	// 3. hg https://code.google.com/p/libqux
	//    name: libqux
	//    host: code.google.com
	// 4. hg https://code.google.com/p/libbaz
	//    name: libbaz
	//    host: code.google.com
}

Running tests

Run go test.

Contributors

Issues
  • Parsing fails on private Gogs repos

    Parsing fails on private Gogs repos

    For example:

    package main
    
    import (
    	"fmt"
    	"gopkg.in/sourcegraph/go-vcsurl.v1"
    )
    
    func main() {
    	urls := []string{
    		"github.com/alice/libfoo",
    		"git://github.com/bob/libbar",
    		"code.google.com/p/libqux",
    		"https://code.google.com/p/libbaz",
    "[email protected]:org/repo.git",
    	}
    	for i, url := range urls {
    		if info, err := vcsurl.Parse(url); err == nil {
    			fmt.Printf("%d. %s %s\n", i+1, info.VCS, info.CloneURL)
    			fmt.Printf("   name: %s\n", info.Name)
    			fmt.Printf("   fullname: %s\n", info.FullName)
    			fmt.Printf("   host: %s\n", info.RepoHost)
    		} else {
    			fmt.Printf("error parsing %s\n")
    		}
    	}
    }
    

    The last URL results in:

    5. git https://[email protected]:org/repo.git
       name: repo
       fullname: repo
       host: git.private.com:org
    

    Solution

    Preprocessing the url with the following code solves the problem:

    gitPreprocessRE := regexp.MustCompile("^[email protected]([a-zA-Z0-9-_\\.]+)\\:(.*)$")
    if parts := gitPreprocessRE.FindStringSubmatch(url); len(parts) == 3 {
    	url = fmt.Sprintf("git://%s/%s", parts[1], parts[2])
    }
    
    opened by ewoutp 2
  • gopkg.in

    gopkg.in

    We are using this library in a internal project, maybe could be a good idea use gopkg.in. Thanks

    /cc @sqs

    opened by mcuadros 1
  • gopkg.in routes

    gopkg.in routes

    opened by mcuadros 1
  • Gogs url fix

    Gogs url fix

    Fixes #3

    opened by ewoutp 1
Owner
Sourcegraph
Code search and navigation for teams (self-hosted, OSS)
Sourcegraph
Semantic Versioning (semver) library written in golang

semver for golang semver is a Semantic Versioning library written in golang. It fully covers spec version 2.0.0. Versioning Old v1-v3 versions exist i

Benedikt Lang 833 Sep 1, 2021
Installs git repos onto your system and keeps them up-to-date

Gitfile Installs git repos onto your system and keeps them up-to-date. It's a lightweight package manager for things that haven't been published to a

Brad Urani 18 Jan 16, 2021
Git folder digger, I'm sure it's worthwhile stuff.

Gigger Git folder digger, I'm sure it's worthwhile stuff. Installation Download a prebuilt binary from releases page. or If you have recent go compile

Rıza Sabuncu 38 Aug 28, 2021
Nut is a tool to manage versioned Go source code packages, called "nuts".

nut Nut is a tool to manage versioned Go source code packages, called "nuts". gonuts.io – central repository (source code) Documents Stable API Mailin

Alexey Palazhchenko 50 Jul 15, 2021