-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support reverting files back to their tracked version (#157)
- Loading branch information
1 parent
ca30fed
commit 635ccaa
Showing
7 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// RestoreUsing will restore a given set of files back to their previous | ||
// known state within the current repository (working directory). By | ||
// inspecting each files [FileStatus], the correct decision can be made | ||
// when restoring it | ||
func (c *Client) RestoreUsing(statuses []FileStatus) error { | ||
for _, status := range statuses { | ||
var err error | ||
|
||
if status.Untracked() { | ||
err = c.removeUntrackedFile(status.Path) | ||
} else if status.Modified() { | ||
err = c.restoreFile(status) | ||
} else if status.Renamed() { | ||
err = c.undoRenamedFile(status.Path) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) removeUntrackedFile(pathspec string) error { | ||
_, err := c.exec("git clean --force -- " + pathspec) | ||
return err | ||
} | ||
|
||
func (c *Client) restoreFile(status FileStatus) error { | ||
var buf strings.Builder | ||
buf.WriteString("git restore ") | ||
if status.Indicators[0] == Modified { | ||
buf.WriteString("--staged --worktree ") | ||
} | ||
buf.WriteString(status.Path) | ||
|
||
_, err := c.exec(buf.String()) | ||
return err | ||
} | ||
|
||
func (c *Client) undoRenamedFile(pathspec string) error { | ||
original, renamed, _ := strings.Cut(pathspec, porcelainRenameSeparator) | ||
|
||
_, err := c.exec(fmt.Sprintf("git mv %s %s", renamed, original)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package git_test | ||
|
||
import ( | ||
"testing" | ||
|
||
git "github.com/purpleclay/gitz" | ||
"github.com/purpleclay/gitz/gittest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRestoreUsingForUntrackedFiles(t *testing.T) { | ||
gittest.InitRepository(t, gittest.WithFiles("README.md", ".github/ci.yaml", "go.mod")) | ||
|
||
untracked := [2]git.FileStatusIndicator{git.Untracked, git.Untracked} | ||
|
||
client, _ := git.NewClient() | ||
err := client.RestoreUsing([]git.FileStatus{ | ||
{Indicators: untracked, Path: "README.md"}, | ||
{Indicators: untracked, Path: ".github/"}, | ||
{Indicators: untracked, Path: "go.mod"}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
statuses := gittest.PorcelainStatus(t) | ||
assert.Empty(t, statuses) | ||
} | ||
|
||
func TestRestoreUsingForModifiedFiles(t *testing.T) { | ||
gittest.InitRepository(t, gittest.WithCommittedFiles("main.go", "doc.go")) | ||
gittest.WriteFile(t, "main.go", "updated", 0o644) | ||
gittest.WriteFile(t, "doc.go", "updated", 0o644) | ||
gittest.StageFile(t, "main.go") | ||
|
||
client, _ := git.NewClient() | ||
err := client.RestoreUsing([]git.FileStatus{ | ||
{Indicators: [2]git.FileStatusIndicator{git.Modified, git.Untracked}, Path: "main.go"}, | ||
{Indicators: [2]git.FileStatusIndicator{git.Untracked, git.Modified}, Path: "doc.go"}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
statuses := gittest.PorcelainStatus(t) | ||
assert.Empty(t, statuses) | ||
} | ||
|
||
func TestRestoreUsingForRenamedFiles(t *testing.T) { | ||
gittest.InitRepository(t, gittest.WithCommittedFiles("main.go", "cache.go", "keys.go")) | ||
gittest.Move(t, "cache.go", "internal/cache/cache.go") | ||
gittest.Move(t, "keys.go", "internal/cache/keys.go") | ||
|
||
renamed := [2]git.FileStatusIndicator{git.Renamed, git.Unmodified} | ||
|
||
client, _ := git.NewClient() | ||
err := client.RestoreUsing([]git.FileStatus{ | ||
{Indicators: renamed, Path: "cache.go -> internal/cache/cache.go"}, | ||
{Indicators: renamed, Path: "keys.go -> internal/cache/keys.go"}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
statuses := gittest.PorcelainStatus(t) | ||
assert.Empty(t, statuses) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.