Skip to content
59 changes: 45 additions & 14 deletions lib/git-subrepo
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ main() {
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_push_parent= # Local commit from before previous push
local subrepo_former= # A retired gitrepo key that might still exist

local refs_subrepo_branch= # A subrepo ref -> commit of branch/pull command
Expand Down Expand Up @@ -494,7 +495,7 @@ subrepo:clone() {

o "Commit the new '$subdir/' content."
subrepo_commit_ref="$upstream_head_commit"
CALL subrepo:commit
CALL subrepo:commit clone
}

# Init a new subrepo from current repo:
Expand Down Expand Up @@ -581,7 +582,7 @@ subrepo:push() {
if ! OK; then
# Check if we are pushing to a new upstream repo (or branch) and just
# push the commit directly. This is common after a `git subrepo init`:
local re="(^|"$'\n'")fatal: Couldn't find remote ref "
local re="(^|"$'\n'")fatal: [Cc]ouldn't find remote ref "
if [[ $output =~ $re ]]; then
o "Pushing to new upstream: $subrepo_remote ($subrepo_branch)."
new_upstream=true
Expand All @@ -604,10 +605,12 @@ subrepo:push() {
if $squash_wanted; then
o "Squash commits"
subrepo_parent="HEAD^"
elif [ -n "$subrepo_push_parent" ]; then
subrepo_parent=$subrepo_push_parent
fi

o "Create subrepo branch '$branch_name'."
CALL subrepo:branch "$branch_name"
CALL subrepo:branch "$branch_name"
cd "$worktree";

if [[ "$join_method" == "rebase" ]]; then
Expand Down Expand Up @@ -668,7 +671,9 @@ subrepo:push() {
o "Put updates into '$subdir/.gitrepo' file."
upstream_head_commit="$new_upstream_head_commit"
subrepo_commit_ref="$upstream_head_commit"
update-gitrepo-file
update-gitrepo-file "push"
# RUN git config --file="$gitrepo" subrepo.push-parent "$original_head_commit"
# RUN git add -f -- "$gitrepo"
RUN git commit -m "$(get-commit-message)"
}

Expand All @@ -693,6 +698,7 @@ subrepo:fetch() {
# Create a subrepo branch containing all changes
subrepo:branch() {
local branch="${1:-"subrepo/$subref"}"

o "Check if the '$branch' branch already exists."
git:branch-exists "$branch" && return

Expand All @@ -704,7 +710,7 @@ subrepo:branch() {
local prev_commit=
local ancestor=
o "Create new commits with parents into the subrepo fetch"
OUT=true RUN git rev-list --reverse --ancestry-path "$subrepo_parent..HEAD"
OUT=true RUN git rev-list --reverse --ancestry-path --topo-order "$subrepo_parent..HEAD"
local commit_list="$output"
for commit in $commit_list; do
o "Working on $commit"
Expand Down Expand Up @@ -807,6 +813,7 @@ subrepo:branch() {

# Commit a merged subrepo branch:
subrepo:commit() {
local is_clone=$1
o "Check that '$subrepo_commit_ref' exists."
git:rev-exists "$subrepo_commit_ref" ||
error "Commit ref '$subrepo_commit_ref' does not exist."
Expand All @@ -829,8 +836,14 @@ subrepo:commit() {
RUN git read-tree --prefix="$subdir" -u "$subrepo_commit_ref"

o "Put info into '$subdir/.gitrepo' file."
update-gitrepo-file
RUN git add -f -- "$gitrepo"
local push_update=
[ "$is_clone" == "clone" ] && push_update="push"
update-gitrepo-file "$push_update"

# if [ "$is_clone" == "clone" ]; then
# RUN git config --file="$gitrepo" subrepo.push-parent "$original_head_commit"
# fi
# RUN git add -f -- "$gitrepo"

local commit_message
if [[ -n "$wanted_commit_message" ]]; then
Expand Down Expand Up @@ -931,6 +944,10 @@ subrepo:status() {
printf " Former Commit: $(git rev-parse --short $subrepo_former)"
echo " *** DEPRECATED ***"
fi
[[ -n $subrepo_push_parent ]] &&
echo " Push Parent: $(git rev-parse --short $subrepo_push_parent)"



# Grep for directory, branch can be in detached state due to conflicts
local _worktree=$(git worktree list | grep "$GIT_TMP/subrepo/$subdir")
Expand Down Expand Up @@ -1322,6 +1339,10 @@ read-gitrepo-file() {
SAY=false OUT=true RUN git config --file="$gitrepo" subrepo.parent
subrepo_parent="$output"

FAIL=false \
SAY=false OUT=true RUN git config --file="$gitrepo" subrepo.push-parent
subrepo_push_parent="$output"

FAIL=false \
SAY=false OUT=true RUN git config --file="$gitrepo" subrepo.method
if [[ $output == "rebase" ]]; then
Expand All @@ -1342,7 +1363,8 @@ read-gitrepo-file() {
# Update the subdir/.gitrepo state file:
update-gitrepo-file() {
local short_commit=

local is_push=$1

local newfile=false
if [[ ! -e $gitrepo ]]; then

Expand Down Expand Up @@ -1374,13 +1396,22 @@ update-gitrepo-file() {
fi

RUN git config --file="$gitrepo" subrepo.commit "$upstream_head_commit"
o " upstream_head_commit:'$upstream_head_commit', subrepo_commit_ref='$subrepo_commit_ref'"

# Only write new parent when we are at the head of upstream
if [[ -n $upstream_head_commit && -n $subrepo_commit_ref ]]; then
OUT=true RUN git rev-parse "$subrepo_commit_ref"
o "$upstream_head_commit == $output"
if [[ $upstream_head_commit == $output ]]; then
RUN git config --file="$gitrepo" subrepo.parent "$original_head_commit"
fi
if [[ -n $upstream_head_commit && -n $subrepo_commit_ref && -n $original_head_commit ]]; then
#
# srg: this check prevents from updating the parent after pull
#
# OUT=true RUN git rev-parse "$subrepo_commit_ref"
# o "$upstream_head_commit == $output"
# if [[ $upstream_head_commit == $output ]]; then

RUN git config --file="$gitrepo" subrepo.parent "$original_head_commit"
if [ "$is_push" == "push" ]; then
RUN git config --file="$gitrepo" subrepo.push-parent "$original_head_commit"
fi
#fi
fi

[[ -z $join_method ]] && join_method="merge"
Expand Down
10 changes: 8 additions & 2 deletions test/error.t
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,14 @@ clone-foo-and-bar

{
is "$(
cd .git
catch git subrepo status
# the test was failing inside a submodule. there is no .git directory, just a ref
if [ -d .git ]; then
cd .git
else
cd $(git rev-parse --git-dir)
fi

catch git subrepo status
)" \
"git-subrepo: Can't 'subrepo status' outside a working tree." \
"Error OK: check inside working tree"
Expand Down
6 changes: 5 additions & 1 deletion test/pull-merge.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ gitrepo=$OWNER/foo/bar/.gitrepo
test-gitrepo-field "parent" "$foo_pull_commit"
}

foo_pull_commit="$(cd $OWNER/foo; git rev-parse HEAD)"
## i think this wrong and the parent should be moved after the pull
#foo_pull_commit="$(cd $OWNER/foo; git rev-parse HEAD)"

(
cd $OWNER/foo
Expand All @@ -36,6 +37,9 @@ foo_pull_commit="$(cd $OWNER/foo; git rev-parse HEAD)"
git push
) &> /dev/null || die

#this is the right place
foo_pull_commit="$(cd $OWNER/foo; git rev-parse HEAD)"

(
cd $OWNER/bar
modify-files-ex Bar2
Expand Down
1 change: 1 addition & 0 deletions test/status.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -e
source test/setup

use Test::More
echo "===> $0: $PWD <===" > /dev/tty

{
output="$(git subrepo status)"
Expand Down