blob: 1bea27cda6978cbf000055dbf4836a068ef6f424 [file] [log] [blame]
iomodo5a7e4e72025-07-25 13:21:41 +04001package git
2
3import (
4 "context"
iomodo0c203b12025-07-26 19:44:57 +04005 "log/slog"
6 "os"
iomodo5a7e4e72025-07-25 13:21:41 +04007)
8
9// Example demonstrates how to use the Git interface
10func Example() {
11 ctx := context.Background()
12
iomodo0c203b12025-07-26 19:44:57 +040013 // Create logger
14 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
15
iomodo5a7e4e72025-07-25 13:21:41 +040016 // 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 {
iomodo0c203b12025-07-26 19:44:57 +040022 logger.Error("Failed to get status", slog.String("error", err.Error()))
23 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040024 }
25
iomodo0c203b12025-07-26 19:44:57 +040026 logger.Info("Repository status", slog.String("branch", status.Branch), slog.Bool("clean", status.IsClean))
iomodo5a7e4e72025-07-25 13:21:41 +040027
28 // List branches
29 branches, err := git.ListBranches(ctx)
30 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040031 logger.Error("Failed to list branches", slog.String("error", err.Error()))
32 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040033 }
34
iomodo0c203b12025-07-26 19:44:57 +040035 logger.Info("Branches found", slog.Int("count", len(branches)))
iomodo5a7e4e72025-07-25 13:21:41 +040036 for _, branch := range branches {
37 current := ""
38 if branch.IsCurrent {
39 current = " (current)"
40 }
iomodo0c203b12025-07-26 19:44:57 +040041 logger.Info("Branch", slog.String("name", branch.Name+current))
iomodo5a7e4e72025-07-25 13:21:41 +040042 }
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 {
iomodo0c203b12025-07-26 19:44:57 +040052 logger.Error("Failed to get log", slog.String("error", err.Error()))
53 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040054 }
55
iomodo0c203b12025-07-26 19:44:57 +040056 logger.Info("Recent commits", slog.Int("count", len(commits)))
iomodo5a7e4e72025-07-25 13:21:41 +040057 for _, commit := range commits {
iomodo0c203b12025-07-26 19:44:57 +040058 logger.Info("Commit", slog.String("hash", commit.Hash[:8]), slog.String("message", commit.Message))
iomodo5a7e4e72025-07-25 13:21:41 +040059 }
60}
61
62// ExampleWorkflow demonstrates a typical Git workflow
63func ExampleWorkflow() {
64 ctx := context.Background()
65
iomodo0c203b12025-07-26 19:44:57 +040066 // Create logger
67 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
68
iomodo5a7e4e72025-07-25 13:21:41 +040069 // 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 {
iomodo0c203b12025-07-26 19:44:57 +040074 logger.Error("Failed to initialize repository", slog.String("error", err.Error()))
75 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040076 }
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 {
iomodo0c203b12025-07-26 19:44:57 +040085 logger.Error("Failed to set user config", slog.String("error", err.Error()))
86 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040087 }
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 {
iomodo0c203b12025-07-26 19:44:57 +040094 logger.Error("Failed to add files", slog.String("error", err.Error()))
95 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +040096 }
97
98 // Commit the changes
99 commitOptions := CommitOptions{
100 AllowEmpty: false,
101 }
102
103 if err := git.Commit(ctx, "Initial commit", commitOptions); err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400104 logger.Error("Failed to commit", slog.String("error", err.Error()))
105 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400106 }
107
108 // Create a new branch
109 if err := git.CreateBranch(ctx, "feature/new-feature", ""); err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400110 logger.Error("Failed to create branch", slog.String("error", err.Error()))
111 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400112 }
113
114 // Switch to the new branch
115 if err := git.Checkout(ctx, "feature/new-feature"); err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400116 logger.Error("Failed to checkout branch", slog.String("error", err.Error()))
117 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400118 }
119
iomodo0c203b12025-07-26 19:44:57 +0400120 logger.Info("Repository initialized and feature branch created!")
iomodo5a7e4e72025-07-25 13:21:41 +0400121}
122
123// ExampleRemoteOperations demonstrates remote repository operations
124func ExampleRemoteOperations() {
125 ctx := context.Background()
126
iomodo0c203b12025-07-26 19:44:57 +0400127 // Create logger
128 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
129
iomodo5a7e4e72025-07-25 13:21:41 +0400130 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 {
iomodo0c203b12025-07-26 19:44:57 +0400134 logger.Error("Failed to add remote", slog.String("error", err.Error()))
135 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400136 }
137
138 // List remotes
139 remotes, err := git.ListRemotes(ctx)
140 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400141 logger.Error("Failed to list remotes", slog.String("error", err.Error()))
142 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400143 }
144
iomodo0c203b12025-07-26 19:44:57 +0400145 logger.Info("Remotes found", slog.Int("count", len(remotes)))
iomodo5a7e4e72025-07-25 13:21:41 +0400146 for _, remote := range remotes {
iomodo0c203b12025-07-26 19:44:57 +0400147 logger.Info("Remote", slog.String("name", remote.Name), slog.String("url", remote.URL))
iomodo5a7e4e72025-07-25 13:21:41 +0400148 }
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 {
iomodo0c203b12025-07-26 19:44:57 +0400157 logger.Error("Failed to fetch", slog.String("error", err.Error()))
158 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400159 }
160
161 // Push to remote
162 pushOptions := PushOptions{
163 SetUpstream: true,
164 }
165
166 if err := git.Push(ctx, "origin", "main", pushOptions); err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400167 logger.Error("Failed to push", slog.String("error", err.Error()))
168 os.Exit(1)
iomodo5a7e4e72025-07-25 13:21:41 +0400169 }
170}