diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2023-06-25 19:44:02 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2023-06-26 17:25:31 +0300 |
commit | 1524d5384dd5c305d09443f4a53732d86551dede (patch) | |
tree | d463eca0c1181200b440928c5cd780e7cc0135a5 | |
parent | 777444e21db329788cc0644bce1696542e514df5 (diff) |
darwin: Usage of extra flag for tar on MacOSps-macos-tar-flags
MacOS operating system of version lower than 13 produces extra files
while creating a tarball archive. Then details can be found in the
answer https://apple.stackexchange.com/a/76077.
The extra flag '--no-mac-metadata' needs to be added to the tar
command to overcome it.
-rw-r--r-- | internal/archive/archive.go | 8 | ||||
-rw-r--r-- | internal/archive/flags.go | 5 | ||||
-rw-r--r-- | internal/archive/flags_darwin.go | 19 | ||||
-rw-r--r-- | internal/darwin/version.go | 42 | ||||
-rw-r--r-- | internal/gitaly/service/repository/create_repository_from_snapshot_test.go | 3 |
5 files changed, 69 insertions, 8 deletions
diff --git a/internal/archive/archive.go b/internal/archive/archive.go index 20735edfe..7212865c1 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "runtime" "gitlab.com/gitlab-org/gitaly/v16/internal/command" ) @@ -13,12 +12,7 @@ import ( // containing the specified archive members. Members should be specified // relative to `path`. func WriteTarball(ctx context.Context, writer io.Writer, path string, members ...string) error { - cmdArgs := []string{"-c", "-f", "-", "-C", path} - - if runtime.GOOS == "darwin" { - cmdArgs = append(cmdArgs, "--no-mac-metadata") - } - + cmdArgs := append([]string{"-c", "-f", "-", "-C", path}, extraFlags()...) cmdArgs = append(cmdArgs, members...) cmd, err := command.New(ctx, append([]string{"tar"}, cmdArgs...), command.WithStdout(writer)) diff --git a/internal/archive/flags.go b/internal/archive/flags.go new file mode 100644 index 000000000..16e196c98 --- /dev/null +++ b/internal/archive/flags.go @@ -0,0 +1,5 @@ +//go:build !darwin + +package archive + +func extraFlags() []string { return nil } diff --git a/internal/archive/flags_darwin.go b/internal/archive/flags_darwin.go new file mode 100644 index 000000000..7050d2512 --- /dev/null +++ b/internal/archive/flags_darwin.go @@ -0,0 +1,19 @@ +package archive + +import ( + "gitlab.com/gitlab-org/gitaly/v16/internal/darwin" +) + +// MacOS operating system of version lower than 13 produces extra files while creating a tarball archive. +// Then details can be found in the answer https://apple.stackexchange.com/a/76077. +// The extra flag '--no-mac-metadata' needs to be added to the tar command to overcome it. +// We can drop this hack once moved to a newer version of MacOS used in our 'test:macos' CI job. +// Also, the function 'generateTarFile()' in internal/gitaly/service/repository/create_repository_from_snapshot_test.go +// can be simplified after version upgrade. +func extraFlags() []string { + if darwin.MajorVersion() < 13 { + return []string{"--no-mac-metadata"} + } + + return nil +} diff --git a/internal/darwin/version.go b/internal/darwin/version.go new file mode 100644 index 000000000..a4bd591a8 --- /dev/null +++ b/internal/darwin/version.go @@ -0,0 +1,42 @@ +package darwin + +import ( + "context" + "fmt" + "strconv" + "strings" + "sync" + + "gitlab.com/gitlab-org/gitaly/v16/internal/command" +) + +var ( + majorVersion int + majorVersionOnce sync.Once +) + +// MajorVersion returns a major number of the MacOS version. +func MajorVersion() int { + majorVersionOnce.Do(func() { + var buffer strings.Builder + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cmd, err := command.New(ctx, []string{"sw_vers", "-productVersion"}, command.WithStdout(&buffer)) + if err != nil { + panic(fmt.Errorf("executing sw_vers command: %w", err)) + } + if err := cmd.Wait(); err != nil { + panic(fmt.Errorf("waiting for sw_vers command completion: %w", err)) + } + parts := strings.Split(buffer.String(), ".") + if len(parts) > 0 { + version, err := strconv.ParseFloat(parts[0], 32) + if err != nil { + panic(fmt.Errorf("parse mac version: %w", err)) + } + majorVersion = int(version) + } + }) + + return majorVersion +} diff --git a/internal/gitaly/service/repository/create_repository_from_snapshot_test.go b/internal/gitaly/service/repository/create_repository_from_snapshot_test.go index 416d0b731..deacb432e 100644 --- a/internal/gitaly/service/repository/create_repository_from_snapshot_test.go +++ b/internal/gitaly/service/repository/create_repository_from_snapshot_test.go @@ -16,6 +16,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v16/internal/darwin" "gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/archive" "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage" @@ -58,7 +59,7 @@ func (h *tarTesthandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Create a tar file for the repo in memory, without relying on TarBuilder func generateTarFile(t *testing.T, path string) ([]byte, []string) { var data []byte - if runtime.GOOS == "darwin" { + if runtime.GOOS == "darwin" && darwin.MajorVersion() < 13 { data = testhelper.MustRunCommand(t, nil, "tar", "-C", path, "--no-mac-metadata", "-cf", "-", ".") } else { data = testhelper.MustRunCommand(t, nil, "tar", "-C", path, "-cf", "-", ".") |