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

Use crypto.sha1() instead of sha1sum command #55

Merged
merged 12 commits into from
Oct 12, 2022
Merged
3 changes: 2 additions & 1 deletion pkg/ghost/types/branchspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pfnet-research/git-ghost/pkg/ghost/git"
"github.com/pfnet-research/git-ghost/pkg/util"
"github.com/pfnet-research/git-ghost/pkg/util/errors"
"github.com/pfnet-research/git-ghost/pkg/util/hash"

multierror "github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -243,7 +244,7 @@ func (bs DiffBranchSpec) CreateBranch(we WorkingEnv) (GhostBranch, errors.GitGho
}
}

hash, err := util.GenerateFileContentHash(tmpFile.Name())
hash, err := hash.GenerateFileContentHash(tmpFile.Name())
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/util/hash.go → pkg/util/hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package hash

import (
"os/exec"
"strings"
"crypto/sha1"
"fmt"
"io"
"os"

"github.com/pfnet-research/git-ghost/pkg/util/errors"
)

func GenerateFileContentHash(filepath string) (string, errors.GitGhostError) {
// TODO: Use appropriate hash algorithm
cmd := exec.Command("sha1sum", "-b", filepath)
output, err := cmd.Output()
// ref: https://pkg.go.dev/crypto/sha1#example-New-File
f, err := os.Open(filepath)
if err != nil {
return "", errors.WithStack(err)
}
hash := strings.Split(string(output), " ")[0]
return hash, nil
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return "", errors.WithStack(err)
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
52 changes: 52 additions & 0 deletions pkg/util/hash/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019 Preferred Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hash_test

import (
"os"
"os/exec"
"strings"
"testing"

"github.com/pfnet-research/git-ghost/pkg/util/hash"
"github.com/stretchr/testify/assert"
)

func calculateHashWithCommand(filepath string) (string, error) {
cmd := exec.Command("sha1sum", "-b", filepath)
output, err := cmd.Output()
if err != nil {
return "", err
}
hash := strings.Split(string(output), " ")[0]
return hash, nil
}

// Check compatibility so that patches generated by previous versions of git-ghost can be loaded
func TestHashCompatibility(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here on why we have this test here??

tmpFile, err := os.CreateTemp(os.TempDir(), "tempfile-test-")
if err != nil {
t.Fatal(err)
}
oldHash, err := calculateHashWithCommand(tmpFile.Name())
if err != nil {
t.Fatal(err)
}
newHash, err := hash.GenerateFileContentHash(tmpFile.Name())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, oldHash, newHash)
}