Skip to content

Instantly share code, notes, and snippets.

@funasoul
Last active November 14, 2022 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funasoul/f40be5ac54f7b19e437677009c4a1c54 to your computer and use it in GitHub Desktop.
Save funasoul/f40be5ac54f7b19e437677009c4a1c54 to your computer and use it in GitHub Desktop.
When attaching an image to the README.md of a repository such as GitHub or GitLab, I want to push the image to another branch (ex. images) and put the URL of the image file in the README.md to avoid polluting the master repository. Since it was tedious to do it by hand every time, I wrote a zsh function to do this.
git-add-screenshot () {
# I need colors!
autoload -Uz colors;
colors
if [ $# -lt 1 ]; then
echo "Usage: $0 imagefile [branch_name (default: images)]"
echo " Create 'images (by default)' branch and push image file to GitHub/GitLab."
echo " The URL to pushed image file is also copied to your clipboard."
echo " (ex.) $0 screenshot.png"
return 1
fi
if [ ! -d .git ]; then
echo "You are ${fg_bold[red]}not in a git repository${reset_color}."
echo "Please 'cd' to the git repository where you want to add a screenshot."
return 1
fi
if [ $# -gt 1 ]; then
branch_name=$2
else
branch_name="images"
fi
if git switch ${branch_name} &>/dev/null; then
echo "A branch named '${fg_bold[red]}${branch_name}${reset_color}' already exists in your local repo."
echo "Please specify the branch name like: ${fg_bold[cyan]}$0 \$somewhere/screenshot.png branch_screenshot${reset_color}"
git checkout - &>/dev/null
return 1
fi
if git fetch origin ${branch_name} &>/dev/null; then
echo "A branch named '${fg_bold[red]}${branch_name}${reset_color}' already exists in origin."
echo "Please specify the branch name like: ${fg_bold[cyan]}$0 \$somewhere/screenshot.png branch_screenshot${reset_color}"
return 1
fi
# here we go.
imagefile=$(basename $1)
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
git checkout --orphan ${branch_name} &&
git rm --cached -rf . &&
command rm -rf *(N) &&
command rm -f .gitignore(N) &&
command cp $1 . &&
git add -f $imagefile &&
git commit -m 'Add screenshot' &&
git push origin ${branch_name} &&
local origin_ghl=$(git config --get remote.origin.url)
local tmp=$(echo $origin_ghl | cut -d':' -f2)
local repo=${tmp:r}
local url=""
if test "${origin_ghl#*github}" != "$origin_ghl"; then
echo "GitHub";
# url="https://github.com/username/reponame/raw/${branch_name}/screenshot.png"
baseurl="https://github.com/"
url="${baseurl}${repo}/raw/${branch_name}/${imagefile}"
elif test "${origin_ghl#*gitlab}" != "$origin_ghl"; then
echo "GitLab";
# url="https://gitlab.com/username/reponame/-/raw/${branch_name}/screenshot.png"
baseurl="https://gitlab.com/"
url="${baseurl}${repo}/-/raw/${branch_name}/${imagefile}"
fi
echo "${fg_bold[cyan]}## Screenshot
![screenshot]($url)${reset_color}"
if hash pbcopy >/dev/null 2>&1; then # check whether pbcopy command exists
echo "## Screenshot
![screenshot]($url)" | pbcopy
echo "Template for ${fg_bold[green]}README.md${reset_color} is copied to your clipboard."
fi
git checkout ${default_branch} &>/dev/null
echo "Now, do the following:"
echo "${fg_bold[yellow]}"
echo "git add README.md"
echo "git commit -m 'Add screenshot to README'"
echo "git push origin ${default_branch}"
echo "${reset_color}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment