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