copyGitTree preserves the index, untracked files and worktree metadata, which a clone would lose. It copies file contents, never hardlinks. Only Git's path files and internal absolute symlinks are relocated; user file contents stay unchanged.
(source, destination string)
| 121 | // files and internal absolute symlinks are relocated; user file contents stay |
| 122 | // unchanged. |
| 123 | func copyGitTree(source, destination string) error { |
| 124 | source, err := filepath.EvalSymlinks(source) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | destination, err = filepath.Abs(destination) |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | if pathWithin(source, destination) { |
| 133 | return fmt.Errorf("cannot copy Git state into itself") |
| 134 | } |
| 135 | if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | if err := os.Mkdir(destination, 0o755); err != nil { |
| 139 | return err |
| 140 | } |
| 141 | destination, err = filepath.EvalSymlinks(destination) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | if pathWithin(source, destination) { |
| 146 | return fmt.Errorf("cannot copy Git state into itself") |
| 147 | } |
| 148 | return filepath.WalkDir(source, func(path string, entry fs.DirEntry, walkErr error) error { |
| 149 | if walkErr != nil { |
| 150 | return walkErr |
| 151 | } |
| 152 | rel, err := filepath.Rel(source, path) |
| 153 | if err != nil || rel == "." { |
| 154 | return err |
| 155 | } |
| 156 | target := filepath.Join(destination, rel) |
| 157 | info, err := entry.Info() |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | switch { |
| 162 | case entry.IsDir(): |
| 163 | return os.Mkdir(target, info.Mode().Perm()|0o700) |
| 164 | case entry.Type()&os.ModeSymlink != 0: |
| 165 | link, err := os.Readlink(path) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | if filepath.IsAbs(link) && pathWithin(source, link) { |
| 170 | relative, err := filepath.Rel(source, link) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | link = filepath.Join(destination, relative) |
| 175 | } |
| 176 | return os.Symlink(link, target) |
| 177 | case !info.Mode().IsRegular(): |
| 178 | return fmt.Errorf("cannot copy Git fixture file %s: not a regular file or symlink", rel) |
| 179 | } |
| 180 | if isGitPathFile(filepath.ToSlash(rel)) { |