Changeset - 8c7bbe5bd032
[Not reviewed]
default
0 2 0
Mads Kiilerich (mads) - 5 years ago 2021-01-11 01:14:07
mads@kiilerich.com
hooks: when overwriting non-Kallithea hooks, move the old hook to .bak

Note: This will only happen when actually overwriting an unknown hook. Repeated
hook installation will thus not clobber the existing .bak file until a
non-Kallithea hook is found again.
2 files changed with 7 insertions and 3 deletions:
0 comments (0 inline, 0 general)
kallithea/model/scm.py
Show inline comments
 
@@ -683,48 +683,52 @@ class ScmModel(object):
 
            hook_file = os.path.join(hooks_path, h_type)
 
            other_hook = False
 
            log.debug('Installing git hook %s in repo %s', h_type, repo.path)
 
            if os.path.islink(hook_file):
 
                log.debug("Found symlink hook at %s", hook_file)
 
                other_hook = True
 
            elif os.path.isfile(hook_file):
 
                log.debug('hook file %s exists, checking if it is from kallithea', hook_file)
 
                with open(hook_file, 'rb') as f:
 
                    data = f.read()
 
                    matches = re.search(br'^KALLITHEA_HOOK_VER\s*=\s*(.*)$', data, flags=re.MULTILINE)
 
                    if matches:
 
                        ver = safe_str(matches.group(1))
 
                        log.debug('Found Kallithea hook - it has KALLITHEA_HOOK_VER %s', ver)
 
                    else:
 
                        log.debug('Found non-Kallithea hook at %s', hook_file)
 
                        other_hook = True
 
            elif os.path.exists(hook_file):
 
                log.debug("Found hook that isn't a regular file at %s", hook_file)
 
                other_hook = True
 
            if other_hook and not force:
 
                log.warning('skipping overwriting hook file %s', hook_file)
 
            else:
 
                log.debug('writing hook file %s', hook_file)
 
                if other_hook:
 
                    backup_file = hook_file + '.bak'
 
                    log.warning('moving existing hook to %s', backup_file)
 
                    os.rename(hook_file, backup_file)
 
                try:
 
                    fh, fn = tempfile.mkstemp(prefix=hook_file + '.tmp.')
 
                    os.write(fh, tmpl.replace(b'_TMPL_', safe_bytes(kallithea.__version__)))
 
                    os.close(fh)
 
                    os.chmod(fn, 0o777 & ~umask)
 
                    os.rename(fn, hook_file)
 
                except (OSError, IOError) as e:
 
                    log.error('error writing hook %s: %s', hook_file, e)
 

	
 

	
 
def AvailableRepoGroupChoices(repo_group_perm_level, extras=()):
 
    """Return group_id,string tuples with choices for all the repo groups where
 
    the user has the necessary permissions.
 

	
 
    Top level is -1.
 
    """
 
    groups = db.RepoGroup.query().all()
 
    if HasPermissionAny('hg.admin')('available repo groups'):
 
        groups.append(None)
 
    else:
 
        groups = list(RepoGroupList(groups, perm_level=repo_group_perm_level))
 
        if HasPermissionAny('hg.create.repository')('available repo groups'):
 
            groups.append(None)
 
        for extra in extras:
kallithea/templates/admin/settings/settings_mapping.html
Show inline comments
 
@@ -4,42 +4,42 @@ ${h.form(url('admin_settings_mapping'), 
 
                <label class="control-label" for="destroy">${_('Rescan options')}:</label>
 
                <div>
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('destroy',True)}
 
                            ${_('Delete records of missing repositories')}
 
                        </label>
 
                    </div>
 
                    <span class="help-block">${_('Check this option to remove all comments, pull requests and other records related to repositories that no longer exist in the filesystem.')}</span>
 

	
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('invalidate',True)}
 
                            ${_('Invalidate cache for all repositories')}
 
                        </label>
 
                    </div>
 
                    <span class="help-block">${_('Check this to reload data and clear cache keys for all repositories.')}</span>
 

	
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('hooks',True)}
 
                            ${_('Install Git hooks')}
 
                        </label>
 
                    </div>
 
                    <span class="help-block">${_("Verify if Kallithea's Git hooks are installed for each repository. Current hooks will be updated to the latest version.")}</span>
 
                    <span class="help-block">${_("Install Kallithea's internal hooks for all Git repositories where they are missing or can be upgraded. Existing hooks that don't seem to come from Kallithea will not be touched.")}</span>
 
                    <div class="checkbox">
 
                        <label>
 
                            ${h.checkbox('hooks_overwrite', True)}
 
                            ${_('Overwrite existing Git hooks')}
 
                            ${_('Install and overwrite Git hooks')}
 
                        </label>
 
                    </div>
 
                    <span class="help-block">${_("If installing Git hooks, overwrite any existing hooks, even if they do not seem to come from Kallithea. WARNING: This operation will destroy any custom git hooks you may have deployed by hand!")}</span>
 
                    <span class="help-block">${_("Install Kallithea's internal hooks for all Git repositories. Existing hooks that don't seem to come from Kallithea will be disabled by renaming to .bak extension.")}</span>
 
                </div>
 
            </div>
 

	
 
            <div class="form-group">
 
                <div class="buttons">
 
                    ${h.submit('rescan',_('Rescan Repositories'),class_="btn btn-default")}
 
                </div>
 
            </div>
 
    </div>
 
${h.end_form()}
0 comments (0 inline, 0 general)