Skip to content

Commit

Permalink
fix: handle non-standard remotes and urls (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtabrams authored Aug 30, 2024
1 parent e9b5e74 commit ee86799
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
35 changes: 19 additions & 16 deletions change
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Usage() {
echo
echo "change tag [-p]"
echo " tags the latest local commit with the lastest $change_file version"
echo " optional: -p also pushes that tag to origin"
echo " optional: -p also pushes that tag"
echo
echo "change auth [--token TOKEN]"
echo " prompts you for a personal access token for posting releases"
Expand All @@ -32,7 +32,7 @@ Usage() {
echo
echo "change all [--bump PATH]"
echo " runs change, then opens $change_file in \$EDITOR"
echo " commits and pushes the $change_file to origin"
echo " commits and pushes the $change_file"
echo " runs change tag -p and then runs change post"
echo " optional: --bump passes the newest version as an arg to a script at PATH"
echo
Expand All @@ -53,9 +53,12 @@ get_tag_date() {
date +%F
}

get_git_url() {
git config --get remote.origin.url |
sed -e "s|^git@\(.*\.com\):\(.*\)|https://\1/\2|" -e "s|\(.*\)\.git|\1|"
remote_url() {
git ls-remote --get-url 2>/dev/null
}

remote_url_clean() {
remote_url | sed -e "s|^git@\(.*\.com\):\(.*\)|https://\1/\2|" -e "s|\(.*\)\.git|\1|"
}

get_latest_log_tag() {
Expand Down Expand Up @@ -242,7 +245,7 @@ update_links() {

latest_log_ver=$(rm_tag_prefix "$latest_log_tag")
needed_ver=$(rm_tag_prefix "$needed_tag")
repo_url=$(get_git_url)
repo_url=$(remote_url_clean)

new_link="[$needed_ver]: $repo_url/compare/$latest_log_tag...$needed_tag"

Expand All @@ -256,8 +259,7 @@ update_links() {
startup_checks() {
[ ! -e "$change_file" ] && { echo "couldn't find $change_file" >&2; return 1; }
[ ! -d .git ] && { echo "the current directory doesn't contain .git" >&2; return 1; }
! git config --get remote.origin.url >/dev/null &&
{ echo "remote origin url isn't set for this repo" >&2; return 1; }
! remote_url >/dev/null && { echo "cannot find remote url for this repo" >&2; return 1; }

return 0
}
Expand All @@ -281,17 +283,18 @@ bump_version() {
Make_and_push_tag() {
! startup_checks && return 1
latest_log_tag=$(get_latest_log_tag)
repo_url=$(remote_url)

git tag "$latest_log_tag" || return 1
echo "tagged latest commit as $latest_log_tag"

[ "$push_tag" = "true" ] &&
git push --quiet origin "$latest_log_tag" &&
echo "pushed $latest_log_tag to origin" &&
git push --quiet "$repo_url" "$latest_log_tag" &&
echo "pushed $latest_log_tag to $repo_url" &&
return 0

echo "to push the latest tag use: git push origin $latest_log_tag"
echo "to push all local tags use: git push origin --tags"
printf "to push the latest tag use:\n\tgit push %s %s\n" "$repo_url" "$latest_log_tag"
printf "to push all local tags use:\n\tgit push %s --tags\n" "$repo_url"
}

Save_auth() {
Expand All @@ -309,7 +312,7 @@ Post_release() {
auth_token=$(base64 --decode < "$auth_file")
latest_log_tag=$(get_latest_log_tag)
latest_log_ver=$(rm_tag_prefix "$latest_log_tag")
repo_url=$(get_git_url | sed "s|\(^.*\)\(github.com\)|\1api.\2/repos|")
repo_url=$(remote_url_clean | sed "s|\(^.*\)\(github.com\)|\1api.\2/repos|")

diff=$(sed -n "/^## \[$latest_log_ver\] -.*/,/^## \[.*\] -.*/P" $change_file | sed "\$d")
link=$(grep "^\[$latest_log_ver\]: .*" $change_file)
Expand Down Expand Up @@ -337,8 +340,8 @@ Create_changelog() {
first_commits=$(git log --pretty=format:"%s" "$first_tag")
[ ! "$first_commits" ] && { echo "couldn't get first commits" >&2; return 2; }

repo_url=$(get_git_url)
[ ! "$repo_url" ] && { echo "remote origin url isn't set for this repo" >&2; return 1; }
repo_url=$(remote_url_clean)
[ ! "$repo_url" ] && { echo "remote url isn't set for this repo" >&2; return 1; }

echo "# Changelog
All notable changes to this project will be documented in this file.
Expand Down Expand Up @@ -396,7 +399,7 @@ Run_all() {
push_tag="true"
git add $change_file &&
git commit --quiet -m "docs: update changelog" &&
git push --quiet origin &&
git push --quiet &&
Make_and_push_tag && Post_release
}

Expand Down
3 changes: 2 additions & 1 deletion run-test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

all_tests=$*
all_tests=$(echo "$*" | tr ' ' '\n')
num_fails=0
num_tests=0
IFS="
Expand All @@ -12,6 +12,7 @@ export PATH="$cur_dir:${PATH};"
[ $# = 0 ] && all_tests="./tests/*"
[ $# != 1 ] && echo "Running tests"

# for test in $all_tests; do
for test in $all_tests; do
[ -f "$test" ] && continue
num_tests=$((num_tests + 1))
Expand Down
2 changes: 1 addition & 1 deletion tests/change-err-url/setup/exp-stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
remote origin url isn't set for this repo
cannot find remote url for this repo
2 changes: 1 addition & 1 deletion tests/init-err-url/setup/exp-stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
remote origin url isn't set for this repo
remote url isn't set for this repo
6 changes: 4 additions & 2 deletions tests/tag-normal/setup/exp-stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tagged latest commit as 0.2.0
to push the latest tag use: git push origin 0.2.0
to push all local tags use: git push origin --tags
to push the latest tag use:
git push https://github.com/adamtabrams/change.git 0.2.0
to push all local tags use:
git push https://github.com/adamtabrams/change.git --tags
6 changes: 4 additions & 2 deletions tests/tag-prefix/setup/exp-stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tagged latest commit as v0.2.0
to push the latest tag use: git push origin v0.2.0
to push all local tags use: git push origin --tags
to push the latest tag use:
git push https://github.com/adamtabrams/change.git v0.2.0
to push all local tags use:
git push https://github.com/adamtabrams/change.git --tags

0 comments on commit ee86799

Please sign in to comment.