gtx


Branch: develop

Author
thewhodidthis <thewhodidthis@fastmail.com>
Date
Nov. 24 '22 18:52:23
Commit
2960425dc98348ff06fccdd4a3933246fb6c31b0
Parent
be497a7c250b904b8af78eb2bdd94ab7322041ec
Changes
diff --git a/main.go b/main.go
index 7d414b7..6292a63 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,8 @@
 package main
 
 import (
+	"bufio"
+	"bytes"
 	_ "embed"
 	"encoding/json"
 	"flag"
@@ -106,35 +108,12 @@ func (r *repo) save(target string) error {
 	return err
 }
 
-func (r *repo) listBranches(bf manyflag) ([]branch, error) {
-	cmd := exec.Command("git", "branch", "-a")
-	cmd.Dir = filepath.Join(r.path, "repo")
-
-	out, err := cmd.Output()
-
-	if err != nil {
-		return nil, err
-	}
-
-	// TODO: Filter to match options.
-	// TODO: Check if `bf` empty, in which case return all.
-	var branches []branch
-
-	for _, b := range bf {
-		fmt.Printf("want: %s\n", b)
-	}
-
-	fmt.Printf("have: %s", out)
-
-	return branches, nil
-}
-
 // https://stackoverflow.com/questions/28322997/how-to-get-a-list-of-values-into-a-flag-in-golang/
 type manyflag []string
 
 func (f *manyflag) Set(value string) error {
 	// Make sure there are no duplicates.
-	if !includes(*f, value) {
+	if !contains(*f, value) {
 		*f = append(*f, value)
 	}
 
@@ -303,7 +282,7 @@ func main() {
 		log.Fatalf("unable to set up repo: %v", err)
 	}
 
-	branches, err := repo.listBranches(opt.Branches)
+	branches, err := findBranches(repo, opt.Branches)
 
 	if err != nil {
 		log.Fatalf("unable to list branches: %v", err)
@@ -320,7 +299,7 @@ func main() {
 
 	rt := template.Must(template.New("repo").Parse(rTmpl))
 	rd := struct {
-		Branches []branch
+		Branches []string
 		Link     string
 		Title    string
 	}{
@@ -335,38 +314,7 @@ func main() {
 }
 
 // TODO: implement!
-func cleanUpBranches(branches manyflag) []string {
-	/*
-	   if test x"$BRANCHES" = x
-	   then
-	     # Strip the start of lines of the form 'origin/HEAD -> origin/master'
-	     BRANCHES=$(git branch --no-color -r \
-	                  | sed 's#.*->##; s#^ *origin/##;')
-	   fi
-
-	   first=""
-	   # Ignore 'origin/HEAD -> origin/master'
-	   for branch in ${BRANCHES:-$(git branch --no-color -r \
-	                                 | sed 's#.*->.*##;
-	                                        s#^ *origin/##;
-	                                        s#^ *HEAD *$##;')}
-	   do
-	     first="$branch"
-	     break
-	   done
-
-	   # Due to branch aliases (a la origin/HEAD), a branch might be listed
-	   # multiple times.  Eliminate this possibility.
-	   BRANCHES=$(for branch in $BRANCHES
-	     do
-	       echo "$branch"
-	     done | sort | uniq)
-	*/
-	return []string{}
-}
-
-// TODO: implement!
-func fetchBranches(branches []string) {
+func updateBranches(r *repo, branches []string) int {
 	/*
 	   	   for branch in $BRANCHES
 	   	   do
@@ -394,6 +342,50 @@ func fetchBranches(branches []string) {
 	   	     let ++bcount
 	   	   done
 	*/
+	return 0
+}
+
+func findBranches(r *repo, bf manyflag) ([]string, error) {
+	cmd := exec.Command("git", "branch", "-a")
+	cmd.Dir = filepath.Join(r.path, "repo")
+
+	out, err := cmd.Output()
+
+	if err != nil {
+		return nil, err
+	}
+
+	var result []string
+	var branch = make(map[string]bool)
+
+	scanner := bufio.NewScanner(bytes.NewReader(out))
+
+	for scanner.Scan() {
+		t := strings.TrimSpace(scanner.Text())
+		_, f := filepath.Split(t)
+
+		branch[f] = !strings.Contains(f, "HEAD")
+	}
+
+	if err := scanner.Err(); err != nil {
+		return nil, err
+	}
+
+	// Filter to match options, but return all if no branch flags given.
+	if len(bf) > 0 {
+		for k, _ := range branch {
+			branch[k] = contains(bf, k)
+		}
+	}
+
+	// Transfer desired branch names to resulting slice.
+	for k, v := range branch {
+		if v {
+			result = append(result, k)
+		}
+	}
+
+	return result, nil
 }
 
 // TODO: implement!
@@ -708,56 +700,3 @@ func doTheRealWork() {
 	   done
 	*/
 }
-
-// TODO: implement!
-func writeIndexFooter() {
-	/*
-	   {
-	     echo "</ul>"
-	     html_footer
-	   } >> "$INDEX"
-	*/
-}
-
-// TODO: implement!
-func htmlHeader() {
-	/*
-	   html_header()
-	   {
-	     title="$1"
-	     top_level="$2"
-
-	     if test x"$PROJECT" != x -a x"$title" != x
-	     then
-	       # Title is not the empty string.  Prefix it with ": "
-	       title=": $title"
-	     fi
-
-	     echo "<html><head><title>$PROJECT$title</title></head>" \
-	       "<body>" \
-	       "<h1><a href=\"$top_level/index.html\">$PROJECT</a>$title</h1>"
-	   }
-	*/
-}
-
-func htmlFooter() {
-	/*
-	   html_footer()
-	   {
-	     echo "<hr>" \
-	       "Generated by" \
-	       "<a href=\"http://hssl.cs.jhu.edu/~neal/git2html\">git2html</a>."
-	   }
-	*/
-}
-
-// Helps decide if value contained in slice.
-func includes(s []string, n string) bool {
-	for _, v := range s {
-		if v == n {
-			return true
-		}
-	}
-
-	return false
-}