| package git |
| |
| import ( |
| "context" |
| "fmt" |
| "log" |
| ) |
| |
| // Example demonstrates how to use the Git interface |
| func Example() { |
| ctx := context.Background() |
| |
| // Create a new Git instance |
| git := DefaultGit("/path/to/your/repo") |
| |
| // Get repository status |
| status, err := git.Status(ctx) |
| if err != nil { |
| log.Fatalf("Failed to get status: %v", err) |
| } |
| |
| fmt.Printf("Current branch: %s\n", status.Branch) |
| fmt.Printf("Repository is clean: %t\n", status.IsClean) |
| |
| // List branches |
| branches, err := git.ListBranches(ctx) |
| if err != nil { |
| log.Fatalf("Failed to list branches: %v", err) |
| } |
| |
| fmt.Println("Branches:") |
| for _, branch := range branches { |
| current := "" |
| if branch.IsCurrent { |
| current = " (current)" |
| } |
| fmt.Printf(" %s%s\n", branch.Name, current) |
| } |
| |
| // Get recent commits |
| logOptions := LogOptions{ |
| MaxCount: 5, |
| Oneline: true, |
| } |
| |
| commits, err := git.Log(ctx, logOptions) |
| if err != nil { |
| log.Fatalf("Failed to get log: %v", err) |
| } |
| |
| fmt.Println("Recent commits:") |
| for _, commit := range commits { |
| fmt.Printf(" %s: %s\n", commit.Hash[:8], commit.Message) |
| } |
| } |
| |
| // ExampleWorkflow demonstrates a typical Git workflow |
| func ExampleWorkflow() { |
| ctx := context.Background() |
| |
| // Initialize a new repository |
| git := DefaultGit("/path/to/new/repo") |
| |
| // Initialize the repository |
| if err := git.Init(ctx, "/path/to/new/repo"); err != nil { |
| log.Fatalf("Failed to initialize repository: %v", err) |
| } |
| |
| // Set user configuration |
| userConfig := UserConfig{ |
| Name: "John Doe", |
| Email: "john@example.com", |
| } |
| |
| if err := git.SetUserConfig(ctx, userConfig); err != nil { |
| log.Fatalf("Failed to set user config: %v", err) |
| } |
| |
| // Create a new file and add it |
| // (In a real scenario, you would create the file here) |
| |
| // Stage all changes |
| if err := git.AddAll(ctx); err != nil { |
| log.Fatalf("Failed to add files: %v", err) |
| } |
| |
| // Commit the changes |
| commitOptions := CommitOptions{ |
| AllowEmpty: false, |
| } |
| |
| if err := git.Commit(ctx, "Initial commit", commitOptions); err != nil { |
| log.Fatalf("Failed to commit: %v", err) |
| } |
| |
| // Create a new branch |
| if err := git.CreateBranch(ctx, "feature/new-feature", ""); err != nil { |
| log.Fatalf("Failed to create branch: %v", err) |
| } |
| |
| // Switch to the new branch |
| if err := git.Checkout(ctx, "feature/new-feature"); err != nil { |
| log.Fatalf("Failed to checkout branch: %v", err) |
| } |
| |
| fmt.Println("Repository initialized and feature branch created!") |
| } |
| |
| // ExampleRemoteOperations demonstrates remote repository operations |
| func ExampleRemoteOperations() { |
| ctx := context.Background() |
| |
| git := DefaultGit("/path/to/your/repo") |
| |
| // Add a remote |
| if err := git.AddRemote(ctx, "origin", "https://github.com/user/repo.git"); err != nil { |
| log.Fatalf("Failed to add remote: %v", err) |
| } |
| |
| // List remotes |
| remotes, err := git.ListRemotes(ctx) |
| if err != nil { |
| log.Fatalf("Failed to list remotes: %v", err) |
| } |
| |
| fmt.Println("Remotes:") |
| for _, remote := range remotes { |
| fmt.Printf(" %s: %s\n", remote.Name, remote.URL) |
| } |
| |
| // Fetch from remote |
| fetchOptions := FetchOptions{ |
| All: true, |
| Tags: true, |
| } |
| |
| if err := git.Fetch(ctx, "", fetchOptions); err != nil { |
| log.Fatalf("Failed to fetch: %v", err) |
| } |
| |
| // Push to remote |
| pushOptions := PushOptions{ |
| SetUpstream: true, |
| } |
| |
| if err := git.Push(ctx, "origin", "main", pushOptions); err != nil { |
| log.Fatalf("Failed to push: %v", err) |
| } |
| } |