omf.repo.pull: Don't repeat yourself treatment

This commit is contained in:
Derek Stavis 2015-10-24 22:21:52 -02:00
parent f95d18481e
commit a1db5d5240

View File

@ -1,49 +1,61 @@
function omf.repo.pull function omf.repo.pull
if test (count $argv) -eq 0 if test (count $argv) -eq 0
echo (omf::err)"Argument of omf.repo.pull is the repo path."(omf::off) echo (omf::err)"Argument of omf.repo.pull is the repo path."(omf::off)
return $OMF_MISSING_ARG return $OMF_MISSING_ARG
end end
set -l repo_dir $argv[1] set -l repo_dir $argv[1]
if test (command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" config --get remote.upstream.url) function omf.repo.git -V repo_dir
set repository upstream command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" $argv
else
set repository origin
end end
set initial_branch (command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" symbolic-ref -q --short HEAD); or return $OMF_UNKNOWN_ERR function omf.repo.git.stash -V repo_dir
set initial_revision (command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" rev-parse -q --verify HEAD); or return $OMF_UNKNOWN_ERR command git -C "$repo_dir" stash $argv
end
if not command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" diff --quiet set -l remote origin
if test (omf.repo.git config --get remote.upstream.url)
set remote upstream
end
set initial_branch (omf.repo.git symbolic-ref -q --short HEAD);
or return $OMF_UNKNOWN_ERR
set initial_revision (omf.repo.git rev-parse -q --verify HEAD);
or return $OMF_UNKNOWN_ERR
if not omf.repo.git diff --quiet
echo (omf::em)"Stashing your changes:"(omf::off) echo (omf::em)"Stashing your changes:"(omf::off)
command git -C "$repo_dir" status --short --untracked-files omf.repo.git status --short --untracked-files
omf.repo.git.stash save --include-untracked --quiet;
command git -C "$repo_dir" stash save --include-untracked --quiet and set stashed
set stashed
end end
if test "$initial_branch" != master if test "$initial_branch" != master
command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" checkout master --quiet omf.repo.git checkout master --quiet
end end
# the refspec ensures that '$repository/master' gets updated set -l refspec "refs/heads/master:refs/remotes/$remote/master"
command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" pull --rebase --quiet $repository "refs/heads/master:refs/remotes/$repository/master"
if test $status -eq 2 #SIGINT # the refspec ensures that '$remote/master' gets updated
command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" checkout $initial_branch if not omf.repo.git pull --ff-only --quiet $remote $refspec
command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" reset --hard $initial_revision omf.repo.git checkout $initial_branch
set -q stashed; and command git -C "$repo_dir" stash pop omf.repo.git reset --hard $initial_revision
set -q stashed; and omf.repo.git.stash pop
end end
if test "$initial_branch" != master if test "$initial_branch" != master
command git --git-dir "$repo_dir"/.git --work-tree "$repo_dir" checkout $initial_branch --quiet omf.repo.git checkout $initial_branch --quiet
end end
if set -q stashed if set -q stashed
command git -C "$repo_dir" stash pop --quiet omf.repo.git.stash pop --quiet
echo (omf::em)"Restored your changes:"(omf::off) echo (omf::em)"Restored your changes:"(omf::off)
command git -C "$repo_dir" status --short --untracked-files command git -C "$repo_dir" status --short --untracked-files
end end
functions -e omf.repo.git{,.stash}
return 0 return 0
end end