Exemple #1
0
// Decode reads from an io.Reader, presumably a packed refs file, and parses
// all the refs within it, ignoring comments and whitespace.
func (p *PackedRefs) Decode(reader io.Reader) error {
	r := bufio.NewReader(reader)

	atEof := false
	for !atEof {
		line, err := r.ReadString('\n')
		if err == io.EOF {
			atEof = true
		} else if err != nil {
			return err
		}

		if pound := strings.IndexRune(line, '#'); pound != -1 {
			line = line[:pound]
		}
		line = strings.TrimSpace(line)

		if line == "" {
			continue
		}

		parts := strings.SplitN(line, " ", 2)
		sha1, name := parts[0], parts[1]

		hash, err := core.Sha1FromString(sha1)
		if err != nil {
			return err
		}

		p.Refs = append(p.Refs, Ref{Sha1: hash, Name: name})
	}

	return nil
}
Exemple #2
0
// Decode reads from a an io.Reader, presumably a loose ref file, and extracts
// the SHA-1 that is supposed to be in the file. If the file does not contain a
// properly formatted SHA-1 checksum, the error ErrInvalidRef is returned.
func (ref *Ref) Decode(reader io.Reader) error {
	r := bufio.NewReader(reader)

	if line, err := r.ReadString('\n'); err != nil {
		return err
	} else if len(line) != 41 {
		return ErrInvalidRef
	} else {
		if sha1, err := core.Sha1FromString(line[:]); err != nil {
			return ErrInvalidRef
		} else {
			ref.Sha1 = sha1
		}
	}

	return nil
}