diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2023-05-22 19:39:49 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2023-05-23 09:38:20 +0300 |
commit | 93a8daed7bab13d473fc4aa1b9d84964718ba4d3 (patch) | |
tree | 28d2fa9ce09cc7142289fb9d3fba44327f5a59de /internal/gitaly/transaction_manager.go | |
parent | b480dad0bb11e5d860226885114477d20296d913 (diff) |
Call hooks custom hooks in TransactionManager
This commit updates references to hooks to talk about custom hooks
in TransactionManager. This makes it clearer we are talking about
the custom hooks the users can write in the repository, not the shims
used by git to call back to Gitaly.
Diffstat (limited to 'internal/gitaly/transaction_manager.go')
-rw-r--r-- | internal/gitaly/transaction_manager.go | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/internal/gitaly/transaction_manager.go b/internal/gitaly/transaction_manager.go index 4d922af88..65a86e838 100644 --- a/internal/gitaly/transaction_manager.go +++ b/internal/gitaly/transaction_manager.go @@ -122,9 +122,9 @@ type ReferenceUpdates map[git.ReferenceName]ReferenceUpdate type Snapshot struct { // ReadIndex is the index of the log entry this Transaction is reading the data at. ReadIndex LogIndex - // HookIndex is index of the hooks on the disk that are included in this Transactions's snapshot - // and were the latest on the read index. - HookIndex LogIndex + // CustomHookIndex is index of the custom hooks on the disk that are included in this Transactions's + // snapshot and were the latest on the read index. + CustomHookIndex LogIndex } // Transaction is a unit-of-work that contains reference changes to perform on the repository. @@ -203,8 +203,8 @@ func (mgr *TransactionManager) Begin(ctx context.Context) (_ *Transaction, retur commit: mgr.commit, finalize: mgr.transactionFinalizer, snapshot: Snapshot{ - ReadIndex: mgr.appendedLogIndex, - HookIndex: mgr.hookIndex, + ReadIndex: mgr.appendedLogIndex, + CustomHookIndex: mgr.customHookIndex, }, finished: make(chan struct{}), } @@ -348,10 +348,10 @@ func (txn *Transaction) SetDefaultBranch(new git.ReferenceName) { } // SetCustomHooks sets the custom hooks as part of the transaction. If SetCustomHooks is called multiple -// times, only the changes from the latest invocation take place. The hooks are extracted as is and are -// not validated. Setting a nil hooksTAR removes the hooks from the repository. -func (txn *Transaction) SetCustomHooks(hooksTAR []byte) { - txn.customHooksUpdate = &CustomHooksUpdate{CustomHooksTAR: hooksTAR} +// times, only the changes from the latest invocation take place. The custom hooks are extracted as is and +// are not validated. Setting a nil hooksTAR removes the hooks from the repository. +func (txn *Transaction) SetCustomHooks(customHooksTAR []byte) { + txn.customHooksUpdate = &CustomHooksUpdate{CustomHooksTAR: customHooksTAR} } // packFilePath returns the path to this transaction's pack file. @@ -451,8 +451,8 @@ type TransactionManager struct { appendedLogIndex LogIndex // appliedLogIndex holds the index of the last log entry applied to the repository appliedLogIndex LogIndex - // hookIndex stores the log index of the latest committed hooks in the repository. - hookIndex LogIndex + // customHookIndex stores the log index of the latest committed custom custom hooks in the repository. + customHookIndex LogIndex // transactionFinalizer executes when a transaction is completed. transactionFinalizer func() @@ -825,7 +825,7 @@ func (mgr *TransactionManager) initialize(ctx context.Context) error { } var err error - mgr.hookIndex, err = mgr.determineHookIndex(ctx, mgr.appendedLogIndex, mgr.appliedLogIndex) + mgr.customHookIndex, err = mgr.determineCustomHookIndex(ctx, mgr.appendedLogIndex, mgr.appliedLogIndex) if err != nil { return fmt.Errorf("determine hook index: %w", err) } @@ -881,15 +881,16 @@ func (mgr *TransactionManager) determineRepositoryExistence() error { return nil } -// determineHookIndex determines the latest hooks in the repository. +// determineCustomHookIndex determines the latest custom hooks in the repository. // // 1. First we iterate through the unapplied log in reverse order. The first log entry that -// contains custom hooks must have the latest hooks since it is the latest log entry. +// contains custom hooks must have the latest custom hooks since it is the latest log entry. // 2. If we don't find any custom hooks in the log, the latest hooks could have been applied -// to the repository already and the log entry pruned away. Look at the hooks on the disk -// to see which are the latest. -// 3. If we found no hooks in the log nor in the repository, there are no hooks configured. -func (mgr *TransactionManager) determineHookIndex(ctx context.Context, appendedIndex, appliedIndex LogIndex) (LogIndex, error) { +// to the repository already and the log entry pruned away. Look at the custom hooks on the +// disk to see which are the latest. +// 3. If we found no custom hooks in the log nor in the repository, there are no custom hooks +// configured. +func (mgr *TransactionManager) determineCustomHookIndex(ctx context.Context, appendedIndex, appliedIndex LogIndex) (LogIndex, error) { if !mgr.repositoryExists { // If the repository doesn't exist, then there are no hooks either. return 0, nil @@ -1166,12 +1167,12 @@ func (mgr *TransactionManager) appendLogEntry(nextLogIndex LogIndex, logEntry *g mgr.mutex.Lock() mgr.appendedLogIndex = nextLogIndex if logEntry.CustomHooksUpdate != nil { - mgr.hookIndex = nextLogIndex + mgr.customHookIndex = nextLogIndex } mgr.applyNotifications[nextLogIndex] = make(chan struct{}) if logEntry.RepositoryDeletion != nil { mgr.repositoryExists = false - mgr.hookIndex = 0 + mgr.customHookIndex = 0 } mgr.mutex.Unlock() @@ -1328,8 +1329,8 @@ func (mgr *TransactionManager) applyPackFile(ctx context.Context, logIndex LogIn return mgr.repository.UnpackObjects(ctx, packFile) } -// applyCustomHooks applies the custom hooks to the repository from the log entry. The hooks are stored -// at `<repo>/wal/hooks/<log_index>`. The hooks are fsynced prior to returning so it is safe to delete +// applyCustomHooks applies the custom hooks to the repository from the log entry. The custom hooks are stored +// at `<repo>/wal/hooks/<log_index>`. The custom hooks are fsynced prior to returning so it is safe to delete // the log entry afterwards. func (mgr *TransactionManager) applyCustomHooks(ctx context.Context, logIndex LogIndex, update *gitalypb.LogEntry_CustomHooksUpdate) error { if update == nil { @@ -1338,12 +1339,12 @@ func (mgr *TransactionManager) applyCustomHooks(ctx context.Context, logIndex Lo syncer := safe.NewSyncer() - hooksPath := filepath.Join("wal", "hooks") - targetDirectory := filepath.Join(mgr.repositoryPath, hooksPath, logIndex.String()) + customHooksPath := filepath.Join("wal", "hooks") + targetDirectory := filepath.Join(mgr.repositoryPath, customHooksPath, logIndex.String()) if err := os.Mkdir(targetDirectory, fs.ModePerm); err != nil { // The target directory may exist if we previously tried to extract the - // hooks there. TAR overwrites existing files and the hooks files are - // guaranteed to be the same as this is the same log entry. + // custom hooks there. TAR overwrites existing files and the custom hooks + // files are guaranteed to be the same as this is the same log entry. if !errors.Is(err, fs.ErrExist) { return fmt.Errorf("create directory: %w", err) } @@ -1359,7 +1360,7 @@ func (mgr *TransactionManager) applyCustomHooks(ctx context.Context, logIndex Lo } // Sync the parent directory as well. - if err := syncer.Sync(filepath.Join(mgr.repositoryPath, hooksPath)); err != nil { + if err := syncer.Sync(filepath.Join(mgr.repositoryPath, customHooksPath)); err != nil { return fmt.Errorf("sync hook directory: %w", err) } |