-
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.
initial changes to support reverting files
- Loading branch information
1 parent
ca30fed
commit f19ca62
Showing
7 changed files
with
211 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,51 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// RestoreUsing ...(against HEAD only) | ||
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 ") | ||
} | ||
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,51 @@ | ||
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) { | ||
// TODO: committed files | ||
// TODO: modify one and stage | ||
// TODO: modify the other and do not stage | ||
} | ||
|
||
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.