Example #1
0
func (repo *DiskRepository) Refs() ([]objects.Ref, error) {

	// First, get all the packed refs.
	pr, err := repo.PackedRefs()
	if err != nil && !os.IsNotExist(err) {
		return nil, err
	}

	// Refs will be stores in a map by their symbolic name.
	refs := make(map[string]objects.Ref)
	for _, ref := range pr {
		refs[ref.Name()] = ref
	}

	// Now let's walk loose refs and collect them to supercede
	// the packed refs. It is worth it to note here that
	// packed refs may contain outdated references because
	// they are updated lazily.
	dir := path.Join(repo.path, "refs")
	err = filepath.Walk(dir,
		func(path string, f os.FileInfo, err error) error {
			// refs are files, so...
			if !f.IsDir() {
				name := util.TrimPrefix(path, repo.path+"/")
				r, e := PeeledRefFromSpec(repo, name)
				if e != nil {
					return e
				}
				refs[name] = objects.NewRef(name, "", r.ObjectId(), nil)
			}
			return nil
		},
	)
	if err != nil {
		return nil, err
	}

	// collect the refs into a list
	refList := make([]objects.Ref, 0, len(refs))
	for _, v := range refs {
		refList = append(refList, v)
	}
	sort.Sort(refByName(refList))
	return refList, nil
}
Example #2
0
func (repo *DiskRepository) LooseRefs() ([]objects.Ref, error) {
	// TODO: figure out a way to decouple this logic
	repoPath := repo.path + "/"
	dir := path.Join(repoPath, "refs")
	refs := make([]objects.Ref, 0)
	err := filepath.Walk(dir,
		func(path string, f os.FileInfo, err error) error {
			if !f.IsDir() {
				name := util.TrimPrefix(path, repoPath)
				r, e := PeeledRefFromSpec(repo, name)
				if e != nil {
					return e
				}
				refs = append(refs, objects.NewRef(name, "", r.ObjectId(), nil))
			}
			return nil
		},
	)
	return refs, err
}