| 1 |
package cli |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/spf13/cobra" |
| 7 |
|
| 8 |
"github.com/lain/larc/internal/convert" |
| 9 |
) |
| 10 |
|
| 11 |
/* CLI commands for larc <-> git conversion. |
| 12 |
* - larc2git: export larc repository to git format |
| 13 |
* - git2larc: import git repository to larc format */ |
| 14 |
|
| 15 |
// Larc2GitCmd creates the larc2git command |
| 16 |
func Larc2GitCmd() *cobra.Command { |
| 17 |
cmd := &cobra.Command{ |
| 18 |
Use: "larc2git <larc-repo> <git-dest>", |
| 19 |
Short: "Export larc repository to git format", |
| 20 |
Long: `Export a larc repository to git format. |
| 21 |
|
| 22 |
Converts larc revisions to git commits, preserving: |
| 23 |
- Commit history and messages |
| 24 |
- Branch structure (port -> main) |
| 25 |
- File contents and permissions |
| 26 |
- Author information and timestamps |
| 27 |
|
| 28 |
The git repository will be created at the destination path.`, |
| 29 |
Args: cobra.ExactArgs(2), |
| 30 |
RunE: func(cmd *cobra.Command, args []string) error { |
| 31 |
verbose, _ := cmd.Flags().GetBool("verbose") |
| 32 |
|
| 33 |
larcPath := args[0] |
| 34 |
gitPath := args[1] |
| 35 |
|
| 36 |
fmt.Printf("Exporting larc repository to git...\n") |
| 37 |
fmt.Printf(" Source: %s\n", larcPath) |
| 38 |
fmt.Printf(" Destination: %s\n", gitPath) |
| 39 |
|
| 40 |
if err := convert.ExportToGit(larcPath, gitPath, verbose); err != nil { |
| 41 |
return fmt.Errorf("export failed: %w", err) |
| 42 |
} |
| 43 |
|
| 44 |
return nil |
| 45 |
}, |
| 46 |
} |
| 47 |
|
| 48 |
cmd.Flags().BoolP("verbose", "v", false, "verbose output") |
| 49 |
return cmd |
| 50 |
} |
| 51 |
|
| 52 |
// Git2LarcCmd creates the git2larc command |
| 53 |
func Git2LarcCmd() *cobra.Command { |
| 54 |
cmd := &cobra.Command{ |
| 55 |
Use: "git2larc <git-repo> <larc-dest>", |
| 56 |
Short: "Import git repository to larc format", |
| 57 |
Long: `Import a git repository to larc format. |
| 58 |
|
| 59 |
Converts git commits to larc revisions, with: |
| 60 |
- Sequential revision numbering (r1, r2, ...) |
| 61 |
- Flat tree structure |
| 62 |
- Branch conversion (main/master -> port) |
| 63 |
- xxhash64 blob hashing with zstd compression |
| 64 |
|
| 65 |
The larc repository will be created at the destination path.`, |
| 66 |
Args: cobra.ExactArgs(2), |
| 67 |
RunE: func(cmd *cobra.Command, args []string) error { |
| 68 |
verbose, _ := cmd.Flags().GetBool("verbose") |
| 69 |
|
| 70 |
gitPath := args[0] |
| 71 |
larcPath := args[1] |
| 72 |
|
| 73 |
fmt.Printf("Importing git repository to larc...\n") |
| 74 |
fmt.Printf(" Source: %s\n", gitPath) |
| 75 |
fmt.Printf(" Destination: %s\n", larcPath) |
| 76 |
|
| 77 |
if err := convert.ImportFromGit(gitPath, larcPath, verbose); err != nil { |
| 78 |
return fmt.Errorf("import failed: %w", err) |
| 79 |
} |
| 80 |
|
| 81 |
return nil |
| 82 |
}, |
| 83 |
} |
| 84 |
|
| 85 |
cmd.Flags().BoolP("verbose", "v", false, "verbose output") |
| 86 |
return cmd |
| 87 |
} |
| 88 |
|
| 89 |
// ConvertCmd creates a parent command for conversion utilities |
| 90 |
func ConvertCmd() *cobra.Command { |
| 91 |
cmd := &cobra.Command{ |
| 92 |
Use: "convert", |
| 93 |
Short: "Convert between larc and git repositories", |
| 94 |
Long: `Convert between larc and git repository formats. |
| 95 |
|
| 96 |
Subcommands: |
| 97 |
larc2git Export larc repository to git |
| 98 |
git2larc Import git repository to larc |
| 99 |
|
| 100 |
Example usage: |
| 101 |
larc convert larc2git ./my-larc-repo ./my-git-repo |
| 102 |
larc convert git2larc ./my-git-repo ./my-larc-repo`, |
| 103 |
} |
| 104 |
|
| 105 |
cmd.AddCommand(Larc2GitCmd()) |
| 106 |
cmd.AddCommand(Git2LarcCmd()) |
| 107 |
|
| 108 |
return cmd |
| 109 |
} |
| 110 |
|