Skip to content

Commit d98fec1

Browse files
committed
Improve exit code handling in git status checking functions
- Use ExecTake with switch pattern in HasStagingChanges/HasUnstagedChanges/HasChanges - Use ExecTake with switch pattern in BranchExists/RemoteBranchExists/TagExists - Use ExecTake with switch pattern in CheckStagedChanges - Change GetCommitMessage format - Add GetIgnoredFiles method with regex matching - Add comprehensive unit tests with True/False scenarios
1 parent c09b492 commit d98fec1

File tree

11 files changed

+992
-139
lines changed

11 files changed

+992
-139
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
runs-on: ubuntu-latest
2828
strategy:
2929
matrix:
30-
go: [ "1.22.x", "1.23.x", "1.24.x", "1.25.x", "stable" ]
30+
go: [ "1.25.x", "stable" ]
3131
steps:
3232
- uses: actions/checkout@v4
3333

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/go-xlan/gitgo/release.yml?branch=main&label=BUILD)](https://github.com/go-xlan/gitgo/actions/workflows/release.yml?query=branch%3Amain)
22
[![GoDoc](https://pkg.go.dev/badge/github.com/go-xlan/gitgo)](https://pkg.go.dev/github.com/go-xlan/gitgo)
33
[![Coverage Status](https://img.shields.io/coveralls/github/go-xlan/gitgo/main.svg)](https://coveralls.io/github/go-xlan/gitgo?branch=main)
4-
[![Supported Go Versions](https://img.shields.io/badge/Go-1.22--1.25-lightgrey.svg)](https://github.com/go-xlan/gitgo)
4+
[![Supported Go Versions](https://img.shields.io/badge/Go-1.25+-lightgrey.svg)](https://github.com/go-xlan/gitgo)
55
[![GitHub Release](https://img.shields.io/github/release/go-xlan/gitgo.svg)](https://github.com/go-xlan/gitgo/releases)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-xlan/gitgo)](https://goreportcard.com/report/github.com/go-xlan/gitgo)
77

@@ -196,6 +196,7 @@ func main() {
196196
- `GetCommitCount() (int, error)` - Get commit count
197197
- `GitCommitHash(ref) (string, error)` - Get commit hash with reference
198198
- `GetRemoteURL(remote) (string, error)` - Get remote repo URL
199+
- `GetIgnoredFiles() ([]string, error)` - Get files ignored in gitignore
199200

200201
### Issue Handling
201202

README.zh.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/go-xlan/gitgo/release.yml?branch=main&label=BUILD)](https://github.com/go-xlan/gitgo/actions/workflows/release.yml?query=branch%3Amain)
22
[![GoDoc](https://pkg.go.dev/badge/github.com/go-xlan/gitgo)](https://pkg.go.dev/github.com/go-xlan/gitgo)
33
[![Coverage Status](https://img.shields.io/coveralls/github/go-xlan/gitgo/main.svg)](https://coveralls.io/github/go-xlan/gitgo?branch=main)
4-
[![Supported Go Versions](https://img.shields.io/badge/Go-1.22--1.25-lightgrey.svg)](https://github.com/go-xlan/gitgo)
4+
[![Supported Go Versions](https://img.shields.io/badge/Go-1.25+-lightgrey.svg)](https://github.com/go-xlan/gitgo)
55
[![GitHub Release](https://img.shields.io/github/release/go-xlan/gitgo.svg)](https://github.com/go-xlan/gitgo/releases)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-xlan/gitgo)](https://goreportcard.com/report/github.com/go-xlan/gitgo)
77

@@ -196,6 +196,7 @@ func main() {
196196
- `GetCommitCount() (int, error)` - 获取提交数量
197197
- `GitCommitHash(ref) (string, error)` - 使用引用获取提交哈希
198198
- `GetRemoteURL(remote) (string, error)` - 获取远程仓库 URL
199+
- `GetIgnoredFiles() ([]string, error)` - 获取 gitignore 忽略的文件
199200

200201
### 问题处理
201202

gcm.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,91 @@ func (G *Gcm) PushTags() *Gcm {
199199
func (G *Gcm) PushTag(tag string) *Gcm {
200200
return G.do("git", "push", "origin", tag)
201201
}
202+
203+
// Remote lists configured remote repositories
204+
// Shows remote names and URLs to view connections
205+
// Use case: check remote configuration and confirm repo connections
206+
//
207+
// Remote 列出配置的远程仓库
208+
// 显示远程名称和 URL 以查看连接
209+
// 使用场景:检查远程配置和验证仓库连接
210+
func (G *Gcm) Remote() *Gcm {
211+
return G.do("git", "remote", "-v")
212+
}
213+
214+
// RemoteAdd adds a new remote repo connection
215+
// Creates named reference to outside repo location
216+
// Use case: set up connection to upstream repo and add forks
217+
//
218+
// RemoteAdd 添加新的远程仓库连接
219+
// 创建对外部仓库位置的命名引用
220+
// 使用场景:设置与上游仓库的连接和添加分支
221+
func (G *Gcm) RemoteAdd(name, url string) *Gcm {
222+
return G.do("git", "remote", "add", name, url)
223+
}
224+
225+
// RemoteRemove removes existing remote repo connection
226+
// Deletes named remote reference from configuration
227+
// Use case: clean up unused remotes and remove obsolete connections
228+
//
229+
// RemoteRemove 删除现有的远程仓库连接
230+
// 从配置中删除命名的远程引用
231+
// 使用场景:清理未使用的远程和删除过时的连接
232+
func (G *Gcm) RemoteRemove(name string) *Gcm {
233+
return G.do("git", "remote", "remove", name)
234+
}
235+
236+
// RemoteSet updates URL of existing remote connection
237+
// Changes the location reference to point at new address
238+
// Use case: switch from HTTPS to SSH and update repo migration URLs
239+
//
240+
// RemoteSet 更新现有远程连接的 URL
241+
// 更改位置引用以指向新地址
242+
// 使用场景:从 HTTPS 切换到 SSH 和更新仓库迁移 URL
243+
func (G *Gcm) RemoteSet(name, remoteLink string) *Gcm {
244+
return G.do("git", "remote", "set-url", name, remoteLink)
245+
}
246+
247+
// Fetch downloads objects and refs from remote repo
248+
// Gets recent commits from specified remote without merging
249+
// Use case: check remote changes when inspecting updates without merging
250+
//
251+
// Fetch 从远程仓库下载对象和引用
252+
// 从指定远程获取最新提交而不合并
253+
// 使用场景:在不合并的情况下检查远程更改以检查更新
254+
func (G *Gcm) Fetch(remote string) *Gcm {
255+
return G.do("git", "fetch", remote)
256+
}
257+
258+
// FetchAll downloads objects and refs from configured remotes
259+
// Gets recent commits from each remote in one operation
260+
// Use case: sync with multiple remotes when updating forks
261+
//
262+
// FetchAll 从配置的远程仓库下载对象和引用
263+
// 在一次操作中从每个远程获取最新提交
264+
// 使用场景:在更新分支时与多个远程同步
265+
func (G *Gcm) FetchAll() *Gcm {
266+
return G.do("git", "fetch", "--all")
267+
}
268+
269+
// PullFrom fetches and merges from specified remote and branch
270+
// Downloads changes from specified branch and integrates them
271+
// Use case: sync with non-default remote and branch combinations
272+
//
273+
// PullFrom 从指定远程和分支获取并合并
274+
// 从指定分支下载更改并集成它们
275+
// 使用场景:与非默认远程和分支组合同步
276+
func (G *Gcm) PullFrom(remote, branch string) *Gcm {
277+
return G.do("git", "pull", remote, branch)
278+
}
279+
280+
// PushTo pushes commits to specified remote and branch
281+
// Uploads branch changes to specified remote target
282+
// Use case: push to non-default remotes and custom branch destinations
283+
//
284+
// PushTo 将提交推送到指定远程和分支
285+
// 将分支更改上传到指定远程目标
286+
// 使用场景:推送到非默认远程和自定义分支目标
287+
func (G *Gcm) PushTo(remote, branch string) *Gcm {
288+
return G.do("git", "push", remote, branch)
289+
}

gcm_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package gitgo_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/go-xlan/gitgo"
8+
"github.com/stretchr/testify/require"
9+
"github.com/yyle88/must"
10+
"github.com/yyle88/rese"
711
"github.com/yyle88/runpath"
812
)
913

1014
// TestGcm_Status tests basic Git status command execution
11-
// Verifies working tree state display with debug output
15+
// Verifies working tree state shown with debug output
1216
// Uses parent path as the Git space location
1317
func TestGcm_Status(t *testing.T) {
1418
gcm := gitgo.New(runpath.PARENT.Path())
@@ -20,7 +24,7 @@ func TestGcm_Status(t *testing.T) {
2024

2125
// TestGcm_Submit tests condition-based commit and push workflow
2226
// Demonstrates WhenThen pattern to commit and push when changes exist
23-
// Disabled by default (if false) to prevent unintended commits in tests
27+
// Disabled on default (if false) to prevent unintended commits in tests
2428
func TestGcm_Submit(t *testing.T) {
2529
gcm := gitgo.New(runpath.PARENT.Path())
2630

@@ -36,3 +40,32 @@ func TestGcm_Submit(t *testing.T) {
3640
MustDone()
3741
}
3842
}
43+
44+
// TestGcm_RemoteOperations tests remote repo management operations
45+
// Verifies Remote, RemoteAdd, RemoteRemove, and RemoteSet functions
46+
//
47+
// TestGcm_RemoteOperations 测试远程仓库管理操作
48+
// 验证 Remote、RemoteAdd、RemoteRemove 和 RemoteSet 函数
49+
func TestGcm_RemoteOperations(t *testing.T) {
50+
tempDIR := rese.V1(os.MkdirTemp("", "gitgo-remote-*"))
51+
defer func() {
52+
must.Done(os.RemoveAll(tempDIR))
53+
}()
54+
55+
gcm := gitgo.New(tempDIR)
56+
gcm.Init().Done()
57+
58+
gcm.RemoteAdd("origin", "https://github.com/example/test.git").Done()
59+
60+
output := gcm.Remote().Output()
61+
require.Contains(t, string(output), "origin")
62+
require.Contains(t, string(output), "https://github.com/example/test.git")
63+
64+
gcm.RemoteSet("origin", "git@github.com:example/test.git").Done()
65+
output = gcm.Remote().Output()
66+
require.Contains(t, string(output), "git@github.com")
67+
68+
gcm.RemoteRemove("origin").Done()
69+
output = gcm.Remote().Output()
70+
require.NotContains(t, string(output), "origin")
71+
}

0 commit comments

Comments
 (0)