|
| 1 | +# git functions |
| 2 | + |
| 3 | +# Just some local things Mike uses (release early, release often) |
| 4 | +# |
| 5 | +# Worth a look: |
| 6 | +# |
| 7 | +# - git-ls-noticed: List files from git index (based on a gitinclude file) |
| 8 | +# - git-tarball-create: Creates a tarball based on a gitinclude (special gitginore) file |
| 9 | +# - git-repos: List all git repos (remote_url and git_dir) |
| 10 | + |
| 11 | +git-tarball-create() { |
| 12 | + |
| 13 | + # git-tarball-create - Creates a tarball based on a single gitinclude file |
| 14 | + # |
| 15 | + # Usage: git-tarball-create <gitinclude_file> |
| 16 | + # |
| 17 | + |
| 18 | + local gitinclude_file="${1:-}" |
| 19 | + |
| 20 | + # Verify gitinclude_file argument is provided |
| 21 | + if [[ -z $gitinclude_file ]]; then |
| 22 | + echo "Usage: tarball-create <gitinclude_file>" >&2 |
| 23 | + return 1 |
| 24 | + elif [[ ! -f $gitinclude_file ]]; then |
| 25 | + # Verify gitinclude_file file exists |
| 26 | + echo "Error: gitinclude file '$gitinclude_file' not found." >&2 |
| 27 | + return 1 |
| 28 | + fi |
| 29 | + |
| 30 | + local last_commit_date="$(git log -1 --format=%cd --date=format:%Y-%m-%d)" |
| 31 | + local git_repo_path="$(git rev-parse --show-toplevel)" |
| 32 | + local git_repo_name="${git_repo_path##*/}" |
| 33 | + local variant="$(basename "${gitinclude_file}")" |
| 34 | + local release="${git_repo_name}-${variant}-${last_commit_date}" |
| 35 | + local build_dir="${git_repo_path}/build" |
| 36 | + local tarball="${build_dir}/${release}.tar.gz" |
| 37 | + |
| 38 | + ( |
| 39 | + cd "${git_repo_path}" || exit |
| 40 | + # Ensure the build directory exists |
| 41 | + mkdir -p "$build_dir" || { echo "Failed to create build directory: $build_dir" >&2; return 1; } |
| 42 | + |
| 43 | + # Use git to list files for inclusion in the tarball |
| 44 | + if ! git-ls-noticed "$gitinclude_file" \ |
| 45 | + | tar \ |
| 46 | + --transform "flags=r;s,^,${release}/," \ |
| 47 | + -czf "${tarball}" -T - |
| 48 | + then |
| 49 | + echo "Error: Failed to create tarball." >&2 |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + ) |
| 53 | + |
| 54 | + echo "Tarball created: ${tarball}" |
| 55 | +} |
| 56 | + |
| 57 | +git-archive() { |
| 58 | + |
| 59 | + local last_commit_date="$(git log -1 --format=%cd --date=format:%Y-%m-%d)" |
| 60 | + local git_repo_path="$(git rev-parse --show-toplevel)" |
| 61 | + local git_repo_name="${git_repo_path##*/}" |
| 62 | + local release="${git_repo_name}-${last_commit_date}" |
| 63 | + local build_dir="${git_repo_path}/build" |
| 64 | + local tarball="${build_dir}/${release}.tar.gz" |
| 65 | + local label="$(basename $(git rev-parse --show-toplevel))-$(git log -1 --format=%cd --date=format:%Y-%m-%d)" |
| 66 | + |
| 67 | + ( |
| 68 | + cd "${git_repo_path}" || exit |
| 69 | + |
| 70 | + # Ensure the build directory exists |
| 71 | + mkdir -p "$build_dir" || { echo "Failed to create build directory: $build_dir" >&2; return 1; } |
| 72 | + |
| 73 | + git archive \ |
| 74 | + --prefix="${release}/" \ |
| 75 | + --format=tar.gz \ |
| 76 | + -o "${build_dir}/${release}.tar.gz" \ |
| 77 | + HEAD |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +git-remote-add-github() { |
| 82 | + git remote add github "github.com_mbailey:mbailey/$(git-repo-name).git" |
| 83 | + git branch -M master |
| 84 | + git push -u github master |
| 85 | +} |
| 86 | + |
| 87 | +git-repo-name() { |
| 88 | + basename "$(git rev-parse --show-toplevel)" |
| 89 | +} |
| 90 | + |
| 91 | +git-repos() { |
| 92 | + |
| 93 | + local search_dirs="$(skim-stdin "$@")" |
| 94 | + [[ -z $search_dirs ]] && search_dirs='.' |
| 95 | + |
| 96 | + # debug "$search_dirs" |
| 97 | + |
| 98 | + # Find all .git directories under the specified directories |
| 99 | + for search_dir in $search_dirs; do |
| 100 | + find -L "$search_dir" -type d -name .git 2>/dev/null | while read -r git_dir; do |
| 101 | + local repo_dir |
| 102 | + repo_dir=$(dirname "$git_dir") # Get the repository directory by removing the .git part |
| 103 | + local repo_url |
| 104 | + repo_url=$(git -C "$repo_dir" config --get remote.origin.url) # Get the repository's remote URL |
| 105 | + printf "%s\t%s\n" "$repo_url" "${repo_dir}" # Print the repository URL and directory path, separated by a tab |
| 106 | + done \ |
| 107 | + | sort -k1,1 \ |
| 108 | + | bma columnise |
| 109 | + done |
| 110 | +} |
| 111 | + |
| 112 | +git-ls-noticed() { |
| 113 | + # This uses gitignore but flips the meaning. Any file matched here is allowed (instead of ignored). |
| 114 | + # |
| 115 | + # List files with: |
| 116 | + # |
| 117 | + # git ls-files --ignored --cached --exclude-from=<path/to/this_file> |
| 118 | + # |
| 119 | + # How it works: |
| 120 | + # |
| 121 | + # `--ignored`: Lists all files that are ignored by git |
| 122 | + # `--cached`: Lists all files that are staged for commit. (Required to use `--ignored`.) |
| 123 | + # `--exclude-from`: Use specified file as the ignore file (and ignore all others) |
| 124 | + # |
| 125 | + # Test with: |
| 126 | + # |
| 127 | + # diff <(git-ls-noticed path/to/.gitinclude/target) <(tar-ls build/example-target-2024-04-17.tar.gz) |
| 128 | + |
| 129 | + local gitinclude_file="${1:-}" |
| 130 | + |
| 131 | + if [[ -z "$gitinclude_file" ]]; then |
| 132 | + echo "Usage: git-ls-noticed <gitinclude_file>" >&2 |
| 133 | + return 1 |
| 134 | + elif [[ ! -f "$gitinclude_file" ]]; then |
| 135 | + echo "Error: File '$gitinclude_file' not found." >&2 |
| 136 | + return 1 |
| 137 | + fi |
| 138 | + |
| 139 | + # Convert the relative path to an absolute path |
| 140 | + gitinclude_file="$(realpath "$gitinclude_file")" |
| 141 | + |
| 142 | + # Ensure command is run within a git repository |
| 143 | + local git_repo_path |
| 144 | + if ! git_repo_path=$(git rev-parse --show-toplevel); then |
| 145 | + echo "Error: Current directory is not within a git repository." >&2 |
| 146 | + return 1 |
| 147 | + fi |
| 148 | + |
| 149 | + # Use subshell do directory change is not persistent |
| 150 | + ( |
| 151 | + cd "${git_repo_path}" || return 1 |
| 152 | + # Use git to list files that are not ignored |
| 153 | + git ls-files --ignored --cached --exclude-from="$gitinclude_file" |
| 154 | + ) |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +git-repo-dirs(){ |
| 160 | + |
| 161 | + # list dirs under a path that contain git repos |
| 162 | + |
| 163 | + local search_dir="${1:-~/git}" # Use provided directory or default to current directory if not provided |
| 164 | + |
| 165 | + git-repos "${search_dir}" | cut -f2 | xargs dirname | sort -u | rg -v .old | sed "s#$search_dir/##" |
| 166 | +} |
| 167 | + |
| 168 | +git-repos-save() { |
| 169 | + ( |
| 170 | + cd ~/.mt/git |
| 171 | + for dir in $(git-repo-dirs ~/git); do |
| 172 | + echo $dir |
| 173 | + mkdir -p $dir |
| 174 | + git-repos ~/git/$dir | tee $dir/git-repos.txt |
| 175 | + done |
| 176 | + ) |
| 177 | +} |
| 178 | + |
| 179 | +git-repo-create() { |
| 180 | + local repo_name=$(basename "$1" .git) # Ensures repo_name is derived cleanly |
| 181 | + |
| 182 | + # Prevent cloning into the same repository directory |
| 183 | + if [ -d "$repo_name.git" ]; then |
| 184 | + echo "A directory named '$repo_name.git' already exists. Exiting to avoid conflict." |
| 185 | + return 1 |
| 186 | + fi |
| 187 | + |
| 188 | + # Ensure all files are committed in $repo_name |
| 189 | + ( |
| 190 | + cd "$repo_name" || return 1 |
| 191 | + if ! git diff --quiet; then |
| 192 | + echo "Uncommitted changes found in '$repo_name'. Please commit all changes before proceeding." |
| 193 | + return 1 |
| 194 | + fi |
| 195 | + ) |
| 196 | + |
| 197 | + # Clone as bare repository |
| 198 | + if ! git clone --bare "$1" "$repo_name.git"; then |
| 199 | + echo "Cloning failed. Please check the repository path." |
| 200 | + return 1 |
| 201 | + fi |
| 202 | + |
| 203 | + # Secure copy to remote server |
| 204 | + if ! scp -r "$repo_name.git" git:git; then |
| 205 | + echo "Failed to copy repository to remote server." |
| 206 | + return 1 |
| 207 | + fi |
| 208 | + |
| 209 | + # clone it |
| 210 | + git clone "git.failmode.com_m:git/${repo_name}" "${repo_name}.cloned-from-remote" |
| 211 | + |
| 212 | + # Move old repo |
| 213 | + mv "${repo_name}" "${repo_name}.cloned_to_remote" |
| 214 | + |
| 215 | + # Move new repo |
| 216 | + mv "${repo_name}.cloned-from-remote" "${repo_name}" |
| 217 | + |
| 218 | + echo "Repository '$repo_name.git' successfully cloned, copied to remote server and cloned back." |
| 219 | +} |
| 220 | + |
| 221 | + |
| 222 | + |
0 commit comments