示例#1
0
文件: cmdcat.go 项目: knieriem/hgo
func findPresentByNodeId(ent *store.ManifestEnt, rlist []*revlog.Rec) (index int, err error) {
	wantId, err := ent.Id()
	if err != nil {
		return
	}

	for i, r := range rlist {
		if wantId.Eq(r.Id()) {
			index = i
			return
		}
	}

	err = fmt.Errorf("internal error: none of the given records matches node id %v", wantId)
	return
}
示例#2
0
func runArchive(cmd *Command, w io.Writer, args []string) {
	openRepository(args)
	rs := getRevisionSpec()
	b := revlog.NewFileBuilder()
	c, err := getChangeset(rs, b)
	if err != nil {
		fatalf("%s", err)
	}

	var ent *store.ManifestEnt
	link := revlog.NewLinkRevSpec(int(c.Linkrev))
	link.FindPresent = func(rlist []*revlog.Rec) (index int, err error) {
		index, err = findPresentByNodeId(ent, rlist)
		return
	}

	mm, err := getManifest(link.Rev, c.ManifestNode, b)
	if err != nil {
		fatalf("%s", err)
	}
	st := repo.NewStore()

	fileArg := getFileArg(args)

	newArchive, ok := amap[*archiveType]
	if !ok {
		fatalf("unknown archive type: %s", *archiveType)
	}
	a, pathPfx, err := newArchive(fileArg)
	if err != nil {
		fatalf("%s", err)
	}

	err = createArchivalTxt(a, pathPfx, c)
	if err != nil {
		fatalf("%s", err)
	}
	pathPfx += "/"

	for i := range mm {
		ent = &mm[i]

		f, err := st.OpenRevlog(ent.FileName)
		if err != nil {
			fatalf("%s", err)
		}
		r, err := link.Lookup(f)
		if err != nil {
			fatalf("%s", err)
		}

		name := pathPfx + ent.FileName
		if ent.IsLink() {
			buf, err := b.Build(r)
			if err != nil {
				fatalf("%s", err)
			}
			err = a.symlink(name, string(buf), c.Date)
			if err != nil {
				fatalf("%s", err)
			}
		} else {
			var mode int
			if ent.IsExecutable() {
				mode = 0755
			} else {
				mode = 0644
			}
			p, err := b.PreparePatch(r)
			if err != nil {
				fatalf("%s", err)
			}

			w, err := a.createFile(name, mode, int64(r.FileLength)-int64(len(p.MetaData)), c.Date)
			if err != nil {
				fatalf("%s", err)
			}
			err = p.Apply(w)
			if err != nil {
				fatalf("%s", err)
			}
		}
	}
	err = a.Close()
	if err != nil {
		fatalf("%s", err)
	}
}