navid/veinsforge / commit

7946f29afb88

Navid M <navidmomtahen@gmail.com> · 2026-11-27

parents: 0a20713c8f27

Browse files at this commit · Download source

all: various bookmark related fixes
--- a/internal/vn/browse.go+++ b/internal/vn/browse.go@@ -28,18 +28,50 @@ 	return r.ensureBrowseLocked(ctx, repoID, central) } -func (r *Runner) ensureBrowseLocked(ctx context.Context, repoID int64, central string) (string, error) {-	path := r.BrowsePath(repoID)+func (r *Runner) initBrowseLocked(ctx context.Context, path, central string) error { 	if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {-		return "", err+		return err 	} 	if !r.RepoExists(path) {-		if _, err := r.runOK(ctx, filepath.Dir(path), nil, "init", path); err != nil {-			return "", err+		if _, err := os.Stat(path); err == nil {+			if err := os.RemoveAll(path); err != nil {+				return err+			}+		}+		args := []string{"clone", "file://" + filepath.Clean(central), path}+		if r.KeyName != "" {+			args = append(args, "-key:"+r.KeyName)+		}+		if _, err := r.runOK(ctx, filepath.Dir(path), nil, args...); err != nil {+			return err 		} 		_ = os.Chmod(path, 0o700) 	}-	if err := r.ensureRemoteConfig(path, central); err != nil {+	return r.ensureRemoteConfig(path, central)+}++func (r *Runner) fetchBrowseLocked(ctx context.Context, path string) (string, error) {+	_, errOut, err := r.runLong(ctx, path, nil, "fetch", "origin")+	return string(errOut), err+}++func (r *Runner) rebuildBrowseLocked(ctx context.Context, repoID int64, path, central string) error {+	if err := os.RemoveAll(path); err != nil {+		return err+	}+	if err := r.initBrowseLocked(ctx, path, central); err != nil {+		return err+	}+	r.mu.Lock()+	delete(r.currentRev, repoID)+	delete(r.fetchHead, repoID)+	r.mu.Unlock()+	return nil+}++func (r *Runner) ensureBrowseLocked(ctx context.Context, repoID int64, central string) (string, error) {+	path := r.BrowsePath(repoID)+	if err := r.initBrowseLocked(ctx, path, central); err != nil { 		return "", err 	} 	head := currentHead(central)@@ -53,16 +85,31 @@ 	if head == seen && time.Since(last) < 30*time.Second { 		return path, nil 	}-	if _, errOut, err := r.runLong(ctx, path, nil, "fetch", "origin"); err != nil {+	errOut, err := r.fetchBrowseLocked(ctx, path)+	if err != nil { 		if r.Logger != nil {-			r.Logger.Printf("vn fetch for repo %d failed: %v: %s", repoID, err, truncate(string(errOut), 1024))+			r.Logger.Printf("vn fetch for repo %d failed: %v: %s", repoID, err, truncate(errOut, 1024)) 		}-		// Back off for a short while instead of re-running a failing fetch on-		// every page request.+		// A cache left behind by an interrupted fetch or with stale remote+		// configuration cannot recover by retrying alone. Rebuild it once.+		if rebuildErr := r.rebuildBrowseLocked(ctx, repoID, path, central); rebuildErr != nil {+			if r.Logger != nil {+				r.Logger.Printf("rebuild browse cache for repo %d failed: %v", repoID, rebuildErr)+			}+		} else {+			errOut, err = r.fetchBrowseLocked(ctx, path)+			if err != nil && r.Logger != nil {+				r.Logger.Printf("vn fetch for rebuilt repo %d failed: %v: %s", repoID, err, truncate(errOut, 1024))+			}+		} 	} 	r.mu.Lock() 	r.fetchAt[repoID] = time.Now()-	r.fetchHead[repoID] = head+	if err == nil {+		r.fetchHead[repoID] = head+	} else {+		delete(r.fetchHead, repoID)+	} 	r.mu.Unlock() 	return path, nil }@@ -149,10 +196,10 @@ 		return nil, nil 	} 	out, _, err := r.run(ctx, path, nil, "bmk", "ls", "-no-col-titles")+	if strings.Contains(strings.ToLower(string(out)), "no bookmarks") {+		return nil, nil+	} 	if err != nil {-		if strings.Contains(strings.ToLower(string(out)), "no bookmarks") {-			return nil, nil-		} 		return nil, err 	} 	return ParseBranches(string(out)), nil--- a/internal/vn/parse.go+++ b/internal/vn/parse.go@@ -124,6 +124,18 @@ 		d.Message = strings.TrimSpace(strings.Join(lines[messageStart:], "\n")) 	} 	return d+}++func isHexHash(text string) bool {+	if len(text) < 7 {+		return false+	}+	for _, c := range text {+		if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {+			return false+		}+	}+	return true }  // ParseBranches parses `vn bmk ls -no-col-titles`.@@ -131,7 +143,7 @@ 	var branches []Branch 	for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") { 		fields := strings.Fields(line)-		if len(fields) < 3 {+		if len(fields) < 3 || !isHexHash(fields[1]) { 			continue 		} 		b := Branch{Name: fields[0], Hash: fields[1], ShortHash: fields[1], ChangeID: fields[2]}--- a/internal/vn/parse_test.go+++ b/internal/vn/parse_test.go@@ -69,6 +69,9 @@ 	branches := ParseBranches("feat      4a79ee834a67  276xmffe  \nmain      b4f9a770b344  fa3elkzy  @\n") 	if len(branches) != 2 || branches[0].Name != "feat" || !branches[1].Current { 		t.Fatalf("branches = %+v", branches)+	}+	if got := ParseBranches("info: no bookmarks\n"); len(got) != 0 {+		t.Fatalf("info line parsed as branches: %+v", got) 	} 	conflicts := ParseConflicts("info: no conflicts\n") 	if len(conflicts) != 0 {