aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-08-05 10:20:38 +0100
committerGitHub <noreply@github.com>2023-08-05 10:20:38 +0100
commite6f68d2e4cd1bc4447126816c7c27e1fc2098e30 (patch)
tree15c5e333b93641f9eadcb4bf4b34c338135f7a23 /_examples
parent5882d60fb7ccd4cfc0fe69286aa96e198c9d1eb0 (diff)
parent4ec6b3f4fa9cdfe8f10d0953ac7d398d01a90f17 (diff)
downloadgo-git-e6f68d2e4cd1bc4447126816c7c27e1fc2098e30.tar.gz
Merge branch 'master' into jc/commit-ammend
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md5
-rw-r--r--_examples/azure_devops/main.go56
-rw-r--r--_examples/blame/main.go48
-rw-r--r--_examples/clone/auth/ssh/main.go3
-rw-r--r--_examples/commit/main.go3
-rw-r--r--_examples/common_test.go3
-rw-r--r--_examples/ls-remote/main.go14
-rw-r--r--_examples/sha256/main.go65
-rw-r--r--_examples/tag-create-push/main.go3
9 files changed, 188 insertions, 12 deletions
diff --git a/_examples/README.md b/_examples/README.md
index 3a4c539..46f1fb0 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -23,10 +23,13 @@ Here you can find a list of annotated _go-git_ examples:
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc.
- [progress](progress/main.go) - Printing the progress information from the sideband.
- [revision](revision/main.go) - Solve a revision into a commit.
-- [config](config/main.go) - Explains how to work with config files.
- [submodule](submodule/main.go) - Submodule update remote.
+- [azure devops](azure_devops/main.go) - Cloning Azure DevOps repositories.
+- [blame](blame/main.go) - Blame/annotate a commit.
+- [ls-remote](ls-remote/main.go) - List remote tags without cloning a repository.
### Advanced
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one.
- [clone with context](context/main.go) - Cloning a repository with graceful cancellation.
- [storage](storage/README.md) - Implementing a custom storage system.
+- [sha256](sha256/main.go) - Init and commiting repositories that use sha256 as object format.
diff --git a/_examples/azure_devops/main.go b/_examples/azure_devops/main.go
new file mode 100644
index 0000000..9c02ca0
--- /dev/null
+++ b/_examples/azure_devops/main.go
@@ -0,0 +1,56 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ git "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
+ "github.com/go-git/go-git/v5/plumbing/transport"
+ "github.com/go-git/go-git/v5/plumbing/transport/http"
+)
+
+func main() {
+ CheckArgs("<url>", "<directory>", "<azuredevops_username>", "<azuredevops_password>")
+ url, directory, username, password := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
+
+ // Clone the given repository to the given directory
+ Info("git clone %s %s", url, directory)
+
+ // Azure DevOps requires capabilities multi_ack / multi_ack_detailed,
+ // which are not fully implemented and by default are included in
+ // transport.UnsupportedCapabilities.
+ //
+ // The initial clone operations require a full download of the repository,
+ // and therefore those unsupported capabilities are not as crucial, so
+ // by removing them from that list allows for the first clone to work
+ // successfully.
+ //
+ // Additional fetches will yield issues, therefore work always from a clean
+ // clone until those capabilities are fully supported.
+ //
+ // New commits and pushes against a remote worked without any issues.
+ transport.UnsupportedCapabilities = []capability.Capability{
+ capability.ThinPack,
+ }
+
+ r, err := git.PlainClone(directory, false, &git.CloneOptions{
+ Auth: &http.BasicAuth{
+ Username: username,
+ Password: password,
+ },
+ URL: url,
+ Progress: os.Stdout,
+ })
+ CheckIfError(err)
+
+ // ... retrieving the branch being pointed by HEAD
+ ref, err := r.Head()
+ CheckIfError(err)
+ // ... retrieving the commit object
+ commit, err := r.CommitObject(ref.Hash())
+ CheckIfError(err)
+
+ fmt.Println(commit)
+}
diff --git a/_examples/blame/main.go b/_examples/blame/main.go
new file mode 100644
index 0000000..3ffae17
--- /dev/null
+++ b/_examples/blame/main.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+)
+
+// Basic example of how to blame a repository.
+func main() {
+ CheckArgs("<url>", "<file_to_blame>")
+ url := os.Args[1]
+ path := os.Args[2]
+
+ tmp, err := os.MkdirTemp("", "go-git-blame-*")
+ CheckIfError(err)
+
+ defer os.RemoveAll(tmp)
+
+ // Clone the given repository.
+ Info("git clone %s %s", url, tmp)
+ r, err := git.PlainClone(
+ tmp,
+ false,
+ &git.CloneOptions{
+ URL: url,
+ Tags: git.NoTags,
+ },
+ )
+ CheckIfError(err)
+
+ // Retrieve the branch's HEAD, to then get the HEAD commit.
+ ref, err := r.Head()
+ CheckIfError(err)
+
+ c, err := r.CommitObject(ref.Hash())
+ CheckIfError(err)
+
+ Info("git blame %s", path)
+
+ // Blame the given file/path.
+ br, err := git.Blame(c, path)
+ CheckIfError(err)
+
+ fmt.Printf("%s", br.String())
+}
diff --git a/_examples/clone/auth/ssh/main.go b/_examples/clone/auth/ssh/main.go
index 1e06e44..5f21d90 100644
--- a/_examples/clone/auth/ssh/main.go
+++ b/_examples/clone/auth/ssh/main.go
@@ -32,9 +32,6 @@ func main() {
}
r, err := git.PlainClone(directory, false, &git.CloneOptions{
- // The intended use of a GitHub personal access token is in replace of your password
- // because access tokens can easily be revoked.
- // https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
Auth: publicKeys,
URL: url,
Progress: os.Stdout,
diff --git a/_examples/commit/main.go b/_examples/commit/main.go
index 4529c84..3f3c880 100644
--- a/_examples/commit/main.go
+++ b/_examples/commit/main.go
@@ -2,7 +2,6 @@ package main
import (
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"time"
@@ -29,7 +28,7 @@ func main() {
// worktree of the project using the go standard library.
Info("echo \"hello world!\" > example-git-file")
filename := filepath.Join(directory, "example-git-file")
- err = ioutil.WriteFile(filename, []byte("hello world!"), 0644)
+ err = os.WriteFile(filename, []byte("hello world!"), 0644)
CheckIfError(err)
// Adds the new file to the staging area.
diff --git a/_examples/common_test.go b/_examples/common_test.go
index 9945c87..6630f15 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -3,7 +3,6 @@ package examples
import (
"flag"
"go/build"
- "io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -65,7 +64,7 @@ func TestExamples(t *testing.T) {
}
func tempFolder() string {
- path, err := ioutil.TempDir("", "")
+ path, err := os.MkdirTemp("", "")
CheckIfError(err)
tempFolders = append(tempFolders, path)
diff --git a/_examples/ls-remote/main.go b/_examples/ls-remote/main.go
index af038d6..e49e8c9 100644
--- a/_examples/ls-remote/main.go
+++ b/_examples/ls-remote/main.go
@@ -2,25 +2,35 @@ package main
import (
"log"
+ "os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "github.com/go-git/go-git/v5/_examples"
)
// Retrieve remote tags without cloning repository
func main() {
+ CheckArgs("<url>")
+ url := os.Args[1]
+
+ Info("git ls-remote --tags %s", url)
// Create the remote with repository URL
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
- URLs: []string{"https://github.com/Zenika/MARCEL"},
+ URLs: []string{url},
})
log.Print("Fetching tags...")
// We can then use every Remote functions to retrieve wanted information
- refs, err := rem.List(&git.ListOptions{})
+ refs, err := rem.List(&git.ListOptions{
+ // Returns all references, including peeled references.
+ PeelingOption: git.AppendPeeled,
+ })
if err != nil {
log.Fatal(err)
}
diff --git a/_examples/sha256/main.go b/_examples/sha256/main.go
new file mode 100644
index 0000000..e1772d2
--- /dev/null
+++ b/_examples/sha256/main.go
@@ -0,0 +1,65 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/plumbing/format/config"
+ "github.com/go-git/go-git/v5/plumbing/object"
+)
+
+// This example requires building with the sha256 tag for it to work:
+// go run -tags sha256 main.go /tmp/repository
+
+// Basic example of how to initialise a repository using sha256 as the hashing algorithmn.
+func main() {
+ CheckArgs("<directory>")
+ directory := os.Args[1]
+
+ os.RemoveAll(directory)
+
+ // Init a new repository using the ObjectFormat SHA256.
+ r, err := git.PlainInitWithOptions(directory, &git.PlainInitOptions{ObjectFormat: config.SHA256})
+ CheckIfError(err)
+
+ w, err := r.Worktree()
+ CheckIfError(err)
+
+ // ... we need a file to commit so let's create a new file inside of the
+ // worktree of the project using the go standard library.
+ Info("echo \"hello world!\" > example-git-file")
+ filename := filepath.Join(directory, "example-git-file")
+ err = os.WriteFile(filename, []byte("hello world!"), 0644)
+ CheckIfError(err)
+
+ // Adds the new file to the staging area.
+ Info("git add example-git-file")
+ _, err = w.Add("example-git-file")
+ CheckIfError(err)
+
+ // Commits the current staging area to the repository, with the new file
+ // just created. We should provide the object.Signature of Author of the
+ // commit Since version 5.0.1, we can omit the Author signature, being read
+ // from the git config files.
+ Info("git commit -m \"example go-git commit\"")
+ commit, err := w.Commit("example go-git commit", &git.CommitOptions{
+ Author: &object.Signature{
+ Name: "John Doe",
+ Email: "john@doe.org",
+ When: time.Now(),
+ },
+ })
+
+ CheckIfError(err)
+
+ // Prints the current HEAD to verify that all worked well.
+ Info("git show -s")
+ obj, err := r.CommitObject(commit)
+ CheckIfError(err)
+
+ fmt.Println(obj)
+}
diff --git a/_examples/tag-create-push/main.go b/_examples/tag-create-push/main.go
index c443641..b820c76 100644
--- a/_examples/tag-create-push/main.go
+++ b/_examples/tag-create-push/main.go
@@ -2,7 +2,6 @@ package main
import (
"fmt"
- "io/ioutil"
"log"
"os"
@@ -67,7 +66,7 @@ func cloneRepo(url, dir, publicKeyPath string) (*git.Repository, error) {
func publicKey(filePath string) (*ssh.PublicKeys, error) {
var publicKey *ssh.PublicKeys
- sshKey, _ := ioutil.ReadFile(filePath)
+ sshKey, _ := os.ReadFile(filePath)
publicKey, err := ssh.NewPublicKeys("git", []byte(sshKey), "")
if err != nil {
return nil, err