From 8ab9a3baea0dd1800c3050493db97a3c81a2499f Mon Sep 17 00:00:00 2001 From: vanphuc Date: Wed, 29 Oct 2025 11:52:28 +0800 Subject: [PATCH 1/6] Add hans on hp-sync-upstream --- hands_on/hp-sync-upstream.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 hands_on/hp-sync-upstream.py diff --git a/hands_on/hp-sync-upstream.py b/hands_on/hp-sync-upstream.py new file mode 100644 index 0000000..1265e97 --- /dev/null +++ b/hands_on/hp-sync-upstream.py @@ -0,0 +1,25 @@ +import os +from exercise_utils.cli import run_command +from exercise_utils.gitmastery import create_start_tag + +__requires_git__ = True +__requires_github__ = True + +def download(verbose: bool): + # Fork the samplerepo-finances repository and clone it locally + run_command([ + "gh", "repo", "fork", + "https://github.com/git-mastery/samplerepo-finances", + "--clone", + "--remote" + ], verbose) + + # Navigate into samplerepo-finances directory + os.chdir("samplerepo-finances") + + # Add the upstream remote pointing to the samplerepo-finances repository + run_command([ + "git", "remote", "add", "upstream", + "https://github.com/git-mastery/samplerepo-finances" + ], verbose) + From efdc462f75d834dca92b117e9a47adf50227fdff Mon Sep 17 00:00:00 2001 From: vanphuc Date: Wed, 29 Oct 2025 12:52:00 +0800 Subject: [PATCH 2/6] Change the name of the hands on --- hands_on/{hp-sync-upstream.py => sync_upstream.py} | 1 - 1 file changed, 1 deletion(-) rename hands_on/{hp-sync-upstream.py => sync_upstream.py} (99%) diff --git a/hands_on/hp-sync-upstream.py b/hands_on/sync_upstream.py similarity index 99% rename from hands_on/hp-sync-upstream.py rename to hands_on/sync_upstream.py index 1265e97..82525a6 100644 --- a/hands_on/hp-sync-upstream.py +++ b/hands_on/sync_upstream.py @@ -22,4 +22,3 @@ def download(verbose: bool): "git", "remote", "add", "upstream", "https://github.com/git-mastery/samplerepo-finances" ], verbose) - From 743b8aa31bbf50408357766a111c5832d1ce2151 Mon Sep 17 00:00:00 2001 From: vanphuc Date: Wed, 29 Oct 2025 12:56:00 +0800 Subject: [PATCH 3/6] Delete the existing file with the same name --- hands_on/sync_upstream.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hands_on/sync_upstream.py b/hands_on/sync_upstream.py index 82525a6..2d90d27 100644 --- a/hands_on/sync_upstream.py +++ b/hands_on/sync_upstream.py @@ -1,4 +1,5 @@ import os +import shutil from exercise_utils.cli import run_command from exercise_utils.gitmastery import create_start_tag @@ -6,6 +7,10 @@ __requires_github__ = True def download(verbose: bool): + # Clean up if there exist the same name folder + if os.path.exists("samplerepo-finances"): + shutil.rmtree("samplerepo-finances") + # Fork the samplerepo-finances repository and clone it locally run_command([ "gh", "repo", "fork", From efbef421a1ea5ba91a15dff88661af6aaa45792b Mon Sep 17 00:00:00 2001 From: vanphuc Date: Thu, 30 Oct 2025 01:13:09 +0800 Subject: [PATCH 4/6] Add the tag for the first commit --- hands_on/sync_upstream.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hands_on/sync_upstream.py b/hands_on/sync_upstream.py index 2d90d27..e24eba1 100644 --- a/hands_on/sync_upstream.py +++ b/hands_on/sync_upstream.py @@ -27,3 +27,6 @@ def download(verbose: bool): "git", "remote", "add", "upstream", "https://github.com/git-mastery/samplerepo-finances" ], verbose) + + # Mark this as the starting state + create_start_tag("hp-sync-upstream", verbose) From 50c9f404d736041204c8ea8ae4258e741ca027f1 Mon Sep 17 00:00:00 2001 From: vanphuc Date: Thu, 30 Oct 2025 01:19:08 +0800 Subject: [PATCH 5/6] Change the doc --- .DS_Store | Bin 0 -> 10244 bytes hands_on/sync_upstream.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fcc5ea7e7c74efbac7b3841dacd82604a1489a46 GIT binary patch literal 10244 zcmeHM&u<$=6n>kSW}P&yD}q!B5G!%vkeGykTB#Q|KMs|ELyh17$n4e|XO-=a@vh@W zQIO9?{0saGh#UV1{4bo~dvDh1&UlmNgec5Nvv0kA-^|ld+W3a8l@_NHxIB!sGYy0;ErgGB2tu5j#d)v{|lS~#0#ijEYvzsV<0m0a|~1jOeSXs zW3xZ+-B%#_ydgzJ%og&;S0FjgOy1u{{%Ijs5oIcG86#H4jYyfjEe&siiKh9eEK)-?`Wf&UZe3u)P_4a)0;Y zqUpc&{)Z2r^iR@fnR%PX^5%g1SNoTVyb9vFKUoUtyBz+56~>O*NU za-4|X6j_;G`W>jMvfcrTxz_63!rgP!F`gz+TTENh7JIN((H@;q2bnoDGqb#JwcA=~ zsgJWOR(ndhh5dB_djxcW9$B@l-vR6h_>@u;+j!5yHo*l)d2*G88b4WxHVxsg&>O$^ z8E`rJRp@VoXfqm`J<8bpxiU6=c$y!}8{lSoL0)aX3U=bM84-o&#((u@0-q z7TzL(g0u>AFv)xlgk9^6M2S`GomwpCCPXHV&6q`RCOSiWQ|Q%JaGhFNarNkzx!mGh z)@fZdS4H1~ANSp1<(P4b+N!>HB=axx%yYQ{N1S&sY@%6fUaO+Dri=pCCo2u>hMf_D z+6=@h5NGHkMr;KlWECW4FRU#C)1)WR)gwBf&tN;q%fLcFUj||WS&HGOq$gf!s_PDa zcjX#jw9~TM(X$(`cFG?4ycQVPwqU=SJz*tQt zS#gH{D*Pp9$OT6jVg@-e`&fp~49soGLWuFtYuph~JQp0>v9ZDo+{PttK4Y-utd2zm ze4l_LYwlt78SS(M!%E@Ms+oAOw_J81{?#9f!B_KWgT&_277q*|NpPud%1ue1CD{4GvGB2dIx*( zpC9+wP(Eu9P`^Xv!hSPt1%itl$HU5TJU;Mo{0FG)yUmsh?z^R(X^ST)|M;H)ZvP*F QcAVt8{Xcda|I7XV-zmn)NB{r; literal 0 HcmV?d00001 diff --git a/hands_on/sync_upstream.py b/hands_on/sync_upstream.py index e24eba1..ec4654b 100644 --- a/hands_on/sync_upstream.py +++ b/hands_on/sync_upstream.py @@ -28,5 +28,5 @@ def download(verbose: bool): "https://github.com/git-mastery/samplerepo-finances" ], verbose) - # Mark this as the starting state + # Mark the starting state create_start_tag("hp-sync-upstream", verbose) From 20b67f2d5b592bf505586822b62f5e6d90b3f445 Mon Sep 17 00:00:00 2001 From: vanphuc Date: Wed, 12 Nov 2025 11:55:09 +0800 Subject: [PATCH 6/6] Add more checking logic --- hands_on/sync_upstream.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/hands_on/sync_upstream.py b/hands_on/sync_upstream.py index ec4654b..acc43db 100644 --- a/hands_on/sync_upstream.py +++ b/hands_on/sync_upstream.py @@ -1,17 +1,32 @@ import os +import sys import shutil -from exercise_utils.cli import run_command -from exercise_utils.gitmastery import create_start_tag +from exercise_utils.cli import run_command, run_command_no_exit __requires_git__ = True __requires_github__ = True + +def _get_full_repo_name(verbose: bool) -> str: + """Get the full GitHub repository name for the authenticated user.""" + output = run_command(["gh", "api", "user", "-q", ".login"], verbose) + username = output.strip() if output else "" + return f"{username}/{"samplerepo-finances"}" + def download(verbose: bool): - # Clean up if there exist the same name folder + full_repo_name = _get_full_repo_name(verbose) + output = run_command_no_exit(["gh", "repo", "view", full_repo_name], verbose) + + if output is not None: + print( + f"\nRepository 'https://github.com/{full_repo_name}' already exists on GitHub.\n" + "Please delete the existing fork before proceeding." + ) + sys.exit(1) + if os.path.exists("samplerepo-finances"): shutil.rmtree("samplerepo-finances") - # Fork the samplerepo-finances repository and clone it locally run_command([ "gh", "repo", "fork", "https://github.com/git-mastery/samplerepo-finances", @@ -19,14 +34,9 @@ def download(verbose: bool): "--remote" ], verbose) - # Navigate into samplerepo-finances directory os.chdir("samplerepo-finances") - # Add the upstream remote pointing to the samplerepo-finances repository run_command([ "git", "remote", "add", "upstream", "https://github.com/git-mastery/samplerepo-finances" ], verbose) - - # Mark the starting state - create_start_tag("hp-sync-upstream", verbose)