首页 > 解决方案 > 预克隆/预初始化 git 挂钩?

问题描述

我试图弄清楚如何最好地编写“预初始化”或“预克隆”钩子。我会为刚刚学习 git 的学生使用它——某种脚本在实际执行启动或克隆 repo 之前一直检查父目录。

我知道 git 中有许多 pre-[action] 挂钩,但在存储库级别,而不是全局级别。有没有办法创建这样的钩子,或者我最好写某种别名?我通读了文档,似乎暗示这种事情是可能的,但没有具体说明如何。

更新:我最终创建了几个别名。他们来了。如果有人对如何使它们变得更好有任何建议,我肯定很感兴趣——bash 脚本不是我的专业领域。

is_git_repo() {
    $(git rev-parse --is-inside-work-tree &> /dev/null)
}

is_git_dir() {
    $(git rev-parse --is-inside-git-dir 2> /dev/null)
}

check_for_git_repo() {
  if is_git_repo || is_git_dir; then
    echo "You are currently in a git repository. Please leave this repository and try again."
    return 1
  fi
}

check_for_git_parent() {
  if tree -a -d | grep -q '.git'; then
    echo "This directory is not a git repository, but one of its subdirectories is. Please create a new subdirectory and try again."
    return 1
  fi
}

alias ginit='check_for_git_repo && check_for_git_parent && echo "Creating repository" && git init'

gclone() {
  check_for_git_repo && git clone $*
}

标签: bashgit

解决方案


推荐阅读