|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/sourcegraph/log" |
| 10 | + |
| 11 | + "github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/common" |
| 12 | + "github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/git" |
| 13 | + "github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/gitserverfs" |
| 14 | + "github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/vcssyncer" |
| 15 | + "github.com/sourcegraph/sourcegraph/internal/api" |
| 16 | + "github.com/sourcegraph/sourcegraph/internal/database" |
| 17 | + "github.com/sourcegraph/sourcegraph/internal/fileutil" |
| 18 | + "github.com/sourcegraph/sourcegraph/lib/errors" |
| 19 | +) |
| 20 | + |
| 21 | +func postRepoFetchActions( |
| 22 | + ctx context.Context, |
| 23 | + logger log.Logger, |
| 24 | + fs gitserverfs.FS, |
| 25 | + db database.DB, |
| 26 | + backend git.GitBackend, |
| 27 | + shardID string, |
| 28 | + repo api.RepoName, |
| 29 | + dir common.GitDir, |
| 30 | + syncer vcssyncer.VCSSyncer, |
| 31 | +) (errs error) { |
| 32 | + // Note: We use a multi error in this function to try to make as many of the |
| 33 | + // post repo fetch actions succeed. |
| 34 | + |
| 35 | + if err := git.RemoveBadRefs(ctx, dir); err != nil { |
| 36 | + errs = errors.Append(errs, errors.Wrapf(err, "failed to remove bad refs for repo %q", repo)) |
| 37 | + } |
| 38 | + |
| 39 | + if err := git.SetRepositoryType(ctx, backend.Config(), syncer.Type()); err != nil { |
| 40 | + errs = errors.Append(errs, errors.Wrapf(err, "failed to set repository type for repo %q", repo)) |
| 41 | + } |
| 42 | + |
| 43 | + if err := git.SetGitAttributes(dir); err != nil { |
| 44 | + errs = errors.Append(errs, errors.Wrap(err, "setting git attributes")) |
| 45 | + } |
| 46 | + |
| 47 | + if err := gitSetAutoGC(ctx, backend.Config()); err != nil { |
| 48 | + errs = errors.Append(errs, errors.Wrap(err, "setting git gc mode")) |
| 49 | + } |
| 50 | + |
| 51 | + // Update the last-changed stamp on disk. |
| 52 | + if err := setLastChanged(logger, dir); err != nil { |
| 53 | + errs = errors.Append(errs, errors.Wrap(err, "failed to update last changed time")) |
| 54 | + } |
| 55 | + |
| 56 | + // Successfully updated, best-effort updating of db fetch state based on |
| 57 | + // disk state. |
| 58 | + if err := setLastFetched(ctx, db, shardID, dir, repo); err != nil { |
| 59 | + errs = errors.Append(errs, errors.Wrap(err, "failed setting last fetch in DB")) |
| 60 | + } |
| 61 | + |
| 62 | + // Successfully updated, best-effort calculation of the repo size. |
| 63 | + repoSizeBytes, err := fs.DirSize(dir.Path()) |
| 64 | + if err != nil { |
| 65 | + errs = errors.Append(errs, errors.Wrap(err, "failed to calculate repo size")) |
| 66 | + } else if err := db.GitserverRepos().SetRepoSize(ctx, repo, repoSizeBytes, shardID); err != nil { |
| 67 | + errs = errors.Append(errs, errors.Wrap(err, "failed to set repo size")) |
| 68 | + } |
| 69 | + |
| 70 | + return errs |
| 71 | +} |
| 72 | + |
| 73 | +// gitSetAutoGC will set the value of gc.auto. If GC is managed by Sourcegraph |
| 74 | +// the value will be 0 (disabled), otherwise if managed by git we will unset |
| 75 | +// it to rely on default (on) or global config. |
| 76 | +// |
| 77 | +// The purpose is to avoid repository corruption which can happen if several |
| 78 | +// git-gc operations are running at the same time. |
| 79 | +func gitSetAutoGC(ctx context.Context, c git.GitConfigBackend) error { |
| 80 | + switch gitGCMode { |
| 81 | + case gitGCModeGitAutoGC, gitGCModeJanitorAutoGC: |
| 82 | + return c.Unset(ctx, "gc.auto") |
| 83 | + |
| 84 | + case gitGCModeMaintenance: |
| 85 | + return c.Set(ctx, "gc.auto", "0") |
| 86 | + |
| 87 | + default: |
| 88 | + // should not happen |
| 89 | + panic(fmt.Sprintf("non exhaustive switch for gitGCMode: %d", gitGCMode)) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// setLastChanged discerns an approximate last-changed timestamp for a |
| 94 | +// repository. This can be approximate; it's used to determine how often we |
| 95 | +// should run `git fetch`, but is not relied on strongly. The basic plan |
| 96 | +// is as follows: If a repository has never had a timestamp before, we |
| 97 | +// guess that the right stamp is *probably* the timestamp of the most |
| 98 | +// chronologically-recent commit. If there are no commits, we just use the |
| 99 | +// current time because that's probably usually a temporary state. |
| 100 | +// |
| 101 | +// If a timestamp already exists, we want to update it if and only if |
| 102 | +// the set of references (as determined by `git show-ref`) has changed. |
| 103 | +// |
| 104 | +// To accomplish this, we assert that the file `sg_refhash` in the git |
| 105 | +// directory should, if it exists, contain a hash of the output of |
| 106 | +// `git show-ref`, and have a timestamp of "the last time this changed", |
| 107 | +// except that if we're creating that file for the first time, we set |
| 108 | +// it to the timestamp of the top commit. We then compute the hash of |
| 109 | +// the show-ref output, and store it in the file if and only if it's |
| 110 | +// different from the current contents. |
| 111 | +// |
| 112 | +// If show-ref fails, we use rev-list to determine whether that's just |
| 113 | +// an empty repository (not an error) or some kind of actual error |
| 114 | +// that is possibly causing our data to be incorrect, which should |
| 115 | +// be reported. |
| 116 | +func setLastChanged(logger log.Logger, dir common.GitDir) error { |
| 117 | + hashFile := dir.Path("sg_refhash") |
| 118 | + |
| 119 | + hash, err := git.ComputeRefHash(dir) |
| 120 | + if err != nil { |
| 121 | + return errors.Wrapf(err, "computeRefHash failed for %s", dir) |
| 122 | + } |
| 123 | + |
| 124 | + var stamp time.Time |
| 125 | + if _, err := os.Stat(hashFile); os.IsNotExist(err) { |
| 126 | + // This is the first time we are calculating the hash. Give a more |
| 127 | + // approriate timestamp for sg_refhash than the current time. |
| 128 | + stamp = git.LatestCommitTimestamp(logger, dir) |
| 129 | + } |
| 130 | + |
| 131 | + _, err = fileutil.UpdateFileIfDifferent(hashFile, hash) |
| 132 | + if err != nil { |
| 133 | + return errors.Wrapf(err, "failed to update %s", hashFile) |
| 134 | + } |
| 135 | + |
| 136 | + // If stamp is non-zero we have a more approriate mtime. |
| 137 | + if !stamp.IsZero() { |
| 138 | + err = os.Chtimes(hashFile, stamp, stamp) |
| 139 | + if err != nil { |
| 140 | + return errors.Wrapf(err, "failed to set mtime to the lastest commit timestamp for %s", dir) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func setLastFetched(ctx context.Context, db database.DB, shardID string, dir common.GitDir, name api.RepoName) error { |
| 148 | + lastFetched, err := repoLastFetched(dir) |
| 149 | + if err != nil { |
| 150 | + return errors.Wrapf(err, "failed to get last fetched for %s", name) |
| 151 | + } |
| 152 | + |
| 153 | + lastChanged, err := repoLastChanged(dir) |
| 154 | + if err != nil { |
| 155 | + return errors.Wrapf(err, "failed to get last changed for %s", name) |
| 156 | + } |
| 157 | + |
| 158 | + return db.GitserverRepos().SetLastFetched(ctx, name, database.GitserverFetchData{ |
| 159 | + LastFetched: lastFetched, |
| 160 | + LastChanged: lastChanged, |
| 161 | + ShardID: shardID, |
| 162 | + }) |
| 163 | +} |
0 commit comments