Skip to content

Commit 4029e5d

Browse files
committed
ParseReference(): new function, extracted from ReferenceIter.Next()
1 parent b3cbef9 commit 4029e5d

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

git/ref_iter.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ package git
22

33
import (
44
"bufio"
5-
"fmt"
65
"io"
76
"os"
87
"os/exec"
9-
"strconv"
10-
"strings"
11-
12-
"github.com/github/git-sizer/counts"
138
)
149

1510
// ReferenceIter is an iterator that interates over references.
@@ -58,27 +53,12 @@ func (iter *ReferenceIter) Next() (Reference, bool, error) {
5853
}
5954
return Reference{}, false, nil
6055
}
61-
line = line[:len(line)-1]
62-
words := strings.Split(line, " ")
63-
if len(words) != 4 {
64-
return Reference{}, false, fmt.Errorf("line improperly formatted: %#v", line)
65-
}
66-
oid, err := NewOID(words[0])
56+
ref, err := ParseReference(line[:len(line)-1])
6757
if err != nil {
68-
return Reference{}, false, fmt.Errorf("SHA-1 improperly formatted: %#v", words[0])
58+
return ref, false, err
6959
}
70-
objectType := ObjectType(words[1])
71-
objectSize, err := strconv.ParseUint(words[2], 10, 32)
72-
if err != nil {
73-
return Reference{}, false, fmt.Errorf("object size improperly formatted: %#v", words[2])
74-
}
75-
refname := words[3]
76-
return Reference{
77-
Refname: refname,
78-
ObjectType: objectType,
79-
ObjectSize: counts.Count32(objectSize),
80-
OID: oid,
81-
}, true, nil
60+
61+
return ref, true, nil
8262
}
8363

8464
// Close closes the iterator and frees up resources.

git/reference.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package git
22

33
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
48
"github.com/github/git-sizer/counts"
59
)
610

@@ -18,3 +22,31 @@ type Reference struct {
1822
// OID is the OID of the referred-to object.
1923
OID OID
2024
}
25+
26+
// ParseReference parses `line` (a non-LF-terminated line) into a
27+
// `Reference`. It is assumed that `line` is formatted like the output
28+
// of
29+
//
30+
// git for-each-ref --format='%(objectname) %(objecttype) %(objectsize) %(refname)'
31+
func ParseReference(line string) (Reference, error) {
32+
words := strings.Split(line, " ")
33+
if len(words) != 4 {
34+
return Reference{}, fmt.Errorf("line improperly formatted: %#v", line)
35+
}
36+
oid, err := NewOID(words[0])
37+
if err != nil {
38+
return Reference{}, fmt.Errorf("SHA-1 improperly formatted: %#v", words[0])
39+
}
40+
objectType := ObjectType(words[1])
41+
objectSize, err := strconv.ParseUint(words[2], 10, 32)
42+
if err != nil {
43+
return Reference{}, fmt.Errorf("object size improperly formatted: %#v", words[2])
44+
}
45+
refname := words[3]
46+
return Reference{
47+
Refname: refname,
48+
ObjectType: objectType,
49+
ObjectSize: counts.Count32(objectSize),
50+
OID: oid,
51+
}, nil
52+
}

0 commit comments

Comments
 (0)