Skip to content

Commit b17ff66

Browse files
yyle88yangyile1990
authored andcommitted
Update: standardize API names and enhance test code
- Remove redundant "Git" from function names (GetLatestGitTag -> GetLatestTag) - Add Get prefix as naming standard (GetLatestTagHasPrefix, GetSortedTags) - Rename HasStagingChanges to HasStagedChanges - Rename GitCommitHash to GetCommitHash - Rename GetFileList to GetTrackedFiles - Rename GetBranchTrackingBranch to GetUpstreamBranch - Rename PushSetUpstreamOriginBranch to PushWithUpstream - Rename GetPorcelainStatus to GetStatusPorcelain - Rename RemoteSet to RemoteSetURL - Rename LatestGitTagMatchRegexp to GetLatestTagMatchGlob - Use remoteLink as argument name in RemoteAdd - Rearrange Tags/GetTags/TagList with Tags as main function - Delete LatestGitTag function (use GetLatestTag instead) - Replace defer with t.Cleanup() in tests - Bump github.com/yyle88/runpath from v1.0.24 to v1.0.25
1 parent 5c96a2b commit b17ff66

File tree

13 files changed

+195
-251
lines changed

13 files changed

+195
-251
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ func main() {
156156
gcm.Add().Commit("v2").Tag("v1.1.0").Done()
157157
zaplog.SUG.Info("tagged v1.1.0")
158158

159-
latest := rese.V1(gcm.LatestGitTag())
159+
latest, exists, err := gcm.GetLatestTag()
160+
must.Done(err)
161+
must.True(exists)
160162
zaplog.SUG.Info("latest tag:", latest)
161163

162164
count := rese.V1(gcm.GetCommitCount())
@@ -190,17 +192,16 @@ func main() {
190192

191193
### Repo State
192194

193-
- `HasStagingChanges() (bool, error)` - Check staged changes existence
195+
- `HasStagedChanges() (bool, error)` - Check staged changes existence
194196
- `HasUnstagedChanges() (bool, error)` - Check unstaged changes existence
195197
- `HasChanges() (bool, error)` - Check changes existence
196198
- `GetCommitCount() (int, error)` - Get commit count
197-
- `GitCommitHash(ref) (string, error)` - Get commit hash with reference
199+
- `GetCommitHash(ref) (string, error)` - Get commit hash with reference
198200
- `GetRemoteURL(remote) (string, error)` - Get remote repo URL
199201
- `GetIgnoredFiles() ([]string, error)` - Get files ignored in gitignore
200202

201203
### Tag Operations
202204

203-
- `LatestGitTag() (string, error)` - Get latest tag name (fails when no tags exist)
204205
- `GetLatestTag() (string, bool, error)` - Get latest tag name with existence check
205206

206207
### Issue Handling

README.zh.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ func main() {
156156
gcm.Add().Commit("v2").Tag("v1.1.0").Done()
157157
zaplog.SUG.Info("tagged v1.1.0")
158158

159-
latest := rese.V1(gcm.LatestGitTag())
159+
latest, exists, err := gcm.GetLatestTag()
160+
must.Done(err)
161+
must.True(exists)
160162
zaplog.SUG.Info("latest tag:", latest)
161163

162164
count := rese.V1(gcm.GetCommitCount())
@@ -190,17 +192,16 @@ func main() {
190192

191193
### 仓库状态
192194

193-
- `HasStagingChanges() (bool, error)` - 检查暂存更改是否存在
195+
- `HasStagedChanges() (bool, error)` - 检查暂存更改是否存在
194196
- `HasUnstagedChanges() (bool, error)` - 检查未暂存更改是否存在
195197
- `HasChanges() (bool, error)` - 检查更改是否存在
196198
- `GetCommitCount() (int, error)` - 获取提交数量
197-
- `GitCommitHash(ref) (string, error)` - 使用引用获取提交哈希
199+
- `GetCommitHash(ref) (string, error)` - 使用引用获取提交哈希
198200
- `GetRemoteURL(remote) (string, error)` - 获取远程仓库 URL
199201
- `GetIgnoredFiles() ([]string, error)` - 获取 gitignore 忽略的文件
200202

201203
### 标签操作
202204

203-
- `LatestGitTag() (string, error)` - 获取最新标签名称(没有标签时会失败)
204205
- `GetLatestTag() (string, bool, error)` - 获取最新标签名称并检查是否存在
205206

206207
### 问题处理

gcm.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (G *Gcm) Commit(m string) *Gcm {
3535
return G.do("git", "commit", "-m", m) // Fails when no staged changes exist // 当没有待提交文件时会失败
3636
}
3737

38-
// Fetch and merge and merges changes from the remote repo
38+
// Pull fetches and merges changes from the remote repo
3939
// Downloads recent commits from remote and integrates them into the branch
4040
// Use case: sync branch with remote changes when starting new work session
4141
//
@@ -57,15 +57,15 @@ func (G *Gcm) Push() *Gcm {
5757
return G.do("git", "push")
5858
}
5959

60-
// PushSetUpstreamOriginBranch pushes new branch and sets upstream tracking
60+
// PushWithUpstream pushes new branch and sets upstream tracking
6161
// Creates remote branch and establishes tracking connections to handle push and fetch
6262
// Use case: publish new branch to remote and enable simple push and fetch commands
6363
//
64-
// PushSetUpstreamOriginBranch 推送新分支并设置上游跟踪
64+
// PushWithUpstream 推送新分支并设置上游跟踪
6565
// 创建远程分支并建立跟踪连接以处理推送和拉取
6666
// 使用场景:将新分支发布到远程并启用简单的推送和拉取命令
67-
func (G *Gcm) PushSetUpstreamOriginBranch(newBranchName string) *Gcm {
68-
return G.do("git", "push", "--set-upstream", "origin", newBranchName)
67+
func (G *Gcm) PushWithUpstream(branchName string) *Gcm {
68+
return G.do("git", "push", "--set-upstream", "origin", branchName)
6969
}
7070

7171
// Reset unstages changes and keeps them in working path
@@ -97,8 +97,8 @@ func (G *Gcm) ResetHard() *Gcm {
9797
// Checkout 切换到现有分支或提交
9898
// 更改工作路径以匹配指定分支或提交状态
9999
// 使用场景:在开发分支间切换或检查过去提交
100-
func (G *Gcm) Checkout(nameBranch string) *Gcm {
101-
return G.do("git", "checkout", nameBranch)
100+
func (G *Gcm) Checkout(branchName string) *Gcm {
101+
return G.do("git", "checkout", branchName)
102102
}
103103

104104
// CheckoutNewBranch creates and switches to a new branch
@@ -108,8 +108,8 @@ func (G *Gcm) Checkout(nameBranch string) *Gcm {
108108
// CheckoutNewBranch 创建并切换到新分支
109109
// 从 HEAD 位置创建新分支并切换到该分支
110110
// 使用场景:开始新功能开发和创建测试分支
111-
func (G *Gcm) CheckoutNewBranch(nameBranch string) *Gcm {
112-
return G.do("git", "checkout", "-b", nameBranch)
111+
func (G *Gcm) CheckoutNewBranch(branchName string) *Gcm {
112+
return G.do("git", "checkout", "-b", branchName)
113113
}
114114

115115
// Init initializes a new Git repo in the path
@@ -145,26 +145,37 @@ func (G *Gcm) MergeAbort() *Gcm {
145145
return G.do("git", "merge", "--abort")
146146
}
147147

148-
// TagList shows existing tags in the repo
148+
// Tags shows existing tags in the repo
149149
// Displays version tags and release points as a complete list
150150
// Use case: examine release records and find specific version tags
151151
//
152-
// TagList 显示仓库中现有标签
152+
// Tags 显示仓库中现有标签
153153
// 显示版本标签和发布点的完整列表
154154
// 使用场景:检查发布记录和查找特定版本标签
155-
func (G *Gcm) TagList() *Gcm {
155+
func (G *Gcm) Tags() *Gcm {
156156
return G.do("git", "tag", "--list")
157157
}
158158

159-
// Tags shows existing tags in the repo (TagList name alternative)
159+
// GetTags shows existing tags in the repo (Tags name alternative)
160160
// Displays version tags and release points as a complete list
161161
// Use case: examine release records and find specific version tags
162162
//
163-
// Tags 显示仓库中现有标签(TagList 的名称替代)
163+
// GetTags 显示仓库中现有标签(Tags 的名称替代)
164164
// 显示版本标签和发布点的完整列表
165165
// 使用场景:检查发布记录和查找特定版本标签
166-
func (G *Gcm) Tags() *Gcm {
167-
return G.do("git", "tag", "--list")
166+
func (G *Gcm) GetTags() *Gcm {
167+
return G.Tags()
168+
}
169+
170+
// TagList shows existing tags in the repo (Tags name alternative)
171+
// Displays version tags and release points as a complete list
172+
// Use case: examine release records and find specific version tags
173+
//
174+
// TagList 显示仓库中现有标签(Tags 的名称替代)
175+
// 显示版本标签和发布点的完整列表
176+
// 使用场景:检查发布记录和查找特定版本标签
177+
func (G *Gcm) TagList() *Gcm {
178+
return G.Tags()
168179
}
169180

170181
// Tag creates a new tag at the commit position
@@ -218,8 +229,8 @@ func (G *Gcm) Remote() *Gcm {
218229
// RemoteAdd 添加新的远程仓库连接
219230
// 创建对外部仓库位置的命名引用
220231
// 使用场景:设置与上游仓库的连接和添加分支
221-
func (G *Gcm) RemoteAdd(name, url string) *Gcm {
222-
return G.do("git", "remote", "add", name, url)
232+
func (G *Gcm) RemoteAdd(name, remoteLink string) *Gcm {
233+
return G.do("git", "remote", "add", name, remoteLink)
223234
}
224235

225236
// RemoteRemove removes existing remote repo connection
@@ -233,14 +244,14 @@ func (G *Gcm) RemoteRemove(name string) *Gcm {
233244
return G.do("git", "remote", "remove", name)
234245
}
235246

236-
// RemoteSet updates URL of existing remote connection
247+
// RemoteSetURL updates URL of existing remote connection
237248
// Changes the location reference to point at new address
238249
// Use case: switch from HTTPS to SSH and update repo migration URLs
239250
//
240-
// RemoteSet 更新现有远程连接的 URL
251+
// RemoteSetURL 更新现有远程连接的 URL
241252
// 更改位置引用以指向新地址
242253
// 使用场景:从 HTTPS 切换到 SSH 和更新仓库迁移 URL
243-
func (G *Gcm) RemoteSet(name, remoteLink string) *Gcm {
254+
func (G *Gcm) RemoteSetURL(name, remoteLink string) *Gcm {
244255
return G.do("git", "remote", "set-url", name, remoteLink)
245256
}
246257

gcm_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestGcm_Submit(t *testing.T) {
3333
Status().
3434
Add().
3535
WhenThen(func(gcm *gitgo.Gcm) (bool, error) {
36-
return gcm.HasStagingChanges()
36+
return gcm.HasStagedChanges()
3737
}, func(gcm *gitgo.Gcm) *gitgo.Gcm {
3838
return gcm.Commit("提交代码").Push()
3939
}).
@@ -42,15 +42,13 @@ func TestGcm_Submit(t *testing.T) {
4242
}
4343

4444
// TestGcm_RemoteOperations tests remote repo management operations
45-
// Verifies Remote, RemoteAdd, RemoteRemove, and RemoteSet functions
45+
// Verifies Remote, RemoteAdd, RemoteRemove, and RemoteSetURL functions
4646
//
4747
// TestGcm_RemoteOperations 测试远程仓库管理操作
48-
// 验证 Remote、RemoteAdd、RemoteRemove 和 RemoteSet 函数
48+
// 验证 Remote、RemoteAdd、RemoteRemove 和 RemoteSetURL 函数
4949
func TestGcm_RemoteOperations(t *testing.T) {
5050
tempDIR := rese.V1(os.MkdirTemp("", "gitgo-remote-*"))
51-
defer func() {
52-
must.Done(os.RemoveAll(tempDIR))
53-
}()
51+
t.Cleanup(func() { must.Done(os.RemoveAll(tempDIR)) })
5452

5553
gcm := gitgo.New(tempDIR)
5654
gcm.Init().Done()
@@ -61,7 +59,7 @@ func TestGcm_RemoteOperations(t *testing.T) {
6159
require.Contains(t, string(output), "origin")
6260
require.Contains(t, string(output), "https://github.com/example/test.git")
6361

64-
gcm.RemoteSet("origin", "[email protected]:example/test.git").Done()
62+
gcm.RemoteSetURL("origin", "[email protected]:example/test.git").Done()
6563
output = gcm.Remote().Output()
6664
require.Contains(t, string(output), "[email protected]")
6765

0 commit comments

Comments
 (0)