|
| 1 | +package downloader |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/dagimg-dot/gitsnip/internal/app/gitutil" |
| 12 | + "github.com/dagimg-dot/gitsnip/internal/app/model" |
| 13 | + "github.com/dagimg-dot/gitsnip/internal/errors" |
| 14 | + "github.com/dagimg-dot/gitsnip/internal/util" |
| 15 | +) |
| 16 | + |
| 17 | +type sparseCheckoutDownloader struct { |
| 18 | + opts model.DownloadOptions |
| 19 | +} |
| 20 | + |
| 21 | +func NewSparseCheckoutDownloader(opts model.DownloadOptions) Downloader { |
| 22 | + return &sparseCheckoutDownloader{opts: opts} |
| 23 | +} |
| 24 | + |
| 25 | +func (s *sparseCheckoutDownloader) Download() error { |
| 26 | + if !gitutil.IsGitInstalled() { |
| 27 | + return &errors.AppError{ |
| 28 | + Err: errors.ErrGitNotInstalled, |
| 29 | + Message: "Git is not installed on this system", |
| 30 | + Hint: "Please install Git to use the sparse checkout method", |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if err := util.EnsureDir(s.opts.OutputDir); err != nil { |
| 35 | + return fmt.Errorf("failed to create output directory: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + if !s.opts.Quiet { |
| 39 | + if s.opts.Branch == "" { |
| 40 | + fmt.Printf("Downloading directory %s from %s (default branch) using sparse checkout...\n", |
| 41 | + s.opts.Subdir, s.opts.RepoURL) |
| 42 | + } else { |
| 43 | + fmt.Printf("Downloading directory %s from %s (branch: %s) using sparse checkout...\n", |
| 44 | + s.opts.Subdir, s.opts.RepoURL, s.opts.Branch) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + tempDir, err := gitutil.CreateTempDir() |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + defer gitutil.CleanupTempDir(tempDir) |
| 53 | + |
| 54 | + repoURL := s.getAuthenticatedRepoURL() |
| 55 | + |
| 56 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 57 | + defer cancel() |
| 58 | + |
| 59 | + if !s.opts.Quiet { |
| 60 | + fmt.Println("Setting up Git repository...") |
| 61 | + } |
| 62 | + if err := s.initRepo(ctx, tempDir, repoURL); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if err := s.setupSparseCheckout(ctx, tempDir); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + if err := s.pullContent(ctx, tempDir); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + sparsePath := filepath.Join(tempDir, s.opts.Subdir) |
| 75 | + if _, err := os.Stat(sparsePath); os.IsNotExist(err) { |
| 76 | + return &errors.AppError{ |
| 77 | + Err: errors.ErrPathNotFound, |
| 78 | + Message: fmt.Sprintf("Directory '%s' not found in the repository", s.opts.Subdir), |
| 79 | + Hint: "Check that the folder path exists in the repository", |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if !s.opts.Quiet { |
| 84 | + fmt.Printf("Copying files to %s...\n", s.opts.OutputDir) |
| 85 | + } |
| 86 | + |
| 87 | + if err := util.CopyDirectory(sparsePath, s.opts.OutputDir); err != nil { |
| 88 | + return fmt.Errorf("failed to copy directory: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + if !s.opts.Quiet { |
| 92 | + fmt.Println("Download completed successfully.") |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (s *sparseCheckoutDownloader) getAuthenticatedRepoURL() string { |
| 98 | + if s.opts.Token == "" { |
| 99 | + return s.opts.RepoURL |
| 100 | + } |
| 101 | + |
| 102 | + if strings.HasPrefix(s.opts.RepoURL, "https://") { |
| 103 | + parts := strings.SplitN(s.opts.RepoURL[8:], "/", 2) |
| 104 | + if len(parts) == 2 { |
| 105 | + return fmt.Sprintf("https://%s@%s/%s", s.opts.Token, parts[0], parts[1]) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return s.opts.RepoURL |
| 110 | +} |
| 111 | + |
| 112 | +func (s *sparseCheckoutDownloader) initRepo(ctx context.Context, dir, repoURL string) error { |
| 113 | + if _, err := gitutil.RunGitCommand(ctx, dir, "init"); err != nil { |
| 114 | + return errors.ParseGitError(err, "git init failed") |
| 115 | + } |
| 116 | + |
| 117 | + if _, err := gitutil.RunGitCommand(ctx, dir, "remote", "add", "origin", repoURL); err != nil { |
| 118 | + return errors.ParseGitError(err, "failed to add remote") |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (s *sparseCheckoutDownloader) setupSparseCheckout(ctx context.Context, dir string) error { |
| 125 | + if _, err := gitutil.RunGitCommand(ctx, dir, "config", "core.sparseCheckout", "true"); err != nil { |
| 126 | + return errors.ParseGitError(err, "failed to enable sparse checkout") |
| 127 | + } |
| 128 | + |
| 129 | + err := s.setupModernSparseCheckout(ctx, dir) |
| 130 | + if err != nil { |
| 131 | + return s.setupLegacySparseCheckout(ctx, dir) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (s *sparseCheckoutDownloader) setupModernSparseCheckout(ctx context.Context, dir string) error { |
| 138 | + _, err := gitutil.RunGitCommand(ctx, dir, "sparse-checkout", "set", s.opts.Subdir) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (s *sparseCheckoutDownloader) setupLegacySparseCheckout(_ context.Context, dir string) error { |
| 146 | + sparseCheckoutPath := filepath.Join(dir, ".git", "info", "sparse-checkout") |
| 147 | + sparseCheckoutDir := filepath.Dir(sparseCheckoutPath) |
| 148 | + |
| 149 | + if err := os.MkdirAll(sparseCheckoutDir, 0755); err != nil { |
| 150 | + return fmt.Errorf("failed to create sparse checkout directory: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + sparseCheckoutPattern := fmt.Sprintf("%s/**", s.opts.Subdir) |
| 154 | + if err := os.WriteFile(sparseCheckoutPath, []byte(sparseCheckoutPattern), 0644); err != nil { |
| 155 | + return fmt.Errorf("failed to write sparse checkout file: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func (s *sparseCheckoutDownloader) pullContent(ctx context.Context, dir string) error { |
| 162 | + args := []string{"pull", "--depth=1", "origin"} |
| 163 | + |
| 164 | + if s.opts.Branch != "" { |
| 165 | + args = append(args, s.opts.Branch) |
| 166 | + } |
| 167 | + |
| 168 | + if !s.opts.Quiet { |
| 169 | + fmt.Println("Downloading content from repository...") |
| 170 | + } |
| 171 | + |
| 172 | + _, err := gitutil.RunGitCommand(ctx, dir, args...) |
| 173 | + if err != nil { |
| 174 | + return errors.ParseGitError(err, "failed to pull content") |
| 175 | + } |
| 176 | + |
| 177 | + return nil |
| 178 | +} |
0 commit comments