Skip to content

Instantly share code, notes, and snippets.

@markstinson
Last active August 22, 2022 16:01
Show Gist options
  • Save markstinson/a6fa4882de7660a1f91f7153dfb0405b to your computer and use it in GitHub Desktop.
Save markstinson/a6fa4882de7660a1f91f7153dfb0405b to your computer and use it in GitHub Desktop.

Revisions

  1. markstinson revised this gist Aug 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mkgitrepo.sh
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ srcpath=${homedir}/${srcdir} # or prj or whatever path/to/use

    usage="
    Usage: $0 reponame
    Purpose: make a person ssh+git repo in your central SSH homedir
    Purpose: make a personal ssh+git repo in your central SSH homedir
    Just re-run when you want to make a new repo.
    Repo should be [0-9A-Za-z_-] and not lead with a symbol
  2. markstinson created this gist Aug 20, 2022.
    123 changes: 123 additions & 0 deletions mkgitrepo.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    #!/usr/bin/env bash

    # I call this script ~/bin/mkgitrepo.sh
    # You could make a git:git if desired -- gitolite3 without management.
    # Make sure your SSH Keys are set as necessary.
    # If you want a differnt set of keys just for git, make sure the
    # pub keys are added/removed from your ~/.ssh/authorized_keys

    my_new_repo="${1}"

    # for legacy hostname lacking fqdn switch
    this_host=$(hostname -f 2> /dev/null) || this_host=$(hostname)

    homedir=~
    gitdir=git # where you bare repos reside, like a project
    srcdir=src # where you'll check out repo to

    gitpath=${homedir}/${gitdir} # or whatever path/to/use
    srcpath=${homedir}/${srcdir} # or prj or whatever path/to/use

    usage="
    Usage: $0 reponame
    Purpose: make a person ssh+git repo in your central SSH homedir
    Just re-run when you want to make a new repo.
    Repo should be [0-9A-Za-z_-] and not lead with a symbol
    "

    [[ -z ${my_new_repo} ]] || [[ ${my_new_repo} == "-h" ]] && echo "${usage}" && exit

    anykey() {
    echo
    read -p "Press Enter to continue or Ctrl+C to abort" junkvar
    echo
    }

    get_set_user_email(){
    [[ $(git config user.name) ]] || git config --global user.name "${USER}"
    [[ $(git config user.email) ]] || git config --global user.email "${USER}@${this_host}"
    echo "[ info ] Confirming ~/.gitconfig . Edit if necessary."
    cat ~/.gitconfig
    echo
    }

    mygit_mkrepo() {
    echo "[ info ] Creating new repo: ${my_new_repo}"
    [[ ! -d ${gitpath} ]] \
    && echo "[ info ] Creating ${gitpath}" \
    && mkdir ${gitpath}

    cd ${gitpath}

    git init --bare ${my_new_repo}.git
    [[ $? ]] \
    && echo "[ info ] Created: $(ls -ld ${my_new_repo}.git)" \
    || { echo "[ err ] Failure to create: ${my_new_repo}"; exit; }
    echo
    }

    mygit_initialize() {
    echo "[ info ] Initializing with README.md"

    [[ ! -d ${srcpath} ]] \
    && echo "[ info ] Creating ${srcpath}" \
    && mkdir ${srcpath}

    cd ${srcpath}

    # Open a ssh+git connection back on itself just to set up a README.md
    # no need to deal with branches yet so use the master branch

    echo "[ info ] Cloning ${my_new_repo}"
    echo
    git clone ${USER}@${this_host}:${gitpath}/${my_new_repo}.git
    cd ${my_new_repo}
    echo
    echo "[ info ] Adding and commiting README.md"
    echo "initial README" > README.md
    git add README.md
    git commit -m "added inital readme"
    echo
    echo "[ info ] Pushing README.md"
    git push origin master
    }

    finish_info() {
    echo "
    If you wish to make another, just re-run the script with a new repo name.
    Repo '${my_new_repo}' is created and initialized.
    On your remote host, do the following:
    mkdir ~/${srcdir} # or prj or whatever
    cd ~/${srcdir}
    git clone ${USER}@${this_host}:${gitdir}/${my_new_repo}.git
    cd ${my_new_repo}
    # have fun doing git business
    If you wish to make things a smidge easier, particularly if using alternate SSH keys,
    you can do the following as well:
    hostalias=gitserver # or whatever
    echo \"
    host \${hostalias}
    user ${USER}
    hostname ${this_host}
    port 22
    identityfile ~/.ssh/id_rsa # or whatever optional private key
    \" >> ~/.ssh/config
    chmod 600 ~/.ssh/config
    "
    }

    # Comment out or delete anykey if that is not your thing
    get_set_user_email
    anykey
    mygit_mkrepo
    anykey
    mygit_initialize
    anykey
    finish_info