* perf(git): locate the repository in-process instead of running git rev-parse Every bd invocation resolved the process-wide git context (beads directory discovery reads it) with `git rev-parse --git-dir --git-common-dir --show-toplevel`: one git subprocess per bd call before any work. Tools that call bd hundreds of times (gascity's doctor issues ~250 bd calls per run) paid it on every call. On Linux, initGitContext now first runs discoverGitInProcess, which mirrors Git's discovery walk (physical cwd, .git as directory or gitfile, bare-dir check, filesystem-boundary stop) and produces byte-for-byte what rev-parse prints for ordinary repositories (top and subdirectories), linked worktrees (absolute or relative gitfile, symlinked targets), submodules (core.worktree pointing back at the gitfile's directory), nested repositories and no repository. Both answers feed one shared normalization (gitContextFromRevParse), so downstream values are unchanged. It declines, and git runs exactly as before, whenever it cannot prove the answer: GIT_DIR / GIT_WORK_TREE / GIT_COMMON_DIR / GIT_CEILING_DIRECTORIES / GIT_DISCOVERY_ACROSS_FILESYSTEM / GIT_OBJECT_DIRECTORY / GIT_CONFIG_PARAMETERS / GIT_CONFIG_COUNT in the environment; a symlinked .git, an invalid gitfile or git directory, a cwd inside a git directory; repository config with core.worktree (other than the submodule shape), core.bare, extensions, include directives, a format version above 1 or unmodeled syntax; any path not owned by the effective uid (safe.directory is git's to evaluate), or root under sudo. darwin and windows keep the subprocess (case folding and ownership semantics). Tests compare discoverGitFrom with real `git rev-parse` output across those layouts, pin every decline case, and check the resulting cached context equals the subprocess-derived one. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(cli): resolve the git user.name actor fallback only when needed PersistentPreRunE assigned `actor = getActorWithGit()` for every command, which, with no --actor / BEADS_ACTOR / BD_ACTOR / config actor, runs `git config user.name`: the second git subprocess of every bd call, paid by read-only commands that never consult the actor. The pre-run now calls deferActorGitFallback, which marks an empty actor as pending; currentActor (and getActorWithGit, getActor) resolve it on first use with the unchanged priority order and store it in the global and the CommandContext copy, as the eager assignment did. Command code reads the actor through currentActor() instead of the raw global (a mechanical rewrite of every read site the compiler reported; writes, the flag binding and the pre-run config binding keep the raw variable). setActor cancels a pending fallback. The telemetry span's bd.actor attribute is still set, but only for a recording span, so a noop span does not force the lookup. With the in-process repository discovery from the previous commit, a read-only `bd list` / `bd show` now starts no git process at startup, in the main checkout and in a linked worktree; a write asks git for user.name once (bd create previously asked once, now still once) and records the same value. Measured on 50 sequential `bd list` calls: 100 -> 0 git execs (embedded and proxied-server workspaces, main checkout and worktree). TestEmbeddedReadOnlyCommandsSpawnNoGit counts git execs through a PATH shim; TestDeferredActorGitFallback pins the deferral semantics. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(git): decline in-process discovery on any config line git might read differently Review follow-ups to the in-process repository discovery. - The repository config scanner was looser than git: a config starting with a UTF-8 BOM followed by `[core] worktree = /other` made git report /other while discovery returned the cwd repository, and `[ core ]` or `!!! = x` (which git refuses) were accepted. scanGitConfig (gitconfig_linux.go) now skips a leading BOM exactly as git does and accepts only a narrow grammar: blank/comment lines, `[section]` / `[section "sub"]` headers at column 0, and `key` / `key = value` lines inside a section, with values free of backslashes and unbalanced quotes. Any other line declines to git. - Walk from getcwd(2) (syscall.Getwd), as git does, instead of os.Getwd, which trusts $PWD when it names the same inode (bind-mount aliases). - Resolve relative gitfile and commondir targets without filepath.Join's lexical Clean, so "link/.." resolves physically like git's realpath. - A mount-boundary stop reports its own reason in the not-a-repository error. - Tests: every decline case (BOM + core.worktree, malformed headers and keys, keys before a section, escapes, continuations, bare repositories, a commondir inside .git, ...) now also checks that what startup derives equals git's answer; BOM and CRLF/comment configs are equivalence cases; the no-repository case must be answered in-process instead of skipping; a $PWD alias test pins the getcwd walk. - The exec-count test drops every GIT_* variable except the config-file selectors, so a runner exporting GIT_CONFIG_PARAMETERS & co. cannot make it fail spuriously. - Comment the two places that touch the raw actor before the deferral. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(backup): decide embedded auto-backup's git-remote probe without git In an embedded workspace whose backup.enabled is unset, maybeAutoBackup asks isBackupAutoEnabled -> primeHasGitRemote after every command, reads included. That built a RepoContext (`git -C .beads rev-parse --git-common-dir`) and ran `git remote`: two git processes per read even after the startup calls were gone. The throttle cannot short-circuit it: without a remote no backup ever runs, so there is no backup state to throttle on. Both probes are now answered in-process when provable, and the subprocess remains the fallback, so the decision and every warning/notice downstream of it are unchanged: - internal/beads getGitCommonDirForPath uses git.CommonDirInProcess (the discovery walk from the given directory, git's spelling). - primeHasGitRemote uses git.HasRemoteInProcess in the same directory primeGitCmd would run git in (primeGitDir, factored out). It answers only when every config file `git remote` reads -- system (GIT_CONFIG_SYSTEM, or /etc/gitconfig and <prefix>/etc/gitconfig of the git on PATH), global (GIT_CONFIG_GLOBAL, or XDG and ~/.gitconfig) and the repository's own -- is absent or within scanGitConfig's grammar and include-free, no GIT_CONFIG_PARAMETERS/COUNT/GIT_CONFIG or discovery override is set, and no legacy .git/remotes or .git/branches entries exist. A remote seen only in a system-config candidate declines. Remote names outside [A-Za-z0-9._-] decline. Tests compare HasRemoteInProcess with `git remote` (no remote, local, worktree, submodule, remote.pushDefault only, global remote, no repo) and pin the declines (global include, malformed global, command-line config); CommonDirInProcess is compared with `git -C dir rev-parse --git-common-dir`. TestEmbeddedReadOnlyCommandsSpawnNoGit now runs with backup.enabled at its default, with and without a git remote, and still expects zero git processes for list/show. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
656 lines
19 KiB
Go
656 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
var migrateIssuesCmd = &cobra.Command{
|
|
Use: "issues",
|
|
Short: "Move issues between repositories",
|
|
Long: `Move issues from one source repository to another with filtering and dependency preservation.
|
|
|
|
This command updates the source_repo field for selected issues, allowing you to:
|
|
- Move contributor planning issues to upstream repository
|
|
- Reorganize issues across multi-phase repositories
|
|
- Consolidate issues from multiple repos
|
|
|
|
Examples:
|
|
# Preview migration from planning repo to current repo
|
|
bd migrate-issues --from ~/.beads-planning --to . --dry-run
|
|
|
|
# Move all open P1 bugs
|
|
bd migrate-issues --from ~/repo1 --to ~/repo2 --priority 1 --type bug --status open
|
|
|
|
# Move specific issues with their dependencies
|
|
bd migrate-issues --from . --to ~/archive --id bd-abc --id bd-xyz --include closure
|
|
|
|
# Move issues with label filter
|
|
bd migrate-issues --from . --to ~/feature-work --label frontend --label urgent`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
evt := metrics.NewCommandEvent("migrate-issues")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
|
|
|
if !dryRun {
|
|
CheckReadonly("migrate-issues")
|
|
}
|
|
|
|
ctx := rootCtx
|
|
|
|
from, _ := cmd.Flags().GetString("from")
|
|
to, _ := cmd.Flags().GetString("to")
|
|
statusStr, _ := cmd.Flags().GetString("status")
|
|
priorityInt, _ := cmd.Flags().GetInt("priority")
|
|
typeStr, _ := cmd.Flags().GetString("type")
|
|
labels, _ := cmd.Flags().GetStringSlice("label")
|
|
ids, _ := cmd.Flags().GetStringSlice("id")
|
|
idsFile, _ := cmd.Flags().GetString("ids-file")
|
|
include, _ := cmd.Flags().GetString("include")
|
|
withinFromOnly, _ := cmd.Flags().GetBool("within-from-only")
|
|
strict, _ := cmd.Flags().GetBool("strict")
|
|
yes, _ := cmd.Flags().GetBool("yes")
|
|
|
|
if from == "" || to == "" {
|
|
return HandleErrorRespectJSON("both --from and --to flags are required")
|
|
}
|
|
|
|
if from == to {
|
|
return HandleErrorRespectJSON("--from and --to must be different repositories")
|
|
}
|
|
|
|
if idsFile != "" {
|
|
fileIDs, err := loadIDsFromFile(idsFile)
|
|
if err != nil {
|
|
return HandleErrorRespectJSON("reading IDs file: %v", err)
|
|
}
|
|
ids = append(ids, fileIDs...)
|
|
}
|
|
|
|
if err := executeMigrateIssues(ctx, migrateIssuesParams{
|
|
from: from,
|
|
to: to,
|
|
status: statusStr,
|
|
priority: priorityInt,
|
|
issueType: typeStr,
|
|
labels: labels,
|
|
ids: ids,
|
|
include: include,
|
|
withinFromOnly: withinFromOnly,
|
|
dryRun: dryRun,
|
|
strict: strict,
|
|
yes: yes,
|
|
}); err != nil {
|
|
return HandleErrorRespectJSON("%v", err)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
type migrateIssuesParams struct {
|
|
from string
|
|
to string
|
|
status string
|
|
priority int
|
|
issueType string
|
|
labels []string
|
|
ids []string
|
|
include string
|
|
withinFromOnly bool
|
|
dryRun bool
|
|
strict bool
|
|
yes bool
|
|
}
|
|
|
|
type migrationPlan struct {
|
|
TotalSelected int `json:"total_selected"`
|
|
AddedByDependency int `json:"added_by_dependency"`
|
|
IncomingEdges int `json:"incoming_edges"`
|
|
OutgoingEdges int `json:"outgoing_edges"`
|
|
Orphans int `json:"orphans"`
|
|
OrphanSamples []string `json:"orphan_samples,omitempty"`
|
|
IssueIDs []string `json:"issue_ids"`
|
|
From string `json:"from"`
|
|
To string `json:"to"`
|
|
}
|
|
|
|
func executeMigrateIssues(ctx context.Context, p migrateIssuesParams) error {
|
|
s := store // use global Storage interface
|
|
|
|
// Step 1: Validate repositories exist
|
|
if err := validateRepos(ctx, s, p.from, p.to, p.strict); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 2: Build initial candidate set C using filters
|
|
candidates, err := findCandidateIssues(ctx, s, p)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to find candidate issues: %w", err)
|
|
}
|
|
|
|
if len(candidates) == 0 {
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{
|
|
"message": "No issues match the specified filters",
|
|
})
|
|
}
|
|
fmt.Println("Nothing to do: no issues match the specified filters")
|
|
return nil
|
|
}
|
|
|
|
// Step 3: Expand set to M (migration set) based on --include
|
|
migrationSet, dependencyStats, err := expandMigrationSet(ctx, s, candidates, p)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to compute migration set: %w", err)
|
|
}
|
|
|
|
// Step 4: Check for orphaned dependencies
|
|
orphans, err := checkOrphanedDependencies(ctx, s)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check dependencies: %w", err)
|
|
}
|
|
|
|
if len(orphans) > 0 && p.strict {
|
|
return fmt.Errorf("strict mode: found %d orphaned dependencies", len(orphans))
|
|
}
|
|
|
|
// Step 5: Build migration plan
|
|
plan := buildMigrationPlan(candidates, migrationSet, dependencyStats, orphans, p.from, p.to)
|
|
|
|
// Step 6: Display plan
|
|
if err := displayMigrationPlan(plan, p.dryRun); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 7: Execute migration if not dry-run
|
|
if !p.dryRun {
|
|
if !p.yes && !jsonOutput {
|
|
if !confirmMigration(plan) {
|
|
fmt.Println("Migration canceled")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
if err := executeMigration(ctx, s, migrationSet, p.to); err != nil {
|
|
return fmt.Errorf("migration failed: %w", err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{
|
|
"success": true,
|
|
"message": fmt.Sprintf("Migrated %d issues from %s to %s", len(migrationSet), p.from, p.to),
|
|
"plan": plan,
|
|
})
|
|
}
|
|
fmt.Printf("\n✓ Successfully migrated %d issues from %s to %s\n", len(migrationSet), p.from, p.to)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateRepos(ctx context.Context, s storage.DoltStorage, from, to string, strict bool) error {
|
|
// migrate-issues is a round-trip path — opt out of BEADS_MAX_ROWS
|
|
// (designer §4.1) so a misconfigured env doesn't abort migration.
|
|
// Check if source repo has any issues
|
|
fromIssues, err := s.SearchIssues(ctx, "", types.IssueFilter{
|
|
SourceRepo: &from,
|
|
Limit: 1,
|
|
MaxRows: 0,
|
|
MaxRowsSource: "",
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check source repository: %w", err)
|
|
}
|
|
|
|
if len(fromIssues) == 0 {
|
|
msg := fmt.Sprintf("source repository '%s' has no issues", from)
|
|
if strict {
|
|
return fmt.Errorf("%s", msg)
|
|
}
|
|
if !jsonOutput {
|
|
fmt.Fprintf(os.Stderr, "Warning: %s\n", msg)
|
|
}
|
|
}
|
|
|
|
// Check if destination repo exists (just a warning)
|
|
toIssues, err := s.SearchIssues(ctx, "", types.IssueFilter{
|
|
SourceRepo: &to,
|
|
Limit: 1,
|
|
MaxRows: 0,
|
|
MaxRowsSource: "",
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check destination repository: %w", err)
|
|
}
|
|
|
|
if len(toIssues) == 0 && !jsonOutput {
|
|
fmt.Fprintf(os.Stderr, "Info: destination repository '%s' will be created\n", to)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func findCandidateIssues(ctx context.Context, s storage.DoltStorage, p migrateIssuesParams) ([]string, error) {
|
|
// Build filter from params. Opt out of BEADS_MAX_ROWS (designer §4.1) —
|
|
// migrate-issues is round-trip and must enumerate every candidate.
|
|
filter := types.IssueFilter{
|
|
SourceRepo: &p.from,
|
|
MaxRows: 0,
|
|
MaxRowsSource: "",
|
|
}
|
|
|
|
// Filter by status
|
|
if p.status != "" && p.status != "all" {
|
|
status := types.Status(p.status)
|
|
filter.Status = &status
|
|
}
|
|
|
|
// Filter by priority
|
|
if p.priority >= 0 {
|
|
filter.Priority = &p.priority
|
|
}
|
|
|
|
// Filter by type
|
|
if p.issueType != "" && p.issueType != "all" {
|
|
issueType := types.IssueType(p.issueType)
|
|
filter.IssueType = &issueType
|
|
}
|
|
|
|
// Filter by labels (AND semantics)
|
|
if len(p.labels) > 0 {
|
|
filter.Labels = p.labels
|
|
}
|
|
|
|
// Filter by explicit IDs if provided (intersect with other filters)
|
|
if len(p.ids) > 0 {
|
|
filter.IDs = p.ids
|
|
}
|
|
|
|
issues, err := s.SearchIssues(ctx, "", filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
candidates := make([]string, len(issues))
|
|
for i, issue := range issues {
|
|
candidates[i] = issue.ID
|
|
}
|
|
|
|
return candidates, nil
|
|
}
|
|
|
|
type dependencyStats struct {
|
|
incomingEdges int
|
|
outgoingEdges int
|
|
}
|
|
|
|
func expandMigrationSet(ctx context.Context, s storage.DoltStorage, candidates []string, p migrateIssuesParams) ([]string, dependencyStats, error) {
|
|
if p.include == "none" || p.include == "" {
|
|
return candidates, dependencyStats{}, nil
|
|
}
|
|
|
|
// Build initial set
|
|
migrationSet := make(map[string]bool)
|
|
for _, id := range candidates {
|
|
migrationSet[id] = true
|
|
}
|
|
|
|
// BFS traversal for dependency closure
|
|
visited := make(map[string]bool)
|
|
queue := make([]string, len(candidates))
|
|
copy(queue, candidates)
|
|
|
|
for len(queue) > 0 {
|
|
current := queue[0]
|
|
queue = queue[1:]
|
|
|
|
if visited[current] {
|
|
continue
|
|
}
|
|
visited[current] = true
|
|
|
|
// Traverse based on include mode
|
|
var deps []string
|
|
var err error
|
|
|
|
switch p.include {
|
|
case "upstream":
|
|
deps, err = getUpstreamDependencies(ctx, s, current, p.from, p.withinFromOnly)
|
|
case "downstream":
|
|
deps, err = getDownstreamDependencies(ctx, s, current, p.from, p.withinFromOnly)
|
|
case "closure":
|
|
upDeps, err1 := getUpstreamDependencies(ctx, s, current, p.from, p.withinFromOnly)
|
|
downDeps, err2 := getDownstreamDependencies(ctx, s, current, p.from, p.withinFromOnly)
|
|
if err1 != nil {
|
|
err = err1
|
|
} else if err2 != nil {
|
|
err = err2
|
|
} else {
|
|
deps = append(upDeps, downDeps...)
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, dependencyStats{}, err
|
|
}
|
|
|
|
for _, dep := range deps {
|
|
if !visited[dep] {
|
|
migrationSet[dep] = true
|
|
queue = append(queue, dep)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Convert map to slice
|
|
result := make([]string, 0, len(migrationSet))
|
|
for id := range migrationSet {
|
|
result = append(result, id)
|
|
}
|
|
|
|
// Count cross-repo edges
|
|
stats, err := countCrossRepoEdges(ctx, s, result)
|
|
if err != nil {
|
|
return nil, dependencyStats{}, err
|
|
}
|
|
|
|
return result, stats, nil
|
|
}
|
|
|
|
// getUpstreamDependencies returns IDs of issues that the given issue depends on.
|
|
// If withinFromOnly is true, only returns dependencies whose issues are in fromRepo.
|
|
func getUpstreamDependencies(ctx context.Context, s storage.DoltStorage, issueID, fromRepo string, withinFromOnly bool) ([]string, error) {
|
|
// GetDependencyRecords returns deps where issue_id = issueID
|
|
depRecords, err := s.GetDependencyRecords(ctx, issueID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var deps []string
|
|
for _, dep := range depRecords {
|
|
if withinFromOnly {
|
|
// Check if the depended-on issue is in the source repo
|
|
depIssue, err := s.GetIssue(ctx, dep.DependsOnID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if depIssue == nil || depIssue.SourceRepo != fromRepo {
|
|
continue
|
|
}
|
|
}
|
|
deps = append(deps, dep.DependsOnID)
|
|
}
|
|
|
|
return deps, nil
|
|
}
|
|
|
|
// getDownstreamDependencies returns IDs of issues that depend on the given issue.
|
|
// If withinFromOnly is true, only returns dependents whose issues are in fromRepo.
|
|
func getDownstreamDependencies(ctx context.Context, s storage.DoltStorage, issueID, fromRepo string, withinFromOnly bool) ([]string, error) {
|
|
// GetDependents returns full Issue objects that depend on issueID
|
|
dependents, err := s.GetDependents(ctx, issueID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var deps []string
|
|
for _, issue := range dependents {
|
|
if withinFromOnly && issue.SourceRepo != fromRepo {
|
|
continue
|
|
}
|
|
deps = append(deps, issue.ID)
|
|
}
|
|
|
|
return deps, nil
|
|
}
|
|
|
|
func countCrossRepoEdges(ctx context.Context, s storage.DoltStorage, migrationSet []string) (dependencyStats, error) {
|
|
if len(migrationSet) == 0 {
|
|
return dependencyStats{}, nil
|
|
}
|
|
|
|
setMap := make(map[string]bool, len(migrationSet))
|
|
for _, id := range migrationSet {
|
|
setMap[id] = true
|
|
}
|
|
|
|
// Get all dependency records for migration set issues (outgoing direction)
|
|
depsByIssue, err := s.GetDependencyRecordsForIssues(ctx, migrationSet)
|
|
if err != nil {
|
|
return dependencyStats{}, fmt.Errorf("failed to get dependency records: %w", err)
|
|
}
|
|
|
|
// Count outgoing edges: migrated issues depend on external issues
|
|
outgoing := 0
|
|
for _, deps := range depsByIssue {
|
|
for _, dep := range deps {
|
|
if !setMap[dep.DependsOnID] {
|
|
outgoing++
|
|
}
|
|
}
|
|
}
|
|
|
|
// For incoming edges, we need to find all deps whose resolved target is in
|
|
// the migration set but whose issue_id is not. Use GetAllDependencyRecords;
|
|
// the returned records expose the target via dep.DependsOnID (resolved from
|
|
// the typed columns).
|
|
allDeps, err := s.GetAllDependencyRecords(ctx)
|
|
if err != nil {
|
|
return dependencyStats{}, fmt.Errorf("failed to get all dependency records: %w", err)
|
|
}
|
|
|
|
incoming := 0
|
|
for issueID, deps := range allDeps {
|
|
if setMap[issueID] {
|
|
continue // Skip edges from within the migration set
|
|
}
|
|
for _, dep := range deps {
|
|
if setMap[dep.DependsOnID] {
|
|
incoming++
|
|
}
|
|
}
|
|
}
|
|
|
|
return dependencyStats{
|
|
incomingEdges: incoming,
|
|
outgoingEdges: outgoing,
|
|
}, nil
|
|
}
|
|
|
|
func checkOrphanedDependencies(ctx context.Context, s storage.DoltStorage) ([]string, error) {
|
|
// Get all dependency records to check for orphans
|
|
allDeps, err := s.GetAllDependencyRecords(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get dependency records: %w", err)
|
|
}
|
|
|
|
// Collect all unique IDs referenced in dependencies
|
|
referencedIDs := make(map[string]bool)
|
|
for issueID, deps := range allDeps {
|
|
referencedIDs[issueID] = true
|
|
for _, dep := range deps {
|
|
referencedIDs[dep.DependsOnID] = true
|
|
}
|
|
}
|
|
|
|
// Batch-check which IDs exist
|
|
idList := make([]string, 0, len(referencedIDs))
|
|
for id := range referencedIDs {
|
|
idList = append(idList, id)
|
|
}
|
|
|
|
existingIssues, err := s.SearchIssues(ctx, "", types.IssueFilter{
|
|
IDs: idList,
|
|
MaxRows: 0,
|
|
MaxRowsSource: "",
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to check issue existence: %w", err)
|
|
}
|
|
|
|
existingSet := make(map[string]bool, len(existingIssues))
|
|
for _, issue := range existingIssues {
|
|
existingSet[issue.ID] = true
|
|
}
|
|
|
|
// Find orphans (referenced but non-existent)
|
|
var orphans []string
|
|
for id := range referencedIDs {
|
|
if !existingSet[id] {
|
|
orphans = append(orphans, id)
|
|
}
|
|
}
|
|
|
|
return orphans, nil
|
|
}
|
|
|
|
func buildMigrationPlan(candidates, migrationSet []string, stats dependencyStats, orphans []string, from, to string) migrationPlan {
|
|
orphanSamples := orphans
|
|
if len(orphanSamples) > 10 {
|
|
orphanSamples = orphanSamples[:10]
|
|
}
|
|
|
|
return migrationPlan{
|
|
TotalSelected: len(candidates),
|
|
AddedByDependency: len(migrationSet) - len(candidates),
|
|
IncomingEdges: stats.incomingEdges,
|
|
OutgoingEdges: stats.outgoingEdges,
|
|
Orphans: len(orphans),
|
|
OrphanSamples: orphanSamples,
|
|
IssueIDs: migrationSet,
|
|
From: from,
|
|
To: to,
|
|
}
|
|
}
|
|
|
|
func displayMigrationPlan(plan migrationPlan, dryRun bool) error {
|
|
if jsonOutput {
|
|
return outputJSON(map[string]interface{}{
|
|
"plan": plan,
|
|
"dry_run": dryRun,
|
|
})
|
|
}
|
|
|
|
fmt.Println("\n=== Migration Plan ===")
|
|
fmt.Printf("From: %s\n", plan.From)
|
|
fmt.Printf("To: %s\n", plan.To)
|
|
fmt.Println()
|
|
fmt.Printf("Total selected: %d issues\n", plan.TotalSelected)
|
|
if plan.AddedByDependency > 0 {
|
|
fmt.Printf("Added by dependencies: %d issues\n", plan.AddedByDependency)
|
|
}
|
|
fmt.Printf("Total to migrate: %d issues\n", len(plan.IssueIDs))
|
|
fmt.Println()
|
|
fmt.Printf("Cross-repo edges preserved:\n")
|
|
fmt.Printf(" Incoming: %d\n", plan.IncomingEdges)
|
|
fmt.Printf(" Outgoing: %d\n", plan.OutgoingEdges)
|
|
|
|
if plan.Orphans > 0 {
|
|
fmt.Println()
|
|
fmt.Printf("⚠️ Warning: Found %d orphaned dependencies\n", plan.Orphans)
|
|
if len(plan.OrphanSamples) > 0 {
|
|
fmt.Println("Sample orphaned IDs:")
|
|
for _, id := range plan.OrphanSamples {
|
|
fmt.Printf(" - %s\n", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
if dryRun {
|
|
fmt.Println("\n[DRY RUN] No changes made")
|
|
if len(plan.IssueIDs) <= 20 {
|
|
fmt.Println("\nIssues to migrate:")
|
|
for _, id := range plan.IssueIDs {
|
|
fmt.Printf(" - %s\n", id)
|
|
}
|
|
} else {
|
|
fmt.Printf("\n(%d issues would be migrated, showing first 20)\n", len(plan.IssueIDs))
|
|
for i := 0; i < 20 && i < len(plan.IssueIDs); i++ {
|
|
fmt.Printf(" - %s\n", plan.IssueIDs[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func confirmMigration(plan migrationPlan) bool {
|
|
fmt.Printf("\nMigrate %d issues from %s to %s? [y/N] ", len(plan.IssueIDs), plan.From, plan.To)
|
|
var response string
|
|
_, _ = fmt.Scanln(&response)
|
|
return strings.ToLower(strings.TrimSpace(response)) == "y"
|
|
}
|
|
|
|
func executeMigration(ctx context.Context, s storage.DoltStorage, migrationSet []string, to string) error {
|
|
return transact(ctx, s, fmt.Sprintf("bd: migrate %d issues to %s", len(migrationSet), to), func(tx storage.Transaction) error {
|
|
for _, id := range migrationSet {
|
|
if err := tx.UpdateIssue(ctx, id, map[string]interface{}{
|
|
"source_repo": to,
|
|
}, currentActor()); err != nil {
|
|
return fmt.Errorf("failed to update issue %s: %w", id, err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func loadIDsFromFile(path string) ([]string, error) {
|
|
// #nosec G304 -- file path supplied explicitly via CLI flag
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
var ids []string
|
|
for _, line := range lines {
|
|
line = strings.TrimSpace(line)
|
|
if line != "" && !strings.HasPrefix(line, "#") {
|
|
ids = append(ids, line)
|
|
}
|
|
}
|
|
|
|
return ids, nil
|
|
}
|
|
|
|
func init() {
|
|
migrateCmd.AddCommand(migrateIssuesCmd)
|
|
|
|
migrateIssuesCmd.Flags().String("from", "", "Source repository (required)")
|
|
migrateIssuesCmd.Flags().String("to", "", "Destination repository (required)")
|
|
migrateIssuesCmd.Flags().String("status", "", "Filter by status (open/closed/all)")
|
|
migrateIssuesCmd.Flags().Int("priority", -1, "Filter by priority (0-4)")
|
|
migrateIssuesCmd.Flags().String("type", "", "Filter by issue type (bug/feature/task/epic/chore/decision)")
|
|
migrateIssuesCmd.Flags().StringSlice("label", nil, "Filter by labels (can specify multiple)")
|
|
migrateIssuesCmd.Flags().StringSlice("id", nil, "Specific issue IDs to migrate (can specify multiple)")
|
|
migrateIssuesCmd.Flags().String("ids-file", "", "File containing issue IDs (one per line)")
|
|
migrateIssuesCmd.Flags().String("include", "none", "Include dependencies: none/upstream/downstream/closure")
|
|
migrateIssuesCmd.Flags().Bool("within-from-only", true, "Only include dependencies from source repo")
|
|
migrateIssuesCmd.Flags().Bool("dry-run", false, "Show plan without making changes")
|
|
migrateIssuesCmd.Flags().Bool("strict", false, "Fail on orphaned dependencies or missing repos")
|
|
migrateIssuesCmd.Flags().Bool("yes", false, "Skip confirmation prompt")
|
|
|
|
_ = migrateIssuesCmd.MarkFlagRequired("from") // Only fails if flag missing (caught in tests)
|
|
_ = migrateIssuesCmd.MarkFlagRequired("to") // Only fails if flag missing (caught in tests)
|
|
|
|
// Backwards compatibility alias at root level (hidden)
|
|
migrateIssuesAliasCmd := *migrateIssuesCmd
|
|
migrateIssuesAliasCmd.Use = "migrate-issues"
|
|
migrateIssuesAliasCmd.Hidden = true
|
|
migrateIssuesAliasCmd.Deprecated = "use 'bd migrate issues' instead (will be removed in v1.0.0)"
|
|
rootCmd.AddCommand(&migrateIssuesAliasCmd)
|
|
}
|