Add comments to describe certain functions

Comments have been added to a couple of functions here and there
where I feel warrent some explanation. Hopefully this makes it a little
easier for people to contribute.

I commented on a couple of functions but my main focus is:
	DepTree
	DepCatagories
	StringSet
	Arguments

These are parts that have been mostly written by me and might seem
confusing without taking a while to study the code. Especially the first
two mentioned. They're a little complex, I'm not sure if they need to be
This is just how I came up with them. Hopefully helping other people
understand them will let them come up with improvments I did not see.
I'm not the best at explaining things but I did try my best here.
This commit is contained in:
morganamilo 2018-03-03 01:11:16 +00:00
parent 4738c96809
commit cfd391b423
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
4 changed files with 80 additions and 0 deletions

View file

@ -43,12 +43,44 @@ func makeDependCatagories() *depCatagories {
return &dc
}
//cut the version requirement from a dependency leaving just the name
func getNameFromDep(dep string) string {
return strings.FieldsFunc(dep, func(c rune) bool {
return c == '>' || c == '<' || c == '='
})[0]
}
//step two of dependency reloving. We already have all the information on the
//packages we need, now it's just about ordering them correctly.
//pkgs is a list of targets, the packages we want to install, dependencies not
//included.
//For each package we want we iterate down the tree until we hit the bottom.
//This is done recursivley for each branch.
//The start of the tree is defined as the package we want.
//When we hit the bottom of the branch we know thats the first package
//we need to install so we add it to the start of the to install
//list (dc.Aur and dc.Repo).
//We work our way up until there is another branch to go down and do it all
//again.
//
//Here is a visual example
//
// a
// / \
// b c
// / \
// d e
//
//we see a and it needs b and c
//we see b and it needs d and e
//we see d it needs nothing so we add d to our list and move up
//we see e it needs nothing so we add e to our list and move up
//we see c it needs nothign so we add c to our list and move up
//
//The final install order would come out as debca
//
//Theres a little more to this, handling provide, multiple packages wanting the
//same dependencies and so on this is just the basic premise.
func getDepCatagories(pkgs []string, dt *depTree) (*depCatagories, error) {
dc := makeDependCatagories()
seen := make(stringSet)
@ -174,6 +206,36 @@ func depCatagoriesRecursive(_pkg *rpc.Pkg, dc *depCatagories, dt *depTree, isMak
}
}
//This is step one for dependency resolving. pkgs is a slice of the packages you
//want to resolve the dependencioes for. They can be a mix of aur and repo
//dependencies. All unmet dependencies will be resolved.
//For Aur dependencies depends, makedepends and checkdepends are resolved but
//for repo packages only depends are resolved as they are pre buiild.
//The return will be split into three catagories. Repo, Aur and Missing.
//The return is in no way ordered this step is is just aimed at gathering the
//packaghes we need.
//This has been designed to make the leat amount or rpc requests as possible.
//Web requests are probably going to be the bottleneck here so minimizing them
//provides a nice spead boost.
//
//Here is a visual expample of the request system.
//Remember only unsatisfied packages are requested, if a package is already
//installed we dont bother.
//
// a
// / \
// b c
// / \
// d e
//
//We see a so we send a request for a
//we see wants b and c so we send a request for b and c
//we see d and e so we send a request for d and e
//
//Thats 5 packages in 3 requests.The amount of requests needed should always be
//the same as the height of the tree.
//The example does not really do this justice, In the real world where packages
//have 10+ dependencies each this is a very nice optimization.
func getDepTree(pkgs []string) (*depTree, error) {
dt := makeDepTree()

View file

@ -7,6 +7,11 @@ import (
"strings"
)
//a basic set implementatrion for strings. this is used a lot so it deserves
//its own type. Other types of sets are used throughout the code but tont have
//their own typedef.
//stringsets and <type>sets should be used throught the code when applicable,
//they are a lot more flexable than slices and provide easy lookup,
type stringSet map[string]struct{}
func (set stringSet) set(v string) {
@ -32,6 +37,8 @@ func (set stringSet) toSlice() []string {
return slice
}
//Parses command line arguments in a way we can interact with progamaticly but
//also in a way that can easily be passed to pacman later on.
type arguments struct {
op string
options map[string]string

View file

@ -100,6 +100,9 @@ func (s repoQuery) printSearch() {
}
}
//pretty print a set of packages from the the same package base
//packages foo and bar from a pkgbase named base would print like so
//base (foo bar)
func formatPkgbase(pkg *rpc.Pkg, bases map[string][]*rpc.Pkg) string {
str := pkg.PackageBase
if len(bases[pkg.PackageBase]) > 1 || pkg.PackageBase != pkg.Name {
@ -305,6 +308,7 @@ func printUpdateList() error {
return nil
}
//formats a unix timestamp to yyy/m/d
func formatTime(i int) string {
t := time.Unix(int64(i), 0)
return fmt.Sprintf("%d/%d/%d", t.Year(), int(t.Month()), t.Day())
@ -366,6 +370,8 @@ func bold(in string) string {
return in
}
//colours text using a hashing algorithm the same text will always produce the
//same colour while different text will produce a different colour
func colourHash(name string) (output string) {
if alpmConf.Options&alpm.ConfColor == 0 {
return name

View file

@ -317,6 +317,11 @@ func min(a, b int) int {
return a
}
//Queriers the aur for information about specified packages
//all packages should be queried in a single rpc request except when the number
//of packages exceeds the number set in config.RequestSplitN
//if the number does exceed config.RequestSplitN multiple rpc requests will be
//preformed concurrently
func aurInfo(names []string) ([]rpc.Pkg, error) {
info := make([]rpc.Pkg, 0, len(names))
seen := make(map[string]int)