Example #1
0
func (repo *DiskRepository) ObjectFromShortOid(short string) (objects.Object, error) {
	l := len(short)
	if l < 4 || l > objects.OidHexSize {
		return nil, fmt.Errorf("fatal: Not a valid object name %s", short)
	}

	// don't bother with directories if we know the full SHA
	if l == objects.OidHexSize {
		oid, err := objects.OidFromString(short)
		if err != nil {
			return nil, fmt.Errorf("fatal: Not a valid object name %s", short)
		}
		return repo.ObjectFromOid(oid)
	}

	head, tail := short[:2], short[2:]
	root := path.Join(repo.path, DefaultObjectsDir, head)
	var matching []*objects.ObjectId
	e := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		// root doesn't exist, or there was a problem reading it
		if err != nil {
			return err
		}
		if !info.IsDir() {
			name := info.Name()
			if strings.HasPrefix(name, tail) {
				if oid, err := objects.OidFromString(head + name); err == nil {
					matching = append(matching, oid)
				}
			}
		}
		return nil
	})
	if e != nil {
		if os.IsNotExist(e) {
			loadPacks(repo)
			if obj, ok := parse.UnpackFromShortOid(repo.packs, short); ok {
				return obj, nil
			}
		}
		return nil, e
	}
	if len(matching) != 1 {
		return nil, fmt.Errorf("fatal: Ambiguous object name %s", short)
	}
	return repo.ObjectFromOid(matching[0])
}
Example #2
0
// ParseOid reads the next objects.OidHexSize bytes from the
// Reader and places the resulting object id in oid.
func (p *objectIdParser) ParseOid() *objects.ObjectId {
	hex := string(p.Consume(objects.OidHexSize))
	oid, e := objects.OidFromString(hex)
	if e != nil {
		util.PanicErrf("expected: hex string of size %d", objects.OidHexSize)
	}
	return oid
}
Example #3
0
func (repo *DiskRepository) LooseObjectIds() (oids []*objects.ObjectId, err error) {
	objectsRoot := path.Join(repo.path, DefaultObjectsDir)
	oids = make([]*objects.ObjectId, 0)
	//look in each objectsDir and make ObjectIds out of the files there.
	err = filepath.Walk(objectsRoot, func(path string, info os.FileInfo, errr error) error {
		if name := info.Name(); name == "info" || name == "pack" {
			return filepath.SkipDir
		} else if !info.IsDir() {
			hash := filepath.Base(filepath.Dir(path)) + name
			var oid *objects.ObjectId
			if oid, err = objects.OidFromString(hash); err != nil {
				return err
			}
			oids = append(oids, oid)
		}
		return nil
	})
	return
}