From 496225671f9fe9decac5d80ac65f86df3a5f45a0 Mon Sep 17 00:00:00 2001 From: Alejandro Menocal Date: Fri, 2 Aug 2024 17:37:15 -0500 Subject: [PATCH 1/4] adds new retar function, checks for errors --- cmd/root.go | 12 +++++++++- internal/archive/archive.go | 37 +++++++++++++++++++++++++++++ internal/commitremap/commitremap.go | 20 ++++++++++------ 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 internal/archive/archive.go diff --git a/cmd/root.go b/cmd/root.go index 994fbc7..5f9e4a5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" ) @@ -37,7 +38,16 @@ 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) + } + + err = archive.ReTar(archivePath) + if err != nil { + log.Fatal(err) + } + }, } diff --git a/internal/archive/archive.go b/internal/archive/archive.go new file mode 100644 index 0000000..f34a092 --- /dev/null +++ b/internal/archive/archive.go @@ -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) 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 nil +} + +// checkTarAvailability checks if the 'tar' command is available in the system's PATH. +func checkTarAvailability() error { + _, err := exec.LookPath("tar") + return err +} diff --git a/internal/commitremap/commitremap.go b/internal/commitremap/commitremap.go index a238dc1..27e6ef0 100644 --- a/internal/commitremap/commitremap.go +++ b/internal/commitremap/commitremap.go @@ -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 @@ -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 @@ -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) { From 382921c97ba4c4067e5cf2558a06f548524f7355 Mon Sep 17 00:00:00 2001 From: Alejandro Menocal Date: Thu, 8 Aug 2024 17:49:43 -0500 Subject: [PATCH 2/4] add test fix test --- internal/archive/archive_test.go | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 internal/archive/archive_test.go diff --git a/internal/archive/archive_test.go b/internal/archive/archive_test.go new file mode 100644 index 0000000..5d081fd --- /dev/null +++ b/internal/archive/archive_test.go @@ -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") + } +} From 6ba654f7ed3a3436dbe2d6df01f1e356665a1238 Mon Sep 17 00:00:00 2001 From: Alejandro Menocal Date: Thu, 8 Aug 2024 17:57:22 -0500 Subject: [PATCH 3/4] return where the tar was created --- cmd/root.go | 4 +++- internal/archive/archive.go | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5f9e4a5..f94c337 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,11 +43,13 @@ var rootCmd = &cobra.Command{ log.Fatal(err) } - err = archive.ReTar(archivePath) + tarPath, err := archive.ReTar(archivePath) if err != nil { log.Fatal(err) } + log.Printf("New archive created: %s", tarPath) + }, } diff --git a/internal/archive/archive.go b/internal/archive/archive.go index f34a092..45104eb 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -8,7 +8,7 @@ import ( // 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) error { +func ReTar(archivePath string) (string, error) { // Extract the directory name from the archivePath dirName := filepath.Base(archivePath) @@ -17,17 +17,17 @@ func ReTar(archivePath string) error { err := checkTarAvailability() if err != nil { - return err + 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 "", fmt.Errorf("error re-tarring the files: %w", err) } - return nil + return archiveName, nil } // checkTarAvailability checks if the 'tar' command is available in the system's PATH. From 9ff04633b485c36eda7a2d21e08457178c24ae2c Mon Sep 17 00:00:00 2001 From: Alejandro Menocal Date: Fri, 9 Aug 2024 14:56:45 -0500 Subject: [PATCH 4/4] fix docs, and example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fc95ff9..0fcfa7d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ```