#!/bin/bash IDM_MOD_DEPS="id" IDM_DISABLE_AUTO+=" git__enable git__disable git__kill " ## Environments ############################## idm_git_header () { local id=$1 idm_vars_git_id $id git_id_config=${IDM_CONFIG_DIR}/git/$id/local_gitconfig git_id_dir=$git_dir git_id_work_tree=$git_work_tree mkdir -p $git_id_dir $git_id_work_tree || idm_exit 1 ERR "Could not create dir: $git_id_dir $git_id_work_tree" } idm_vars_git_id () { local id=$1 git_dir=$IDM_DIR_CACHE/git/$id/local.git git_work_tree=$HOME } ## Front functions ############################## idm_git__help () { echo "Git" printf " %-20s: %s\n" "git init" "Start a local repo" printf " %-20s: %s\n" "git scan" "Search and add interesting files" printf " %-20s: %s\n" "git enabled" "Enable as default git" printf " %-20s: %s\n" "git ls" "Show tracked files" printf " %-20s: %s\n" "git disable" "Disable as default git" printf " %-20s: %s\n" "git kill" "Like disable" echo printf " %-20s: %s\n" "git --help" "Git wrapper" printf " %-20s: %s\n" "git [cmd]" "Git wrapper" #if lib_id_is_enabled $id; then # idm_git_header $id # echo # echo " Config:" # lib_git id config -l | sort \ # | grep -E '(core|remote|include|remote|user|status)\.' | uniq | sed 's/^/ /' # # TOFIX: We have duplicate config entry here ... the fuckin fuck :( #fi } idm_git () { local action=$1 local id=$2 shift 2 opts=${*-} # Loading lib_id_is_enabled $id || return 1 idm_git_header $id # Forward to git lib_git id $action $opts } idm_git__init () { local id=$1 shift 1 opts=${*-} # Loading lib_id_is_enabled $id || return 1 idm_git_header $id # Check if repo exists if lib_git_is_repo_with_commits id &>/dev/null ; then lib_log WARN "Do you want to override the existing repo?" idm_cli_timeout 1 || idm_exit 1 "User cancelled" elif lib_git_is_repo id &>/dev/null; then lib_log INFO "Git repo is already there" return 0 fi # Initialise repo lib_git id init $opts || idm_exit ERR "Could not create reporitory" # Generate config lib_git id config --add include.path "$git_id_config" idm_git__gen_git_config > $git_id_config # Notify user lib_log NOTICE "Repository has been created into '$git_dir'" } idm_git__scan () { local id=$1 shift 1 opts=${*-} # Loading lib_id_is_enabled $id || return 1 idm_git_header $id # Check if it is a valid repo lib_git_is_repo id || idm_git__init $id # Add all files lib_git id add -f $( xargs <<<"$( idm_git_get_files_of_interest $id )" ) # Check uncommited changes if ! lib_git_is_all_commited id &>/dev/null ; then lib_log INFO "There are the files we could add:" lib_git id status -s lib_log PROMPT "Do you want to add these files to your repo?" if idm_cli_timeout 1; then lib_git id commit --file=- <<< "Add: Import $(hostname) data" || idm_exit 1 "Could not commit files" lib_log NOTICE "New files has been added to local repo" else lib_log NOTICE "Scan returned some new files, please commit them" fi else lib_log NOTICE "Scan didn't find other files" fi } idm_git__ls () { local id=$1 # Loading lib_id_is_enabled $id || return 1 idm_git_header $id # Check if it is a valid repo lib_git_is_repo id || return 1 # Show files lib_git id ls-files | sort | sed 's/^/ /' } idm_git__enable () { local id=$1 idm_git_header $id cat </dev/null find $HOME/.ssh/known_hosts.d/ $find_args -name "${id}*" 2>/dev/null find $HOME/.openstack/$id/ $find_args 2>/dev/null find $GNUPGHOME/private-keys-v1.d/ $find_args 2>/dev/null find $PASSWORD_STORE_DIR/ $find_args 2>/dev/null find $IDM_DIR_ID/ $find_args -name "$id*" 2>/dev/null } | sed -E "s@$HOME/?@@g" } idm_git__gen_git_config () { ( cat < /dev/null 2>&1 ; return $? # } # # lib_git_has_commits () # { # local git_dir=$1 # local git_work_tree=$2 # # lib_git_is_repo $git_dir $git_work_tree || return $? # # find "$git_dir" -type f &>/dev/null || return 1 # } # # lib_git_is_all_commited () # { # local git_dir=$1 # local git_work_tree=$2 # # [ "$( lib_git_bin $git_dir $git_work_tree status -s | wc -l)" -eq 0 ] # } #