|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +//go:build go1.25 |
| 5 | + |
| 6 | +package repository |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "path" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | + |
| 17 | + repo_model "forgejo.org/models/repo" |
| 18 | + "forgejo.org/modules/git" |
| 19 | + "forgejo.org/modules/log" |
| 20 | + "forgejo.org/modules/util" |
| 21 | +) |
| 22 | + |
| 23 | +func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error { |
| 24 | + commitTimeStr := time.Now().Format(time.RFC3339) |
| 25 | + authorSig := repo.Owner.NewGitSig() |
| 26 | + |
| 27 | + // Because this may call hooks we should pass in the environment |
| 28 | + env := append(os.Environ(), |
| 29 | + "GIT_AUTHOR_NAME="+authorSig.Name, |
| 30 | + "GIT_AUTHOR_EMAIL="+authorSig.Email, |
| 31 | + "GIT_AUTHOR_DATE="+commitTimeStr, |
| 32 | + "GIT_COMMITTER_NAME="+authorSig.Name, |
| 33 | + "GIT_COMMITTER_EMAIL="+authorSig.Email, |
| 34 | + "GIT_COMMITTER_DATE="+commitTimeStr, |
| 35 | + ) |
| 36 | + |
| 37 | + // Clone to temporary path and do the init commit. |
| 38 | + templateRepoPath := templateRepo.RepoPath() |
| 39 | + if err := git.Clone(ctx, templateRepoPath, tmpDir, git.CloneRepoOptions{ |
| 40 | + Depth: 1, |
| 41 | + Branch: templateRepo.DefaultBranch, |
| 42 | + }); err != nil { |
| 43 | + return fmt.Errorf("git clone: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + if err := util.RemoveAll(path.Join(tmpDir, ".git")); err != nil { |
| 47 | + return fmt.Errorf("remove git dir: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Variable expansion |
| 51 | + gt, err := checkGiteaTemplate(tmpDir) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("checkGiteaTemplate: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + if gt != nil { |
| 57 | + if err := util.Remove(gt.Path); err != nil { |
| 58 | + return fmt.Errorf("remove .giteatemplate: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Avoid walking tree if there are no globs |
| 62 | + if len(gt.Globs()) > 0 { |
| 63 | + // All file access should be done through `root` to avoid file traversal attacks, especially with symlinks |
| 64 | + root, err := os.OpenRoot(tmpDir) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("open root: %w", err) |
| 67 | + } |
| 68 | + defer root.Close() |
| 69 | + |
| 70 | + tmpDirSlash := strings.TrimSuffix(filepath.ToSlash(tmpDir), "/") + "/" |
| 71 | + if err := filepath.WalkDir(tmpDirSlash, func(path string, d os.DirEntry, walkErr error) error { |
| 72 | + if walkErr != nil { |
| 73 | + return walkErr |
| 74 | + } |
| 75 | + |
| 76 | + if d.IsDir() { |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + base := strings.TrimPrefix(filepath.ToSlash(path), tmpDirSlash) |
| 81 | + for _, g := range gt.Globs() { |
| 82 | + if g.Match(base) { |
| 83 | + // `path` will be an absolute filepath from `WalkDir`, but `os.Root` requires all accesses are |
| 84 | + // relative file paths from the root -- use `relPath` from here out. |
| 85 | + relPath, err := filepath.Rel(tmpDir, path) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + content, err := root.ReadFile(relPath) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + if err := root.WriteFile(relPath, |
| 96 | + []byte(generateExpansion(string(content), templateRepo, generateRepo, false)), |
| 97 | + 0o644); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + substPath := generateExpansion(relPath, templateRepo, generateRepo, true) |
| 102 | + |
| 103 | + // Create parent subdirectories if needed or continue silently if it exists |
| 104 | + if err := root.MkdirAll(filepath.Dir(substPath), 0o755); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + // Substitute filename variables |
| 109 | + if err := root.Rename(relPath, substPath); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + break |
| 114 | + } |
| 115 | + } |
| 116 | + return nil |
| 117 | + }); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if err := git.InitRepository(ctx, tmpDir, false, templateRepo.ObjectFormatName); err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + repoPath := repo.RepoPath() |
| 128 | + if stdout, _, err := git.NewCommand(ctx, "remote", "add", "origin").AddDynamicArguments(repoPath). |
| 129 | + SetDescription(fmt.Sprintf("generateRepoCommit (git remote add): %s to %s", templateRepoPath, tmpDir)). |
| 130 | + RunStdString(&git.RunOpts{Dir: tmpDir, Env: env}); err != nil { |
| 131 | + log.Error("Unable to add %v as remote origin to temporary repo to %s: stdout %s\nError: %v", repo, tmpDir, stdout, err) |
| 132 | + return fmt.Errorf("git remote add: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + // set default branch based on whether it's specified in the newly generated repo or not |
| 136 | + defaultBranch := repo.DefaultBranch |
| 137 | + if strings.TrimSpace(defaultBranch) == "" { |
| 138 | + defaultBranch = templateRepo.DefaultBranch |
| 139 | + } |
| 140 | + |
| 141 | + return initRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch) |
| 142 | +} |
0 commit comments