Skip to content

Commit f788f7e

Browse files
authored
Merge pull request #31 from github/disable-replace-and-grafts
Disable replace references and grafts; require a non-shallow clone
2 parents b607644 + cb3c1c7 commit f788f7e

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Is your Git repository bursting at the seams?
7878

7979
b. Build and install from source. See the instructions in [`docs/BUILDING.md`](docs/BUILDING.md).
8080

81-
3. Change to the directory containing the Git repository that you'd like to analyze, then run
81+
3. Change to the directory containing a full, non-shallow clone of the Git repository that you'd like to analyze. Then run
8282

8383
git sizer [<option>...]
8484

git/git.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
6778
func 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

Comments
 (0)