Mercurial > kallithea
comparison kallithea/model/scm.py @ 8598:eb486c0c3114
scm: refactor install_git_hooks
Rename, simplify, and negate some logic to make the flow more readable to me
and give better logging.
For example, "force_create" were more about "force overwrite". Calling it
"force" is more precise.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Mon, 20 Jul 2020 19:46:30 +0200 |
parents | ed92c3b846c7 |
children | bbd96bdf8372 |
comparison
equal
deleted
inserted
replaced
8597:ed92c3b846c7 | 8598:eb486c0c3114 |
---|---|
689 # FIXME This may not work on Windows and may need a shell wrapper script. | 689 # FIXME This may not work on Windows and may need a shell wrapper script. |
690 return (kallithea.CONFIG.get('git_hook_interpreter') | 690 return (kallithea.CONFIG.get('git_hook_interpreter') |
691 or sys.executable | 691 or sys.executable |
692 or '/usr/bin/env python3') | 692 or '/usr/bin/env python3') |
693 | 693 |
694 def install_git_hooks(self, repo, force_create=False): | 694 def install_git_hooks(self, repo, force=False): |
695 """ | 695 """ |
696 Creates a kallithea hook inside a git repository | 696 Creates a kallithea hook inside a git repository |
697 | 697 |
698 :param repo: Instance of VCS repo | 698 :param repo: Instance of VCS repo |
699 :param force_create: Create even if same name hook exists | 699 :param force: Overwrite existing non-Kallithea hooks |
700 """ | 700 """ |
701 | 701 |
702 hooks_path = os.path.join(repo.path, 'hooks') | 702 hooks_path = os.path.join(repo.path, 'hooks') |
703 if not repo.bare: | 703 if not repo.bare: |
704 hooks_path = os.path.join(repo.path, '.git', 'hooks') | 704 hooks_path = os.path.join(repo.path, '.git', 'hooks') |
714 'kallithea', os.path.join('config', 'pre_receive_tmpl.py') | 714 'kallithea', os.path.join('config', 'pre_receive_tmpl.py') |
715 ) | 715 ) |
716 | 716 |
717 for h_type, tmpl in [('pre', tmpl_pre), ('post', tmpl_post)]: | 717 for h_type, tmpl in [('pre', tmpl_pre), ('post', tmpl_post)]: |
718 hook_file = os.path.join(hooks_path, '%s-receive' % h_type) | 718 hook_file = os.path.join(hooks_path, '%s-receive' % h_type) |
719 has_hook = False | 719 other_hook = False |
720 log.debug('Installing git hook in repo %s', repo) | 720 log.debug('Installing git hook in repo %s', repo) |
721 if os.path.exists(hook_file): | 721 if os.path.exists(hook_file): |
722 # let's take a look at this hook, maybe it's kallithea ? | 722 # let's take a look at this hook, maybe it's kallithea ? |
723 log.debug('hook exists, checking if it is from kallithea') | 723 log.debug('hook exists, checking if it is from kallithea') |
724 with open(hook_file, 'rb') as f: | 724 with open(hook_file, 'rb') as f: |
725 data = f.read() | 725 data = f.read() |
726 matches = re.search(br'^KALLITHEA_HOOK_VER\s*=\s*(.*)$', data, flags=re.MULTILINE) | 726 matches = re.search(br'^KALLITHEA_HOOK_VER\s*=\s*(.*)$', data, flags=re.MULTILINE) |
727 if matches: | 727 if matches: |
728 try: | 728 ver = matches.groups()[0] |
729 ver = matches.groups()[0] | 729 log.debug('Found Kallithea hook - it has KALLITHEA_HOOK_VER %r', ver) |
730 log.debug('Found Kallithea hook - it has KALLITHEA_HOOK_VER %r', ver) | 730 else: |
731 has_hook = True | 731 log.debug('Found non-Kallithea hook at %s', hook_file) |
732 except Exception: | 732 other_hook = True |
733 log.error(traceback.format_exc()) | 733 |
734 if other_hook and not force: | |
735 log.warning('skipping overwriting hook file %s', hook_file) | |
734 else: | 736 else: |
735 # there is no hook in this dir, so we want to create one | |
736 has_hook = True | |
737 | |
738 if has_hook or force_create: | |
739 log.debug('writing %s hook file !', h_type) | 737 log.debug('writing %s hook file !', h_type) |
740 try: | 738 try: |
741 with open(hook_file, 'wb') as f: | 739 with open(hook_file, 'wb') as f: |
742 tmpl = tmpl.replace(b'_TMPL_', safe_bytes(kallithea.__version__)) | 740 tmpl = tmpl.replace(b'_TMPL_', safe_bytes(kallithea.__version__)) |
743 f.write(tmpl) | 741 f.write(tmpl) |
744 os.chmod(hook_file, 0o755) | 742 os.chmod(hook_file, 0o755) |
745 except IOError as e: | 743 except IOError as e: |
746 log.error('error writing %s: %s', hook_file, e) | 744 log.error('error writing hook %s: %s', hook_file, e) |
747 else: | |
748 log.debug('skipping writing hook file') | |
749 | 745 |
750 | 746 |
751 def AvailableRepoGroupChoices(repo_group_perm_level, extras=()): | 747 def AvailableRepoGroupChoices(repo_group_perm_level, extras=()): |
752 """Return group_id,string tuples with choices for all the repo groups where | 748 """Return group_id,string tuples with choices for all the repo groups where |
753 the user has the necessary permissions. | 749 the user has the necessary permissions. |