aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/blame
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/blame
parent5882d60fb7ccd4cfc0fe69286aa96e198c9d1eb0 (diff)
parent4ec6b3f4fa9cdfe8f10d0953ac7d398d01a90f17 (diff)
downloadgo-git-e6f68d2e4cd1bc4447126816c7c27e1fc2098e30.tar.gz
Merge branch 'master' into jc/commit-ammend
Diffstat (limited to '_examples/blame')
-rw-r--r--_examples/blame/main.go48
1 files changed, 48 insertions, 0 deletions
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())
+}