|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/cli/go-gh/v2/pkg/browser" |
| 12 | + "github.com/github/gh-stack/internal/config" |
| 13 | + "github.com/github/gh-stack/internal/git" |
| 14 | + "github.com/github/gh-stack/internal/stack" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +type viewOptions struct { |
| 19 | + short bool |
| 20 | + web bool |
| 21 | +} |
| 22 | + |
| 23 | +func ViewCmd(cfg *config.Config) *cobra.Command { |
| 24 | + opts := &viewOptions{} |
| 25 | + |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "view", |
| 28 | + Short: "View the current stack", |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + return runView(cfg, opts) |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + cmd.Flags().BoolVarP(&opts.short, "short", "s", false, "Show compact output") |
| 35 | + cmd.Flags().BoolVarP(&opts.web, "web", "w", false, "Open PRs in the browser") |
| 36 | + |
| 37 | + return cmd |
| 38 | +} |
| 39 | + |
| 40 | +func runView(cfg *config.Config, opts *viewOptions) error { |
| 41 | + gitDir, err := git.GitDir() |
| 42 | + if err != nil { |
| 43 | + cfg.Errorf("not a git repository") |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + sf, err := stack.Load(gitDir) |
| 48 | + if err != nil { |
| 49 | + cfg.Errorf("failed to load stack state: %s", err) |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + currentBranch, err := git.CurrentBranch() |
| 54 | + if err != nil { |
| 55 | + cfg.Errorf("failed to get current branch: %s", err) |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + s := sf.FindStackForBranch(currentBranch) |
| 60 | + if s == nil { |
| 61 | + cfg.Errorf("current branch %q is not part of a stack", currentBranch) |
| 62 | + cfg.Printf("Checkout an existing stack using %s or create a new stack using %s", cfg.ColorCyan("gh stack checkout"), cfg.ColorCyan("gh stack init")) |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + if opts.web { |
| 67 | + return viewWeb(cfg, s) |
| 68 | + } |
| 69 | + |
| 70 | + if opts.short { |
| 71 | + return viewShort(cfg, s, currentBranch) |
| 72 | + } |
| 73 | + |
| 74 | + return viewFull(cfg, s, currentBranch) |
| 75 | +} |
| 76 | + |
| 77 | +func viewShort(cfg *config.Config, s *stack.Stack, currentBranch string) error { |
| 78 | + for i := len(s.Branches) - 1; i >= 0; i-- { |
| 79 | + b := s.Branches[i] |
| 80 | + if b.Branch == currentBranch { |
| 81 | + cfg.Outf("● %s %s\n", cfg.ColorBold(b.Branch), cfg.ColorCyan("(current)")) |
| 82 | + } else { |
| 83 | + cfg.Outf("○ %s\n", b.Branch) |
| 84 | + } |
| 85 | + } |
| 86 | + cfg.Outf("└ %s\n", s.Trunk.Branch) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func viewFull(cfg *config.Config, s *stack.Stack, currentBranch string) error { |
| 91 | + client, clientErr := cfg.GitHubClient() |
| 92 | + |
| 93 | + var repoOwner, repoName string |
| 94 | + repo, repoErr := cfg.Repo() |
| 95 | + if repoErr == nil { |
| 96 | + repoOwner = repo.Owner |
| 97 | + repoName = repo.Name |
| 98 | + } |
| 99 | + |
| 100 | + var buf bytes.Buffer |
| 101 | + |
| 102 | + for i := len(s.Branches) - 1; i >= 0; i-- { |
| 103 | + b := s.Branches[i] |
| 104 | + isCurrent := b.Branch == currentBranch |
| 105 | + |
| 106 | + bullet := "○" |
| 107 | + if isCurrent { |
| 108 | + bullet = "●" |
| 109 | + } |
| 110 | + |
| 111 | + prInfo := "" |
| 112 | + if clientErr == nil && repoErr == nil { |
| 113 | + pr, err := client.FindPRForBranch(b.Branch) |
| 114 | + if err == nil && pr != nil { |
| 115 | + prInfo = fmt.Sprintf(" https://github.com/%s/%s/pull/%d", repoOwner, repoName, pr.Number) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + branchName := cfg.ColorMagenta(b.Branch) |
| 120 | + if isCurrent { |
| 121 | + branchName = cfg.ColorCyan(b.Branch + " (current)") |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Fprintf(&buf, "%s %s%s\n", bullet, branchName, prInfo) |
| 125 | + |
| 126 | + commits, err := git.Log(b.Branch, 1) |
| 127 | + if err == nil && len(commits) > 0 { |
| 128 | + c := commits[0] |
| 129 | + short := c.SHA |
| 130 | + if len(short) > 7 { |
| 131 | + short = short[:7] |
| 132 | + } |
| 133 | + fmt.Fprintf(&buf, "│ %s %s\n", short, cfg.ColorGray("· "+timeAgo(c.Time))) |
| 134 | + fmt.Fprintf(&buf, "│ %s\n", cfg.ColorGray(c.Subject)) |
| 135 | + } |
| 136 | + |
| 137 | + fmt.Fprintf(&buf, "│\n") |
| 138 | + } |
| 139 | + |
| 140 | + fmt.Fprintf(&buf, "└ %s\n", s.Trunk.Branch) |
| 141 | + |
| 142 | + return runPager(cfg, buf.String()) |
| 143 | +} |
| 144 | + |
| 145 | +func runPager(cfg *config.Config, content string) error { |
| 146 | + if !cfg.IsInteractive() { |
| 147 | + _, err := fmt.Fprint(cfg.Out, content) |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + pagerCmd := os.Getenv("GIT_PAGER") |
| 152 | + if pagerCmd == "" { |
| 153 | + pagerCmd = os.Getenv("PAGER") |
| 154 | + } |
| 155 | + if pagerCmd == "" { |
| 156 | + pagerCmd = "less" |
| 157 | + } |
| 158 | + |
| 159 | + args := strings.Fields(pagerCmd) |
| 160 | + if args[0] == "less" { |
| 161 | + hasR := false |
| 162 | + for _, a := range args[1:] { |
| 163 | + if strings.Contains(a, "R") { |
| 164 | + hasR = true |
| 165 | + break |
| 166 | + } |
| 167 | + } |
| 168 | + if !hasR { |
| 169 | + args = append(args, "-R") |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + cmd := exec.Command(args[0], args[1:]...) |
| 174 | + cmd.Stdout = cfg.Out |
| 175 | + cmd.Stderr = cfg.Err |
| 176 | + cmd.Stdin = strings.NewReader(content) |
| 177 | + |
| 178 | + return cmd.Run() |
| 179 | +} |
| 180 | + |
| 181 | +func timeAgo(t time.Time) string { |
| 182 | + d := time.Since(t) |
| 183 | + switch { |
| 184 | + case d < time.Minute: |
| 185 | + secs := int(d.Seconds()) |
| 186 | + if secs == 1 { |
| 187 | + return "1 second ago" |
| 188 | + } |
| 189 | + return fmt.Sprintf("%d seconds ago", secs) |
| 190 | + case d < time.Hour: |
| 191 | + mins := int(d.Minutes()) |
| 192 | + if mins == 1 { |
| 193 | + return "1 minute ago" |
| 194 | + } |
| 195 | + return fmt.Sprintf("%d minutes ago", mins) |
| 196 | + case d < 24*time.Hour: |
| 197 | + hours := int(d.Hours()) |
| 198 | + if hours == 1 { |
| 199 | + return "1 hour ago" |
| 200 | + } |
| 201 | + return fmt.Sprintf("%d hours ago", hours) |
| 202 | + case d < 30*24*time.Hour: |
| 203 | + days := int(d.Hours() / 24) |
| 204 | + if days == 1 { |
| 205 | + return "1 day ago" |
| 206 | + } |
| 207 | + return fmt.Sprintf("%d days ago", days) |
| 208 | + default: |
| 209 | + months := int(d.Hours() / 24 / 30) |
| 210 | + if months <= 1 { |
| 211 | + return "1 month ago" |
| 212 | + } |
| 213 | + return fmt.Sprintf("%d months ago", months) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +func viewWeb(cfg *config.Config, s *stack.Stack) error { |
| 218 | + client, err := cfg.GitHubClient() |
| 219 | + if err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + |
| 223 | + repo, err := cfg.Repo() |
| 224 | + if err != nil { |
| 225 | + return err |
| 226 | + } |
| 227 | + |
| 228 | + b := browser.New("", cfg.Out, cfg.Err) |
| 229 | + |
| 230 | + opened := 0 |
| 231 | + for _, br := range s.Branches { |
| 232 | + pr, err := client.FindPRForBranch(br.Branch) |
| 233 | + if err != nil || pr == nil { |
| 234 | + continue |
| 235 | + } |
| 236 | + url := fmt.Sprintf("https://github.com/%s/%s/pull/%d", repo.Owner, repo.Name, pr.Number) |
| 237 | + if err := b.Browse(url); err != nil { |
| 238 | + cfg.Warningf("failed to open %s: %v\n", url, err) |
| 239 | + } else { |
| 240 | + opened++ |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + if opened == 0 { |
| 245 | + cfg.Printf("No PRs found to open in browser.\n") |
| 246 | + } else { |
| 247 | + cfg.Successf("Opened %d PRs in browser\n", opened) |
| 248 | + } |
| 249 | + |
| 250 | + return nil |
| 251 | +} |
0 commit comments