├── README.md └── ghq-migrator.bash /README.md: -------------------------------------------------------------------------------- 1 | # ghq-migrator 2 | 3 | move your local repository into [ghq](https://github.com/motemen/ghq)'s root directory with suitable URI-based path. 4 | 5 | ## DEPENDENCIES 6 | 7 | - bash 8 | - sed (may not work with GNU's version. Only tested with the BSD version) 9 | - git 10 | 11 | And you need to set ghq's root directory configuration in 'git-config'. If you already started using ghq, you need to do nothing about it. Read ghq's README. 12 | 13 | ## SYNOPSIS 14 | 15 | ``` 16 | $ ghq-migrator.bash ./(YOUR_REPOSITORY)/ 17 | ``` 18 | 19 | ## CONFIGURATION 20 | 21 | You can customize behavior of ghq-migrator by following environmental variables. 22 | 23 | ### `GHQ_MIGRATOR_ACTUALLY_RUN` 24 | 25 | Unless this variable set to `1`, ghq-migrator doesn't move repository's directory (dry-run). 26 | It's recommended to run ghq-migrator without this option at first, check output, and run with `GHQ_MIGRATOR_ACTUALLY_RUN=1` to move directory actually. 27 | 28 | ### `GHQ_MIGRATOR_PREFER_ORIGIN` 29 | 30 | By default, ghq-migrator doesn't move a repository with more than two git-remote url. 31 | If `GHQ_MIGRATOR_PREFER_ORIGIN=1` is set, ghq-migrator moves the repository according to url of `origin` even it has another remote. 32 | 33 | *Note* : Even if `GHQ_MIGRATOR_PREFER_ORIGIN=1` is specified, ghq-migrator cannot move repositories which its origin has more than 2 urls, because ghq-migrator cannot decide which url to use. 34 | 35 | ### `GHQ_MIGRATOR_LINK` 36 | 37 | If `GHQ_MIGRATOR_LINK=1` is set, a symbolic link to the destination directory will be created at the place where the repository was moved from. 38 | 39 | ### AUTHOR 40 | 41 | astj (asato.wakisaka@gmail.com) 42 | 43 | ### LICENSE 44 | 45 | MIT 46 | -------------------------------------------------------------------------------- /ghq-migrator.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | TARGET_DIR=$1 3 | GHQ_ROOT=$(ghq root) 4 | 5 | if ! [ -d $TARGET_DIR ]; then 6 | echo "directory needed! : ${TARGET_DIR}" 7 | exit 1 8 | fi 9 | 10 | # get remote-url 11 | echo $TARGET_DIR 12 | 13 | N_OF_REMOTES=$(cd $TARGET_DIR;git config --get-regexp remote.*.url | wc -l) 14 | if [ ${N_OF_REMOTES:-0} -eq 0 ]; then 15 | echo 'obtain remote url failed. is this really git repository?' 16 | exit 1 17 | fi 18 | 19 | function _remote_path_from_url { 20 | # git remote url may be 21 | # ssh://git@hoge.host:22/var/git/projects/Project 22 | # git@github.com:motemen/ghq.git 23 | # (normally considering only github is enough?) 24 | # remove ^.*:// 25 | # => remove ^hoge@ (usually git@ ?) 26 | # => replace : => / 27 | # => remove .git$ 28 | REMOTE_PATH=$(echo $1 | sed -e 's!^.*://!!; s!^.*@!!; s!:!/!; s!\.git$!!;') 29 | echo $REMOTE_PATH 30 | } 31 | 32 | function _move_repository_directory { 33 | TARGET_DIR=$1 34 | REMOTE_PATH=$2 35 | 36 | echo "move this repository to ${GHQ_ROOT}/${REMOTE_PATH}" 37 | 38 | if [ ${GHQ_MIGRATOR_LINK:-0} -eq 1 ]; then 39 | echo "create a link from ${TARGET_DIR%/} to ${GHQ_ROOT}/${REMOTE_PATH}" 40 | fi 41 | 42 | if [ ${GHQ_MIGRATOR_ACTUALLY_RUN:-0} -eq 1 ]; then 43 | NEW_REPO_DIR="${GHQ_ROOT}/${REMOTE_PATH}" 44 | if [ -e $NEW_REPO_DIR ]; then 45 | echo "${NEW_REPO_DIR} already exists!!!!" 46 | exit 1 47 | fi 48 | mkdir -p "${NEW_REPO_DIR%/*}" 49 | mv ${TARGET_DIR%/} $NEW_REPO_DIR 50 | 51 | if [ ${GHQ_MIGRATOR_LINK:-0} -eq 1 ]; then 52 | ln -s ${NEW_REPO_DIR} ${TARGET_DIR%/} 53 | fi 54 | else 55 | echo 'specify GHQ_MIGRATOR_ACTUALLY_RUN=1 to work actually' 56 | fi 57 | } 58 | 59 | if [ $N_OF_REMOTES -eq 1 ]; then 60 | REMOTE_PATH=$(_remote_path_from_url $(cd $TARGET_DIR;git config --get-regexp remote.*.url | cut -d ' ' -f 2)) 61 | _move_repository_directory $TARGET_DIR $REMOTE_PATH 62 | else 63 | echo "multiple remote detected!!!" 64 | echo '';echo '' 65 | (cd $TARGET_DIR;git config --get-regexp remote.*.url) 66 | echo '' 67 | 68 | # `git config --get remote.origin.url` returns only 1 line when remote.origin has 2 or more url ?... 69 | N_OF_ORIGIN_REMOTES=$(cd $TARGET_DIR;git config --get-regexp remote.origin.url | wc -l) 70 | if [ $N_OF_ORIGIN_REMOTES -eq 1 ] && [ ${GHQ_MIGRATOR_PREFER_ORIGIN:-0} -eq 1 ]; then 71 | REMOTE_PATH=$(_remote_path_from_url $(cd $TARGET_DIR;git config --get-regexp remote.origin.url | cut -d ' ' -f 2)) 72 | echo "Use origin" 73 | _move_repository_directory $TARGET_DIR $REMOTE_PATH 74 | else 75 | echo "We cannot decide which remote to use..." 76 | exit 1 77 | fi 78 | fi 79 | --------------------------------------------------------------------------------