Clone a repository by its provided URL into a newly created directory and check out an initial branch forked from the cloned repository’s default branch.
packagemainimport("log"git"github.com/purpleclay/gitz")funcmain(){client,_:=git.NewClient()_,err:=client.Clone("https://github.com/purpleclay/gitz")iferr!=nil{log.Fatal("failed to clone repository")}}
If a complete git history isn't desirable, a faster approach to cloning is to provide a clone depth using the WithDepth option. This results in a truncated history, better known as a shallow clone. Great for large established repositories.
packagemainimport("fmt""log"git"github.com/purpleclay/gitz")funcmain(){client,_:=git.NewClient()// A repository exists with the following commits:// > feat: add support for shallow cloning// > initialized repository_,err:=client.Clone("https://github.com/purpleclay/gitz",git.WithDepth(1))iferr!=nil{log.Fatal("failed to clone repository")}repoLog,err:=client.Log()iferr!=nil{log.Fatal("failed to retrieve repository log")}for_,commit:=rangerepoLog.Commits{fmt.Println(commit.Message)}}
Printing the log results in:
feat: add options that can configure the size of the repository during a clone
packagemainimport("fmt""log"git"github.com/purpleclay/gitz")funcmain(){client,_:=git.NewClient()_,err:=client.Clone("https://github.com/purpleclay/gitz",git.WithNoTags())iferr!=nil{log.Fatal("failed to clone repository")}tags,err:=client.Tags()iferr!=nil{log.Fatal("failed to retrieve repository tags")}iflen(tags)==0{fmt.Println("repository has no tags")}}
By default, git will always clone the repository into a directory based on the human-friendly part of the clone URL. To clone into a different directory, use the WithDirectory option.
packagemainimport("log"git"github.com/purpleclay/gitz")funcmain(){client,_:=git.NewClient()_,err:=client.Clone("https://github.com/purpleclay/gitz",git.WithDirectory("my-gitz"))iferr!=nil{log.Fatal("failed to clone repository")}}
Git will always clone and checkout the default branch of a repository. To change this behavior, provide a branch or tag reference with the WithCheckoutRef option. The latter results in a detached HEAD where the HEAD of a repository points to a specific commit reference.
packagemainimport("log"git"github.com/purpleclay/gitz")funcmain(){client,_:=git.NewClient()_,err:=client.Clone("https://github.com/purpleclay/gitz",git.WithCheckoutRef("0.1.0"))iferr!=nil{log.Fatal("failed to clone repository")}}
You can provide git config through the WithCloneConfig option to only take effect during the execution of a Clone, removing the need to change config permanently.