Skip to content

Commit a7ab401

Browse files
committed
[Tools] add git utilities (to be sourced)
1 parent ff4a3d3 commit a7ab401

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

tools/git_utils.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# This file provide some git related utilities, especially shortcut regarding github
4+
5+
# Note: this script uses return 1 to exit the script if an error occurs because if it is sourced,
6+
# it will not exit the shell
7+
8+
echo " -- enabling git utils --"
9+
10+
# Check that gh is avail and is logged in
11+
if ! which gh &>/dev/null; then
12+
echo "gh is not installed" >&2
13+
return 1
14+
fi
15+
16+
if ! gh auth status &>/dev/null; then
17+
echo "gh is not logged in, do 'gh auth login' to login" >&2
18+
return 1
19+
fi
20+
21+
function current_gh_username {
22+
echo $(gh api user -q .login)
23+
}
24+
25+
gh_username=$(current_gh_username)
26+
repo_name="$gh_username/Shamrock"
27+
upstream_repo="Shamrock-code/Shamrock"
28+
main_branch="main"
29+
30+
echo " -- gh username: $gh_username"
31+
32+
function sync_fork {
33+
echo " -- syncing fork $repo_name ($main_branch -> $main_branch) with upstream"
34+
gh repo sync $repo_name -b $main_branch
35+
echo " -- fetched latest changes from upstream"
36+
git fetch --all
37+
}
38+
39+
function current_branch {
40+
echo $(git rev-parse --abbrev-ref HEAD)
41+
}
42+
43+
# Update the fork + create branch up to date with main and push it to the fork
44+
function new_branch {
45+
sync_fork
46+
echo " -- checking out $main_branch"
47+
git checkout $main_branch
48+
echo " -- pulling $main_branch"
49+
git pull
50+
echo " -- creating new branch $1"
51+
git switch --create $1
52+
echo " -- pushing new branch $1 to origin"
53+
git push --set-upstream origin $(current_branch)
54+
}
55+
56+
# Open a PR from the fork (web interface)
57+
function open_pr_web {
58+
echo " -- opening PR ($upstream_repo:$main_branch <= $gh_username:$(current_branch)) in web browser"
59+
gh pr create --repo $upstream_repo --base $main_branch --head $gh_username:$(current_branch) --web
60+
}
61+
62+
# Open a PR from the fork (CLI)
63+
function open_pr_cli {
64+
echo " -- opening PR ($upstream_repo:$main_branch <= $gh_username:$(current_branch)) in CLI"
65+
gh pr create --repo $upstream_repo --base $main_branch --head $gh_username:$(current_branch)
66+
}

0 commit comments

Comments
 (0)