| iomodo | 5a7e4e7 | 2025-07-25 13:21:41 +0400 | [diff] [blame^] | 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | ) |
| 8 | |
| 9 | // Example demonstrates how to use the Git interface |
| 10 | func Example() { |
| 11 | ctx := context.Background() |
| 12 | |
| 13 | // Create a new Git instance |
| 14 | git := DefaultGit("/path/to/your/repo") |
| 15 | |
| 16 | // Get repository status |
| 17 | status, err := git.Status(ctx) |
| 18 | if err != nil { |
| 19 | log.Fatalf("Failed to get status: %v", err) |
| 20 | } |
| 21 | |
| 22 | fmt.Printf("Current branch: %s\n", status.Branch) |
| 23 | fmt.Printf("Repository is clean: %t\n", status.IsClean) |
| 24 | |
| 25 | // List branches |
| 26 | branches, err := git.ListBranches(ctx) |
| 27 | if err != nil { |
| 28 | log.Fatalf("Failed to list branches: %v", err) |
| 29 | } |
| 30 | |
| 31 | fmt.Println("Branches:") |
| 32 | for _, branch := range branches { |
| 33 | current := "" |
| 34 | if branch.IsCurrent { |
| 35 | current = " (current)" |
| 36 | } |
| 37 | fmt.Printf(" %s%s\n", branch.Name, current) |
| 38 | } |
| 39 | |
| 40 | // Get recent commits |
| 41 | logOptions := LogOptions{ |
| 42 | MaxCount: 5, |
| 43 | Oneline: true, |
| 44 | } |
| 45 | |
| 46 | commits, err := git.Log(ctx, logOptions) |
| 47 | if err != nil { |
| 48 | log.Fatalf("Failed to get log: %v", err) |
| 49 | } |
| 50 | |
| 51 | fmt.Println("Recent commits:") |
| 52 | for _, commit := range commits { |
| 53 | fmt.Printf(" %s: %s\n", commit.Hash[:8], commit.Message) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // ExampleWorkflow demonstrates a typical Git workflow |
| 58 | func ExampleWorkflow() { |
| 59 | ctx := context.Background() |
| 60 | |
| 61 | // Initialize a new repository |
| 62 | git := DefaultGit("/path/to/new/repo") |
| 63 | |
| 64 | // Initialize the repository |
| 65 | if err := git.Init(ctx, "/path/to/new/repo"); err != nil { |
| 66 | log.Fatalf("Failed to initialize repository: %v", err) |
| 67 | } |
| 68 | |
| 69 | // Set user configuration |
| 70 | userConfig := UserConfig{ |
| 71 | Name: "John Doe", |
| 72 | Email: "john@example.com", |
| 73 | } |
| 74 | |
| 75 | if err := git.SetUserConfig(ctx, userConfig); err != nil { |
| 76 | log.Fatalf("Failed to set user config: %v", err) |
| 77 | } |
| 78 | |
| 79 | // Create a new file and add it |
| 80 | // (In a real scenario, you would create the file here) |
| 81 | |
| 82 | // Stage all changes |
| 83 | if err := git.AddAll(ctx); err != nil { |
| 84 | log.Fatalf("Failed to add files: %v", err) |
| 85 | } |
| 86 | |
| 87 | // Commit the changes |
| 88 | commitOptions := CommitOptions{ |
| 89 | AllowEmpty: false, |
| 90 | } |
| 91 | |
| 92 | if err := git.Commit(ctx, "Initial commit", commitOptions); err != nil { |
| 93 | log.Fatalf("Failed to commit: %v", err) |
| 94 | } |
| 95 | |
| 96 | // Create a new branch |
| 97 | if err := git.CreateBranch(ctx, "feature/new-feature", ""); err != nil { |
| 98 | log.Fatalf("Failed to create branch: %v", err) |
| 99 | } |
| 100 | |
| 101 | // Switch to the new branch |
| 102 | if err := git.Checkout(ctx, "feature/new-feature"); err != nil { |
| 103 | log.Fatalf("Failed to checkout branch: %v", err) |
| 104 | } |
| 105 | |
| 106 | fmt.Println("Repository initialized and feature branch created!") |
| 107 | } |
| 108 | |
| 109 | // ExampleRemoteOperations demonstrates remote repository operations |
| 110 | func ExampleRemoteOperations() { |
| 111 | ctx := context.Background() |
| 112 | |
| 113 | git := DefaultGit("/path/to/your/repo") |
| 114 | |
| 115 | // Add a remote |
| 116 | if err := git.AddRemote(ctx, "origin", "https://github.com/user/repo.git"); err != nil { |
| 117 | log.Fatalf("Failed to add remote: %v", err) |
| 118 | } |
| 119 | |
| 120 | // List remotes |
| 121 | remotes, err := git.ListRemotes(ctx) |
| 122 | if err != nil { |
| 123 | log.Fatalf("Failed to list remotes: %v", err) |
| 124 | } |
| 125 | |
| 126 | fmt.Println("Remotes:") |
| 127 | for _, remote := range remotes { |
| 128 | fmt.Printf(" %s: %s\n", remote.Name, remote.URL) |
| 129 | } |
| 130 | |
| 131 | // Fetch from remote |
| 132 | fetchOptions := FetchOptions{ |
| 133 | All: true, |
| 134 | Tags: true, |
| 135 | } |
| 136 | |
| 137 | if err := git.Fetch(ctx, "", fetchOptions); err != nil { |
| 138 | log.Fatalf("Failed to fetch: %v", err) |
| 139 | } |
| 140 | |
| 141 | // Push to remote |
| 142 | pushOptions := PushOptions{ |
| 143 | SetUpstream: true, |
| 144 | } |
| 145 | |
| 146 | if err := git.Push(ctx, "origin", "main", pushOptions); err != nil { |
| 147 | log.Fatalf("Failed to push: %v", err) |
| 148 | } |
| 149 | } |