Skip to content

Commit 5d239d6

Browse files
committed
ensure cleaned hawser objects exist before pushing
1 parent cb666d2 commit 5d239d6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

commands/command_push.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package commands
22

33
import (
4+
"fmt"
45
"github.com/hawser/git-hawser/git"
56
"github.com/hawser/git-hawser/hawser"
7+
"github.com/hawser/git-hawser/pointer"
68
"github.com/hawser/git-hawser/scanner"
79
"github.com/rubyist/tracerx"
810
"github.com/spf13/cobra"
911
"io/ioutil"
1012
"os"
13+
"path/filepath"
1114
"strings"
1215
)
1316

@@ -138,6 +141,10 @@ func pushAsset(oid, filename string, index, totalFiles int) *hawser.WrappedError
138141
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
139142
}
140143

144+
if err := ensureFile(filename, path); err != nil {
145+
return hawser.Errorf(err, "Error uploading file %s (%s)", filename, oid)
146+
}
147+
141148
cb, file, cbErr := hawser.CopyCallbackFile("push", filename, index, totalFiles)
142149
if cbErr != nil {
143150
Error(cbErr.Error())
@@ -150,6 +157,41 @@ func pushAsset(oid, filename string, index, totalFiles int) *hawser.WrappedError
150157
return hawser.Upload(path, filename, cb)
151158
}
152159

160+
// ensureFile makes sure that the cleanPath exists before pushing it. If it
161+
// does not exist, it attempts to clean it by reading the file at smudgePath.
162+
func ensureFile(smudgePath, cleanPath string) error {
163+
if _, err := os.Stat(cleanPath); err == nil {
164+
return nil
165+
}
166+
167+
expectedOid := filepath.Base(cleanPath)
168+
localPath := filepath.Join(hawser.LocalWorkingDir, smudgePath)
169+
file, err := os.Open(localPath)
170+
if err != nil {
171+
return err
172+
}
173+
174+
defer file.Close()
175+
176+
stat, err := file.Stat()
177+
if err != nil {
178+
return err
179+
}
180+
181+
cleaned, err := pointer.Clean(file, stat.Size(), nil)
182+
if err != nil {
183+
return err
184+
}
185+
186+
cleaned.Close()
187+
188+
if expectedOid != cleaned.Oid {
189+
return fmt.Errorf("Expected %s to have an OID of %s, got %s", smudgePath, expectedOid, cleaned.Oid)
190+
}
191+
192+
return nil
193+
}
194+
153195
// decodeRefs pulls the sha1s out of the line read from the pre-push
154196
// hook's stdin.
155197
func decodeRefs(input string) (string, string) {

0 commit comments

Comments
 (0)