MCPcopy Create free account
hub / github.com/cli/cli / TestExecuteLocalRepoSyncNonCurrentBranch

Function TestExecuteLocalRepoSyncNonCurrentBranch

pkg/cmd/repo/sync/sync_test.go:121–221  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

119}
120
121func TestExecuteLocalRepoSyncNonCurrentBranch(t *testing.T) {
122 t.Run("branch checked out in another worktree", func(t *testing.T) {
123 repoDir := initSyncTestRepo(t)
124 worktreeDir := filepath.Join(t.TempDir(), "trunk-worktree")
125
126 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("old\n"), 0o600))
127 runGit(t, repoDir, "add", "file.txt")
128 runGit(t, repoDir, "commit", "--quiet", "--message=initial")
129 runGit(t, repoDir, "switch", "--quiet", "--create", "test")
130 runGit(t, repoDir, "worktree", "add", "--quiet", worktreeDir, "trunk")
131 originalBranch := runGit(t, repoDir, "rev-parse", "trunk")
132
133 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("new\n"), 0o600))
134 runGit(t, repoDir, "commit", "--quiet", "--all", "--message=upstream")
135 runGit(t, repoDir, "fetch", "--quiet", ".", "test")
136
137 opts := &SyncOptions{
138 Branch: "trunk",
139 Git: &gitExecuter{client: &git.Client{RepoDir: repoDir}},
140 }
141
142 err := executeLocalRepoSync(ghrepo.New("OWNER", "REPO"), "origin", opts)
143 require.ErrorContains(t, err, `can't sync "trunk" because it's checked out in another worktree at `)
144 // Assert on the directory name rather than the full path because git reports the
145 // symlink-resolved path, which differs from t.TempDir() on macOS.
146 require.ErrorContains(t, err, filepath.Base(worktreeDir))
147 require.ErrorContains(t, err, "tip: run `gh repo sync` from that worktree instead")
148 require.Equal(t, originalBranch, runGit(t, repoDir, "rev-parse", "trunk"))
149 require.Empty(t, runGit(t, worktreeDir, "status", "--porcelain"))
150 })
151
152 t.Run("branch associated with prunable worktree", func(t *testing.T) {
153 repoDir := initSyncTestRepo(t)
154 worktreeDir := filepath.Join(t.TempDir(), "missing-trunk-worktree")
155
156 runGit(t, repoDir, "commit", "--quiet", "--allow-empty", "--message=initial")
157 runGit(t, repoDir, "switch", "--quiet", "--create", "test")
158 runGit(t, repoDir, "worktree", "add", "--quiet", worktreeDir, "trunk")
159 require.NoError(t, os.RemoveAll(worktreeDir))
160 runGit(t, repoDir, "commit", "--quiet", "--allow-empty", "--message=upstream")
161 runGit(t, repoDir, "fetch", "--quiet", ".", "test")
162
163 opts := &SyncOptions{
164 Branch: "trunk",
165 Git: &gitExecuter{client: &git.Client{RepoDir: repoDir}},
166 }
167
168 err := executeLocalRepoSync(ghrepo.New("OWNER", "REPO"), "origin", opts)
169 require.ErrorContains(t, err, `can't sync "trunk" because stale worktree metadata still associates it with `)
170 require.ErrorContains(t, err, filepath.Base(worktreeDir))
171 require.ErrorContains(t, err, "tip: run `git worktree prune` and retry")
172 })
173
174 t.Run("fast-forwards non-current branch", func(t *testing.T) {
175 repoDir := initSyncTestRepo(t)
176
177 require.NoError(t, os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("old\n"), 0o600))
178 runGit(t, repoDir, "add", "file.txt")

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
initSyncTestRepoFunction · 0.85
runGitFunction · 0.85
executeLocalRepoSyncFunction · 0.85
JoinMethod · 0.80
BaseMethod · 0.80
EqualMethod · 0.80
EmptyMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected