Beispiel #1
0
func tagHelper(h objects.HID, nextPathSegment string, restOfPath string,
	postBytes objects.Byteser, postType string) (objects.HID, error) {
	t, geterr := GetTag(h.Bytes(), nextPathSegment)
	posterr := error(nil)
	if geterr == nil {
		//the tag exists with that nameSegment
		nextTypeString := t.TypeString
		nextHash := t.HashBytes
		nextPath := restOfPath
		var hashOfPosted objects.HID
		if restOfPath == "" && postType != "blob" {
			hashOfPosted = objects.HKID(postBytes.Bytes()) //insrt reference by HKID
		} else {
			hashOfPosted, posterr = post(nextHash, nextPath,
				nextTypeString, postBytes, postType)
		}
		t = t.Update(hashOfPosted, t.TypeString)
	} else {
		//no tag exists with that nameSegment
		_, err := GetPrivateKeyForHkid(h.(objects.HKID))
		if err == nil {
			//you own the Domain

			nextHash := objects.HID(nil)
			nextPath := restOfPath
			nextTypeString := "list"
			if restOfPath == "" {
				nextTypeString = postType
			}
			log.Printf("tag pointing to a %s named %s", nextTypeString, nextPathSegment)
			var hashOfPosted objects.HID
			if restOfPath == "" && postType != "blob" {
				hashOfPosted = objects.HKID(postBytes.Bytes()) //insert reference by HKID
			} else {
				hashOfPosted, posterr = post(nextHash, nextPath,
					nextTypeString, postBytes, postType)
			}
			t = objects.NewTag(hashOfPosted, nextTypeString, nextPathSegment, nil, h.Bytes())
		} else {
			log.Printf("You don't seem to own this Domain")
			return nil, fmt.Errorf("You dont own the Domain, meanie")
		}
	}
	if posterr != nil {
		return nil, posterr
	}
	//log.Print(t)
	err := PostTag(t)
	if err == nil {
		return t.Hkid, nil
	}
	return nil, err
}
Beispiel #2
0
func commitHelper(h objects.HKID, path string, postBytes objects.Byteser, postType string) (objects.HID, error) {
	c, geterr := GetCommit(h)
	posterr := error(nil)
	if geterr == nil {
		//A existing vertion was found
		nextTypeString := "list"
		nextHash := c.ListHash
		nextPath := path
		var hashOfPosted objects.HID
		hashOfPosted, posterr = post(nextHash, nextPath,
			nextTypeString, postBytes, postType)
		if posterr != nil {
			return nil, posterr
		}
		c = c.Update(hashOfPosted.Bytes())
	} else {
		//A existing vertion was NOT found
		_, err := GetPrivateKeyForHkid(h)
		if err == nil {
			nextHash := objects.HID(nil)
			nextPath := path
			nextTypeString := "list"
			var hashOfPosted objects.HID
			hashOfPosted, posterr = post(nextHash, nextPath,
				nextTypeString, postBytes, postType)
			c = objects.NewCommit(hashOfPosted.Bytes(), h)
		} else {
			log.Printf("You don't seem to own this repo\n\th=%v\n\terr=%v\n", h, err)
			return objects.HKID{}, fmt.Errorf("You don't seem to own this repo")
		}
	}
	if posterr != nil {
		return nil, posterr
	}
	//log.Print(c)
	err := PostCommit(c)
	if err == nil {
		return c.Hkid, nil
	}
	return nil, err
}
Beispiel #3
0
func post(h objects.HID, path string, nextPathSegmentType string,
	postBytes objects.Byteser, postType string) (hid objects.HID, err error) {
	//log.Printf(
	//	"\n\th: %v\n\tpath: %v\n\tnext_path_segment_type: %v\n\tpost_bytes: %s\n\tpost_type: %v\n",
	//	h, path, next_path_segment_type, post_bytes, post_type)
	if path == "" {
		//log.Printf("post_type: %s", post_type)
		err := PostBlob(postBytes.(objects.Blob))
		return objects.HCID(postBytes.(objects.Blob).Hash()), err
	}

	nameSegments := strings.SplitN(path, "/", 2)
	nextPathSegment := nameSegments[0]
	restOfPath := ""
	if len(nameSegments) > 1 {
		restOfPath = nameSegments[1]
	}
	switch nextPathSegmentType {
	default:
		return nil, fmt.Errorf(fmt.Sprintf("Invalid type %T", nextPathSegmentType))
	case "blob":
		return nil, fmt.Errorf(fmt.Sprintf("only \"\" path can be blob"))
	case "list":
		postedListHash, err := listHelper(h, nextPathSegment, restOfPath,
			postBytes, postType)
		return postedListHash, err
	case "commit":
		postedCommitHash, err := commitHelper(h.Bytes(), path, postBytes,
			postType)
		return postedCommitHash, err
	case "tag":
		postedTagHash, err := tagHelper(h, nextPathSegment, restOfPath,
			postBytes, postType)
		return postedTagHash, err
	}
}
Beispiel #4
0
func get(objecthash objects.HID, path string, typeString string) (b objects.Blob, err error) {
	//typeString := "commit"
	err = nil
	nameSegments := []string{"", path}
	for {
		if len(nameSegments) > 1 {
			nameSegments = strings.SplitN(nameSegments[1], "/", 2)
		} else {
			nameSegments = []string{""}
		}
		//log.Printf("\n\tPath: %s\n\tType: %v\n\tobjecthash: %v\n",
		//	nameSegments, typeString, objecthash)
		switch typeString {
		case "blob":
			b, err = GetBlob(objecthash.Bytes())
			if err != nil {
				log.Printf("\n\t%v\n", err)
			}
			return b, err
		case "list":
			var l objects.List
			l, err = GetList(objecthash.Bytes())
			if err != nil {
				log.Printf("\n\t%v\n", err)
			}
			typeString, objecthash = l.HashForNamesegment(nameSegments[0])
			if objecthash == nil && nameSegments[0] != "" {
				err = fmt.Errorf("Blob not found")
			}
			b = l.Bytes()
		case "tag":
			var t objects.Tag
			//if nameSegments[0] == "" {
			//	log.Printf("\n\tNo Path\n")
			//}
			t, err = GetTag(objecthash.(objects.HKID), nameSegments[0])
			if err != nil {
				//log.Printf("\n\t%v\n", err)
				return nil, err
			}
			if !t.Verify() {
				return nil, fmt.Errorf("Tag Verify Failed")
			}
			typeString = t.TypeString
			objecthash = t.HashBytes
			b = t.Bytes()
		case "commit":
			var c objects.Commit
			c, err = GetCommit(objecthash.Bytes())
			if err != nil {
				log.Printf("\n\t%v\n", err)
			}
			if !c.Verify() {
				return nil, fmt.Errorf("Commit Verify Failed")
			}
			var l objects.List
			l, err = GetList(c.ListHash)
			if err != nil {
				log.Printf("\n\t%v\n", err)
			}
			typeString, objecthash = l.HashForNamesegment(nameSegments[0])
			if objecthash == nil && nameSegments[0] != "" {
				err = fmt.Errorf("Blob not found")
			}
			//if err != nil {
			//	log.Printf("%v\n", err)
			//}
			b = l.Bytes()
		default:
			log.Printf("\n\t%v\n", err)
			panic(err)
		}
		//if len(nameSegments) == 1 && typeString != "blob" {
		if objecthash == nil {
			return b, err
		}
	}
}