11package commands
22
33import (
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.
155197func decodeRefs (input string ) (string , string ) {
0 commit comments