Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds new retar function, checks for errors #4

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CLI tool to remap commits during a migration after a history rewrite
## Install

```bash
gh extension install kuhlman-labs/gh-commit-remap
gh extension install mona-actions/gh-commit-remap
```

## Upgrade
Expand All @@ -27,5 +27,5 @@ Usage:
Flags:
-h, --help help for gh-commit-remap
-c, --mapping-file string Path to the commit map file Example: /path/to/commit-map
-m, --migration-archive string Path to the migration archive Example: /path/to/migration-archive.tar.gz
-m, --migration-archive string Path to the migration archive Example: /path/to/migration-archive
```
14 changes: 13 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"

"github.com/mona-actions/gh-commit-remap/internal/archive"
"github.com/mona-actions/gh-commit-remap/internal/commitremap"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,7 +38,18 @@ var rootCmd = &cobra.Command{

archivePath, _ := cmd.Flags().GetString("migration-archive")

commitremap.ProcessFiles(archivePath, types, commitMap)
err = commitremap.ProcessFiles(archivePath, types, commitMap)
if err != nil {
log.Fatal(err)
}

tarPath, err := archive.ReTar(archivePath)
if err != nil {
log.Fatal(err)
}

log.Printf("New archive created: %s", tarPath)

},
}

Expand Down
37 changes: 37 additions & 0 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package archive

import (
"fmt"
"os/exec"
"path/filepath"
)

// reTarFiles creates a new tar archive from the files in the given directory.
// The name of the archive is the same as the directory name.
func ReTar(archivePath string) (string, error) {
// Extract the directory name from the archivePath
dirName := filepath.Base(archivePath)

// Create the name of the new archive
archiveName := dirName + ".tar.gz"

err := checkTarAvailability()
if err != nil {
return "", err
}

// Create and run the tar command
tarCmd := exec.Command("tar", "-czf", archiveName, "-C", archivePath, ".")
err = tarCmd.Run()
if err != nil {
return "", fmt.Errorf("error re-tarring the files: %w", err)
}

return archiveName, nil
}

// checkTarAvailability checks if the 'tar' command is available in the system's PATH.
func checkTarAvailability() error {
_, err := exec.LookPath("tar")
return err
}
64 changes: 64 additions & 0 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package archive

import (
"bytes"
"os"
"os/exec"
"path/filepath"
"testing"
)

func TestReTar(t *testing.T) {
// Create a temporary directory
tempDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// Create a file in the temporary directory
tempFile := filepath.Join(tempDir, "testfile")
origContent := []byte("test")
if err := os.WriteFile(tempFile, origContent, 0644); err != nil {
t.Fatalf("Failed to write temp file: %v", err)
}

// Call the function under test
tarFile, err := ReTar(tempDir)
if err != nil {
t.Fatalf("ReTar failed: %v", err)
}

// Check if the tar file was created
if _, err := os.Stat(tarFile); os.IsNotExist(err) {
t.Fatalf("Tar file was not created: %v", err)
}

// Ensure the tar file is removed after the test
defer os.Remove(tarFile)

// Extract the tar file to a new directory
extractDir, err := os.MkdirTemp("", "extract")
if err != nil {
t.Fatalf("Failed to create extract directory: %v", err)
}
defer os.RemoveAll(extractDir)

tarCmd := exec.Command("tar", "-xzf", tarFile, "-C", extractDir)
err = tarCmd.Run()
if err != nil {
t.Fatalf("Failed to extract tar file: %v", err)
}

// Read the contents of the extracted file
extractedFile := filepath.Join(extractDir, "testfile")
extractedContent, err := os.ReadFile(extractedFile)
if err != nil {
t.Fatalf("Failed to read extracted file: %v", err)
}

// Compare the contents of the original file and the extracted file
if !bytes.Equal(origContent, extractedContent) {
t.Fatalf("Original file content and extracted file content do not match")
}
}
20 changes: 13 additions & 7 deletions internal/commitremap/commitremap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ParseCommitMap(filePath string) (*[]CommitMapEntry, error) {
return &commitMap, nil
}

func ProcessFiles(archiveLocation string, prefixes []string, commitMap *[]CommitMapEntry) {
func ProcessFiles(archiveLocation string, prefixes []string, commitMap *[]CommitMapEntry) error {

for _, prefix := range prefixes {
// Get a list of all files that match the pattern
Expand All @@ -59,22 +59,26 @@ func ProcessFiles(archiveLocation string, prefixes []string, commitMap *[]Commit
for _, file := range files {
log.Println("Processing file:", file)

updateMetadataFile(file, commitMap)
err := updateMetadataFile(file, commitMap)
if err != nil {
return fmt.Errorf("Error updating metadata file: %v; %v", file, err)
}
}
}
return nil
}

func updateMetadataFile(filePath string, commitMap *[]CommitMapEntry) {
func updateMetadataFile(filePath string, commitMap *[]CommitMapEntry) error {
// Read the JSON file
data, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading data: %v", err)
return fmt.Errorf("Error reading data: %v", err)
}

var dataMap interface{}
err = json.Unmarshal(data, &dataMap)
if err != nil {
log.Fatalf("Error unmarshaling data: %v", err)
return fmt.Errorf("Error unmarshaling data: %v", err)
}

// Iterate over the commit map and replace the old commit hashes with the new ones
Expand All @@ -85,14 +89,16 @@ func updateMetadataFile(filePath string, commitMap *[]CommitMapEntry) {
// Marshal the updated data to JSON and pretty print it
updatedData, err := json.MarshalIndent(dataMap, "", " ")
if err != nil {
log.Fatalf("Error marshaling updated data: %v", err)
return fmt.Errorf("Error marshaling updated data: %v", err)
}

// Overwrite the original file with the updated data
err = os.WriteFile(filePath, updatedData, 0644)
if err != nil {
log.Fatalf("Error writing updated data: %v", err)
return fmt.Errorf("Error writing updated data: %v", err)
}

return nil
}

func replaceSHA(data interface{}, oldSHA string, newSHA string) {
Expand Down
Loading