@@ -64,6 +64,17 @@ type Repository struct {
6464 path string
6565}
6666
67+ // smartJoin returns the path that can be described as `relPath`
68+ // relative to `path`, given that `path` is either absolute or is
69+ // relative to the current directory.
70+ func smartJoin (path , relPath string ) string {
71+ if filepath .IsAbs (relPath ) {
72+ return relPath
73+ } else {
74+ return filepath .Join (path , relPath )
75+ }
76+ }
77+
6778func NewRepository (path string ) (* Repository , error ) {
6879 cmd := exec .Command ("git" , "-C" , path , "rev-parse" , "--git-dir" )
6980 out , err := cmd .Output ()
@@ -87,19 +98,42 @@ func NewRepository(path string) (*Repository, error) {
8798 return nil , err
8899 }
89100 }
90- gitDir := string (bytes .TrimSpace (out ))
91- if ! filepath .IsAbs (gitDir ) {
92- gitDir = filepath .Join (path , gitDir )
93- }
94- repo := & Repository {
95- path : gitDir ,
101+ gitDir := smartJoin (path , string (bytes .TrimSpace (out )))
102+
103+ cmd = exec .Command ("git" , "rev-parse" , "--git-path" , "shallow" )
104+ cmd .Dir = gitDir
105+ out , err = cmd .Output ()
106+ if err != nil {
107+ return nil , errors .New (
108+ fmt .Sprintf (
109+ "could not run 'git rev-parse --git-path shallow': %s" , err ,
110+ ),
111+ )
112+ }
113+ shallow := smartJoin (gitDir , string (bytes .TrimSpace (out )))
114+ _ , err = os .Lstat (shallow )
115+ if err == nil {
116+ return nil , errors .New ("this appears to be a shallow clone; full clone required" )
96117 }
97- return repo , nil
118+
119+ return & Repository {path : gitDir }, nil
98120}
99121
100- func (repo * Repository ) gitCommand (args ... string ) * exec.Cmd {
122+ func (repo * Repository ) gitCommand (callerArgs ... string ) * exec.Cmd {
123+ // Disable replace references when running our commands:
124+ args := []string {"--no-replace-objects" }
125+
126+ args = append (args , callerArgs ... )
127+
101128 cmd := exec .Command ("git" , args ... )
102- cmd .Env = append (os .Environ (), "GIT_DIR=" + repo .path )
129+
130+ cmd .Env = append (
131+ os .Environ (),
132+ "GIT_DIR=" + repo .path ,
133+ // Disable grafts when running our commands:
134+ "GIT_GRAFT_FILE=" + os .DevNull ,
135+ )
136+
103137 return cmd
104138}
105139
0 commit comments