Skip to content

Commit e19b4c7

Browse files
authored
Merge pull request #4 from github/check-repo-exists
Check that `git` can be run and that the repository exists
2 parents b949b5b + f6f4e19 commit e19b4c7

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ Is your Git repository bursting at the seams?
8080

8181
* On other platforms: TBD
8282

83-
3. Run:
83+
3. Change to the directory containing the Git repository, then run
8484

85-
git sizer [<opt>...] [<path-to-git-repository>]
85+
git sizer [<opt>...]
8686

8787
To get a summary of the current repository, all you need is
8888

git-sizer.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (v *NegatedBoolValue) IsBoolFlag() bool {
4343
func main() {
4444
err := mainImplementation()
4545
if err != nil {
46-
fmt.Fprintf(os.Stderr, "%s\n", err)
46+
fmt.Fprintf(os.Stderr, "error: %s\n", err)
4747
os.Exit(1)
4848
}
4949
}
@@ -107,19 +107,13 @@ func mainImplementation() error {
107107

108108
args := flag.Args()
109109

110-
var path string
111-
switch len(args) {
112-
case 0:
113-
path = "."
114-
case 1:
115-
path = args[0]
116-
default:
110+
if len(args) != 0 {
117111
return errors.New("excess arguments")
118112
}
119113

120-
repo, err := sizes.NewRepository(path)
114+
repo, err := sizes.NewRepository(".")
121115
if err != nil {
122-
return fmt.Errorf("couldn't open %v: %s", path, err)
116+
return fmt.Errorf("couldn't open Git repository: %s", err)
123117
}
124118
defer repo.Close()
125119

sizes/git.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,35 @@ type Repository struct {
5757
}
5858

5959
func NewRepository(path string) (*Repository, error) {
60-
return &Repository{
61-
path: path,
62-
}, nil
60+
command := exec.Command(
61+
"git", "-C", path,
62+
"rev-parse", "--git-dir",
63+
)
64+
out, err := command.Output()
65+
if err != nil {
66+
switch err := err.(type) {
67+
case *exec.Error:
68+
return nil, errors.New(
69+
fmt.Sprintf(
70+
"could not run git (is it in your PATH?): %s",
71+
err.Err,
72+
),
73+
)
74+
case *exec.ExitError:
75+
return nil, errors.New(
76+
fmt.Sprintf(
77+
"git rev-parse failed: %s",
78+
err.Stderr,
79+
),
80+
)
81+
default:
82+
return nil, err
83+
}
84+
}
85+
repo := &Repository{
86+
path: string(bytes.TrimSpace(out)),
87+
}
88+
return repo, nil
6389
}
6490

6591
func (repo *Repository) Close() error {

sizes/graph.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ func ScanRepositoryUsingGraph(
2626
if err != nil {
2727
return HistorySize{}, err
2828
}
29-
defer refIter.Close()
29+
defer func() {
30+
if refIter != nil {
31+
refIter.Close()
32+
}
33+
}()
3034

3135
iter, in, err := repo.NewObjectIter("--stdin", "--date-order")
3236
if err != nil {
3337
return HistorySize{}, err
3438
}
35-
defer iter.Close()
39+
defer func() {
40+
if iter != nil {
41+
iter.Close()
42+
}
43+
}()
3644

3745
errChan := make(chan error, 1)
3846
var refs []Reference
@@ -67,7 +75,9 @@ func ScanRepositoryUsingGraph(
6775
return
6876
}
6977
}
70-
errChan <- nil
78+
err := refIter.Close()
79+
refIter = nil
80+
errChan <- err
7181
}()
7282

7383
type ObjectHeader struct {
@@ -154,11 +164,21 @@ func ScanRepositoryUsingGraph(
154164
return HistorySize{}, err
155165
}
156166

167+
err = iter.Close()
168+
iter = nil
169+
if err != nil {
170+
return HistorySize{}, err
171+
}
172+
157173
objectIter, objectIn, err := repo.NewBatchObjectIter()
158174
if err != nil {
159175
return HistorySize{}, err
160176
}
161-
defer objectIter.Close()
177+
defer func() {
178+
if objectIter != nil {
179+
objectIter.Close()
180+
}
181+
}()
162182

163183
go func() {
164184
defer objectIn.Close()
@@ -297,6 +317,12 @@ func ScanRepositoryUsingGraph(
297317
return HistorySize{}, err
298318
}
299319

320+
err = objectIter.Close()
321+
objectIter = nil
322+
if err != nil {
323+
return HistorySize{}, err
324+
}
325+
300326
progressMeter.Start("Processing references: %d")
301327
for _, ref := range refs {
302328
progressMeter.Inc()

0 commit comments

Comments
 (0)