manus-tracker

tracker for scientific manuscripts
git clone git://src.adamsgaard.dk/manus-tracker # fast
git clone https://src.adamsgaard.dk/manus-tracker.git # slow
Log | Files | Refs | README Back to index

revise.go (5491B)


      1 package main
      2 
      3 import (
      4 	"flag"
      5 	"fmt"
      6 	"os"
      7 	"os/exec"
      8 	"path/filepath"
      9 	"regexp"
     10 	"slices"
     11 	"strings"
     12 
     13 	"manus/store"
     14 )
     15 
     16 type reviewItem struct {
     17 	Label string
     18 	N     int
     19 	Text  string
     20 }
     21 
     22 var reviewHeader = regexp.MustCompile(`(?i)^\s*(reviewer|referee|editor|associate editor)\s*#?\s*(\d*)\s*:?\s*(.*)$`)
     23 
     24 // parseReview splits reviewer comments into items: blank-line separated
     25 // blocks, with "Reviewer 2" / "Editor" lines starting a new reviewer.
     26 func parseReview(text string) []reviewItem {
     27 	var items []reviewItem
     28 	label := "r"
     29 	n := map[string]int{}
     30 	for _, block := range regexp.MustCompile(`\n\s*\n`).Split(strings.ReplaceAll(text, "\r\n", "\n"), -1) {
     31 		block = strings.TrimSpace(block)
     32 		if block == "" {
     33 			continue
     34 		}
     35 		first, rest, _ := strings.Cut(block, "\n")
     36 		if m := reviewHeader.FindStringSubmatch(first); m != nil && len(first) < 60 {
     37 			label = "r" + m[2]
     38 			if strings.Contains(strings.ToLower(m[1]), "editor") {
     39 				label = "editor"
     40 			}
     41 			block = strings.TrimSpace(strings.TrimSpace(m[3]) + "\n" + rest)
     42 			if block == "" {
     43 				continue
     44 			}
     45 		}
     46 		n[label]++
     47 		items = append(items, reviewItem{label, n[label], block})
     48 	}
     49 	return items
     50 }
     51 
     52 func short(s string, n int) string {
     53 	s = oneLine(s)
     54 	r := []rune(s)
     55 	if len(r) <= n {
     56 		return s
     57 	}
     58 	return string(r[:n-1]) + "…"
     59 }
     60 
     61 var revSuffix = regexp.MustCompile(`-r\d+$`)
     62 
     63 func gitOut(dir string, args ...string) (string, error) {
     64 	out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput()
     65 	if err != nil {
     66 		return "", fmt.Errorf("git %s: %v: %s", strings.Join(args, " "), err, strings.TrimSpace(string(out)))
     67 	}
     68 	return strings.TrimSpace(string(out)), nil
     69 }
     70 
     71 func (a *app) revise(args []string) error {
     72 	fs := flag.NewFlagSet("revise", flag.ContinueOnError)
     73 	mode := fs.String("mode", "auto", "auto, worktree, copy or none")
     74 	comments := fs.String("c", "", "reviewer comments file, one task per paragraph")
     75 	notag := fs.Bool("notag", false, "do not tag the submitted state")
     76 	pos, err := parse(fs, args)
     77 	if err != nil {
     78 		return err
     79 	}
     80 	var review []reviewItem
     81 	if *comments != "" {
     82 		b, err := os.ReadFile(*comments)
     83 		if err != nil {
     84 			return err
     85 		}
     86 		review = parseReview(string(b))
     87 	}
     88 	return a.edit(func(s *store.Store) (string, error) {
     89 		p, err := a.pick(s, pos)
     90 		if err != nil {
     91 			return "", err
     92 		}
     93 		if c := s.Children(p.ID); len(c) > 0 {
     94 			return "", fmt.Errorf("%s already has revision %s", p.ID, c[0].ID)
     95 		}
     96 		rev := p.Revision() + 1
     97 		nid := fmt.Sprintf("%s-r%d", revSuffix.ReplaceAllString(p.ID, ""), rev)
     98 		repo := s.RepoPath(p)
     99 		switch *mode {
    100 		case "auto":
    101 			*mode = "copy"
    102 			if repo != "" {
    103 				*mode = "worktree"
    104 			}
    105 		case "worktree":
    106 			if repo == "" {
    107 				return "", fmt.Errorf("%s has no git repo; use -mode copy", p.ID)
    108 			}
    109 		case "copy", "none":
    110 		default:
    111 			return "", fmt.Errorf("unknown mode %q", *mode)
    112 		}
    113 
    114 		f := map[string]string{}
    115 		for _, k := range []string{"title", "dir", "src", "repo", "main", "kind", "role", "lead", "coauthors", "journal"} {
    116 			f[k] = p.Get(k)
    117 		}
    118 		f["parent"], f["revision"], f["stage"] = p.ID, fmt.Sprint(rev), "revision"
    119 
    120 		dest := filepath.Join(a.root, nid)
    121 		if _, err := os.Stat(dest); err == nil && *mode != "none" {
    122 			return "", fmt.Errorf("%s exists", dest)
    123 		}
    124 		start := "HEAD"
    125 		if repo != "" && !*notag {
    126 			tag := fmt.Sprintf("submitted-r%d", rev-1)
    127 			if _, err := gitOut(repo, "tag", "-a", tag, "-m", "state of "+p.ID+" before revision "+fmt.Sprint(rev)); err != nil {
    128 				return "", err
    129 			}
    130 			f["tag"], start = tag, tag
    131 			a.printf("tagged %s in %s\n", tag, repo)
    132 		}
    133 		switch *mode {
    134 		case "worktree":
    135 			if out, err := gitOut(repo, "status", "--porcelain"); err == nil && out != "" {
    136 				a.printf("warning: %s has uncommitted changes; they are not in the worktree\n", repo)
    137 			}
    138 			if _, err := gitOut(repo, "worktree", "add", "-b", "revision-r"+fmt.Sprint(rev), dest, start); err != nil {
    139 				return "", err
    140 			}
    141 			src, err := filepath.Rel(repo, s.SrcPath(p))
    142 			if err != nil {
    143 				return "", err
    144 			}
    145 			f["dir"], f["src"], f["repo"] = nid, src, "."
    146 			a.printf("worktree %s\n", dest)
    147 		case "copy":
    148 			if out, err := exec.Command("cp", "-Rp", s.Path(p), dest).CombinedOutput(); err != nil {
    149 				return "", fmt.Errorf("cp: %v: %s", err, out)
    150 			}
    151 			f["dir"] = nid
    152 			a.printf("copied to %s\n", dest)
    153 		}
    154 
    155 		n, err := s.Create(nid, f)
    156 		if err != nil {
    157 			return "", err
    158 		}
    159 		carried := map[int]int{}
    160 		for _, t := range p.Tasks {
    161 			if t.Status == "done" {
    162 				continue
    163 			}
    164 			nt, err := s.AddTask(n, t.Title)
    165 			if err != nil {
    166 				return "", err
    167 			}
    168 			nt.Status, nt.Due, nt.Source, nt.Comments = t.Status, t.Due, t.Source, slices.Clone(t.Comments)
    169 			carried[t.ID] = nt.ID
    170 		}
    171 		for old, nw := range carried {
    172 			for _, r := range p.Task(old).Rels {
    173 				if to, ok := carried[r.To.N]; ok && r.To.MS == p.ID {
    174 					r.To = store.Ref{MS: nid, N: to}
    175 				}
    176 				n.Task(nw).Rels = append(n.Task(nw).Rels, r)
    177 			}
    178 			ot := p.Task(old)
    179 			store.SetStatus(ot, "done")
    180 			s.Comment(p, ot, fmt.Sprintf("carried over to %s#%d", nid, nw))
    181 		}
    182 		for _, it := range review {
    183 			t, err := s.AddTask(n, fmt.Sprintf("%s.%d: %s", it.Label, it.N, short(it.Text, 70)))
    184 			if err != nil {
    185 				return "", err
    186 			}
    187 			t.Source = fmt.Sprintf("%s#%d", it.Label, it.N)
    188 			s.Comment(n, t, it.Text)
    189 		}
    190 		s.Touch(p)
    191 		a.printf("%s: revision %d of %s, %d tasks carried over, %d reviewer comments\n", nid, rev, p.ID, len(carried), len(review))
    192 		return fmt.Sprintf("revise %s as %s", p.ID, nid), nil
    193 	})
    194 }