Domanda

I work in a team where we do all of our development in feature branches, and when features are complete we merge them back into our main branch (called development). Periodically we will do formal usability testing or demos of our software, so we make branches specifically for those tests where we alter some parts of our configuration for the test environment before we merge into our testing branch.

As a super lazy person, I decided to script the process of creating the staging branch, but I am new enough to git that I want to be sure that I haven't written a function that will come back and bite me later. So here's my function (all of this is in my .bashrc):

alias updateDev='git stash && git rebase -m -X theirs development && git stash pop'
function newTest() {
    updateDev
    git checkout -b staging-test-$1
    git push origin staging-test-$1
}

Here's the intended usage:

newTest 1.3.7

And here's what I think is happening:

  • All changes are stashed
  • The branch "development" is checked out and updated
  • Changes are reapplied (Note: No development happens directly on development, so there should never be changes)
  • The staging branch is created using the passed in version number, matching the contents of the development branch
  • The staging branch is pushed to a new remote branch on origin

Is anything in this code going to work differently than I expect it to, or can anything in this code be improved to work more reliably?

Thanks for the help!

È stato utile?

Soluzione

Well, much depends on how paranoid you want to be.

One obvious thing you could do is verify that there is at least, or exactly, one argument to the function, so that $1 expands to something:

case $# in
1) ;;
*) echo "usage: newTest <name>" 1>&2; return 1;;
esac

Another is to make sure the branch name is valid. Per git-check-ref-format:

local ref

ref=$(git check-ref-format --normalize "refs/heads/staging-test-$1") ||
    { echo "'staging-test-$1' is not a valid branch name" 1>&2; return 1; }

although then you probably want to strip refs/heads/ off again:

ref=${ref#refs/heads/}

after which you can use $ref to avoid repeating the staging-test- part.

Then: what if that branch name already exists? Is that an error, or should it be replaced? (To make it an error, check for git checkout -b failure, for instance.)

Then: what if stash or rebase fails? You say "there should never be changes", but presumably the point of saving and applying the stash is that there are some changes. It's not really clear to me what updateDev is intended to do. It does not check what the current branch is, it simply rebases the current branch (whatever it is) onto development using -X theirs (which means keep "our" version of everything).

Moreover, suppose there are no changes to stash, but there is already an existing stash. In this case, git stash will exit successfully after printing No local changes to save. The updateDev script will then run the rebase, and assuming it succeeds, will pop (apply and drop) the top-most existing stash. This is almost certainly not a good thing.

Last: what if git checkout -b and/or git push fail? (E.g., due to running out of disk space or something equally terrible, or simply because the name supplied already existed.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top