From 0598599f3fa7641de1d2d1b3b731f9c54888e65f Mon Sep 17 00:00:00 2001 From: Gilad Sharaby Date: Thu, 30 May 2024 14:19:11 +0300 Subject: [PATCH 1/2] feat: Add --commit option to git subrepo pull command --- lib/git-subrepo | 124 ++++++++++++++------------ lib/git-subrepo.d/help-functions.bash | 9 +- 2 files changed, 71 insertions(+), 62 deletions(-) diff --git a/lib/git-subrepo b/lib/git-subrepo index a6d5d96a..f6d703f8 100755 --- a/lib/git-subrepo +++ b/lib/git-subrepo @@ -64,7 +64,7 @@ Options: h Show the command summary help Help overview version Print the git-subrepo version number - + a,all Perform command on all current subrepos A,ALL Perform command on all subrepos and subsubrepos b,branch= Specify the upstream branch to push/pull/fetch @@ -76,8 +76,9 @@ m,message= Specify a commit message file= Specify a commit message file r,remote= Specify the upstream remote to push/pull/fetch s,squash Squash commits on push +c,commit= Specify a commit to pull u,update Add the --branch and/or --remote overrides to .gitrepo - + q,quiet Show minimal output v,verbose Show verbose output d,debug Show the actual commands used @@ -89,63 +90,64 @@ x,DEBUG Turn on -x Bash debugging #------------------------------------------------------------------------------ main() { # Define global variables: - local command= # Subrepo subcommand to run - local command_arguments=() # Command args after getopt parsing - local commit_msg_args=() # Arguments to show in the commit msg - local subrepos=() # List of multiple subrepos - - local all_wanted=false # Apply command to all subrepos - local ALL_wanted=false # Apply command to all subrepos and subsubrepos - local force_wanted=false # Force certain operations - local fetch_wanted=false # Fetch requested before a command - local squash_wanted=false # Squash commits on push - local update_wanted=false # Update .gitrepo with --branch and/or --remote - - local quiet_wanted=false # Output should be quiet - local verbose_wanted=false # Output should be verbose - local debug_wanted=false # Show debug messages - - local subdir= # Subdirectory of the subrepo being used - local subref= # Valid git ref format of subdir - local gitrepo= # Path to .gitrepo file - local worktree= # Worktree created by 'git worktree' + local command= # Subrepo subcommand to run + local command_arguments=() # Command args after getopt parsing + local commit_msg_args=() # Arguments to show in the commit msg + local subrepos=() # List of multiple subrepos + + local all_wanted=false # Apply command to all subrepos + local ALL_wanted=false # Apply command to all subrepos and subsubrepos + local force_wanted=false # Force certain operations + local fetch_wanted=false # Fetch requested before a command + local squash_wanted=false # Squash commits on push + local update_wanted=false # Update .gitrepo with --branch and/or --remote + + local quiet_wanted=false # Output should be quiet + local verbose_wanted=false # Output should be verbose + local debug_wanted=false # Show debug messages + + local subdir= # Subdirectory of the subrepo being used + local subref= # Valid git ref format of subdir + local gitrepo= # Path to .gitrepo file + local worktree= # Worktree created by 'git worktree' local start_pwd - start_pwd=$(pwd) # Store the original directory + start_pwd=$(pwd) # Store the original directory - local original_head_commit= # HEAD commit id at start of command - local original_head_branch= # HEAD ref at start of command - local upstream_head_commit= # HEAD commit id from a subrepo fetch + local original_head_commit= # HEAD commit id at start of command + local original_head_branch= # HEAD ref at start of command + local upstream_head_commit= # HEAD commit id from a subrepo fetch - local subrepo_remote= # Remote url for subrepo's upstream repo - local subrepo_branch= # Upstream branch to clone/push/pull - local subrepo_commit= # Upstream HEAD from previous clone/pull - local subrepo_parent= # Local commit from before previous clone/pull - local subrepo_former= # A retired gitrepo key that might still exist + local subrepo_remote= # Remote url for subrepo's upstream repo + local subrepo_branch= # Upstream branch to clone/push/pull + local subrepo_commit= # Upstream HEAD from previous clone/pull + local subrepo_commit_specified= # Commit specified by --commit + local subrepo_parent= # Local commit from before previous clone/pull + local subrepo_former= # A retired gitrepo key that might still exist - local refs_subrepo_branch= # A subrepo ref -> commit of branch/pull command - local refs_subrepo_commit= # A subrepo ref -> commit last merged - local refs_subrepo_fetch= # A subrepo ref -> FETCH_HEAD after fetch - local refs_subrepo_push= # A subrepo ref -> branch after push + local refs_subrepo_branch= # A subrepo ref -> commit of branch/pull command + local refs_subrepo_commit= # A subrepo ref -> commit last merged + local refs_subrepo_fetch= # A subrepo ref -> FETCH_HEAD after fetch + local refs_subrepo_push= # A subrepo ref -> branch after push - local override_remote= # Remote specified with -r - local override_branch= # Remote specified with -b + local override_remote= # Remote specified with -r + local override_branch= # Remote specified with -b - local edit_wanted=false # Edit commit message using -e - local wanted_commit_message= # Custom commit message using -m - local commit_msg_file= # Custom commit message using --file + local edit_wanted=false # Edit commit message using -e + local wanted_commit_message= # Custom commit message using -m + local commit_msg_file= # Custom commit message using --file - local join_method= # Current join method (rebase/merge) + local join_method= # Current join method (rebase/merge) - local FAIL=true # Flag for RUN: fail on error - local OUT=false # Flag for RUN: put output in $output - local TTY=false # Flag for RUN: print output directly - local SAY=true # Flag for RUN: print command for verbose - local EXEC=false # Flag for RUN: run subprocess - local OK=true # Flag that commands have succeeded - local CODE=0 # Failure reason code - local INDENT= # Verbose indentation + local FAIL=true # Flag for RUN: fail on error + local OUT=false # Flag for RUN: put output in $output + local TTY=false # Flag for RUN: print output directly + local SAY=true # Flag for RUN: print command for verbose + local EXEC=false # Flag for RUN: run subprocess + local OK=true # Flag that commands have succeeded + local CODE=0 # Failure reason code + local INDENT= # Verbose indentation - local git_version= # Git version in use + local git_version= # Git version in use # Check environment and parse CLI options: assert-environment-ok @@ -715,15 +717,18 @@ subrepo:fetch() { fi o "Fetch the upstream: $subrepo_remote ($subrepo_branch)." - RUN git fetch --no-tags --quiet "$subrepo_remote" "$subrepo_branch" - OK || return - - o "Get the upstream subrepo HEAD commit." - OUT=true RUN git rev-parse FETCH_HEAD^0 - upstream_head_commit=$output + if [[ -n $subrepo_commit_specified ]]; then + RUN git fetch --no-tags --quiet --depth=1 "$subrepo_remote" "$subrepo_commit_specified" + OUT=true RUN git rev-parse FETCH_HEAD^0 + upstream_head_commit=$output + else + RUN git fetch --no-tags --quiet "$subrepo_remote" "$subrepo_branch" + OUT=true RUN git rev-parse FETCH_HEAD^0 + upstream_head_commit=$output + fi o "Create ref '$refs_subrepo_fetch'." - git:make-ref "$refs_subrepo_fetch" FETCH_HEAD^0 + git:make-ref "$refs_subrepo_fetch" "$upstream_head_commit" } # Create a subrepo branch containing all changes @@ -1063,6 +1068,9 @@ get-command-options() { override_branch=$1 commit_msg_args+=("--branch=$1") shift ;; + -c) subrepo_commit_specified=$1 + commit_msg_args+=("--commit=$1") + shift ;; -e) edit_wanted=true ;; -f) force_wanted=true commit_msg_args+=("--force") ;; @@ -1186,6 +1194,8 @@ command-prepare() { # Do the setup steps needed by most of the subrepo subcommands: command-setup() { + subrepo_commit_specified=${subrepo_commit_specified-} + get-params "$@" check-and-normalize-subdir diff --git a/lib/git-subrepo.d/help-functions.bash b/lib/git-subrepo.d/help-functions.bash index 123bb54c..823f54dd 100644 --- a/lib/git-subrepo.d/help-functions.bash +++ b/lib/git-subrepo.d/help-functions.bash @@ -142,7 +142,7 @@ help:config() { help:fetch() { cat <<'...' - Usage: git subrepo fetch |--all [-r ] [-b ] + Usage: git subrepo fetch |--all [-r ] [-b ] [--commit=] Fetch the remote/upstream content for a subrepo. @@ -152,7 +152,7 @@ help:fetch() { `subrepo/`. These are temporary and you can easily remove them with the subrepo `clean` command. - The `fetch` command accepts the `--all`, `--branch=` and `--remote=` options. + The `fetch` command accepts the `--all`, `--branch=`, `--commit=`, and `--remote=` options. ... } @@ -205,8 +205,7 @@ help:init() { help:pull() { cat <<'...' - - Usage: git subrepo pull |--all [-M|-R|-f] [-m ] [--file=] [-e] [-b ] [-r ] [-u] + Usage: git subrepo pull |--all [-M|-R|-f] [-m ] [--file=] [-e] [-b ] [-r ] [-u] [--commit=] Update the subrepo subdir with the latest upstream changes. @@ -252,7 +251,7 @@ help:pull() { The set of commands used above are described in detail below. - The `pull` command accepts the `--all`, `--branch=`, `--edit`, `--file`, + The `pull` command accepts the `--all`, `--branch=`, `--commit=`, `--edit`, `--file`, `--force`, `--message=`, `--remote=` and `--update` options. ... } From 09dd372b220bce5049f497ab2970ecc1a1709115 Mon Sep 17 00:00:00 2001 From: Gilad Sharaby Date: Sun, 2 Jun 2024 10:21:30 +0300 Subject: [PATCH 2/2] feat: Add --commit option to git subrepo pull command - fixed fetch function --- lib/git-subrepo | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/git-subrepo b/lib/git-subrepo index f6d703f8..7db2226b 100755 --- a/lib/git-subrepo +++ b/lib/git-subrepo @@ -718,15 +718,27 @@ subrepo:fetch() { o "Fetch the upstream: $subrepo_remote ($subrepo_branch)." if [[ -n $subrepo_commit_specified ]]; then - RUN git fetch --no-tags --quiet --depth=1 "$subrepo_remote" "$subrepo_commit_specified" - OUT=true RUN git rev-parse FETCH_HEAD^0 - upstream_head_commit=$output + o "Fetching specific commit $subrepo_commit_specified" + RUN git fetch --no-tags --quiet "$subrepo_remote" "$subrepo_commit_specified" else + o "Fetching branch $subrepo_branch" RUN git fetch --no-tags --quiet "$subrepo_remote" "$subrepo_branch" - OUT=true RUN git rev-parse FETCH_HEAD^0 - upstream_head_commit=$output fi + if ! OK; then + error "Failed to fetch the upstream." + fi + + o "Get the upstream subrepo HEAD commit." + OUT=true RUN git rev-parse FETCH_HEAD^0 + upstream_head_commit=$output + + if [[ -z $upstream_head_commit ]]; then + error "Failed to resolve the fetched commit." + fi + + o "Fetched commit: $upstream_head_commit" + o "Create ref '$refs_subrepo_fetch'." git:make-ref "$refs_subrepo_fetch" "$upstream_head_commit" }