@@ -64,6 +64,17 @@ type Repository struct {
64
64
path string
65
65
}
66
66
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
+
67
78
func NewRepository (path string ) (* Repository , error ) {
68
79
cmd := exec .Command ("git" , "-C" , path , "rev-parse" , "--git-dir" )
69
80
out , err := cmd .Output ()
@@ -87,19 +98,42 @@ func NewRepository(path string) (*Repository, error) {
87
98
return nil , err
88
99
}
89
100
}
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" )
96
117
}
97
- return repo , nil
118
+
119
+ return & Repository {path : gitDir }, nil
98
120
}
99
121
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
+
101
128
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
+
103
137
return cmd
104
138
}
105
139
0 commit comments