How to use git-stash --keep-index in a shell-script without conflicts

Viewed 195

I'm attempting to automate a particular operation on a git-repository (typedoc, a command to generate TypeScript documentation); but I need that command to only "see" the actually-staged changes. Any unstaged changes need to be "hidden" from the command.

My current approach is attempting to automate git-stash, such that it stashes away any untracked-files and unstaged-changes, then runs the command, then pops that stash. The meat of the script is thus:

stash_working=_no

git update-index --refresh
if git diff-index HEAD -- .':!Documentation/'; then
   stash_working=_yes
fi

if [ "$stash_working" != "_no" ]; then
   git stash push --include-untracked --keep-index -m "AUTOMATED STASH" \
       -- .':!Documentation/'
fi

typedoc
typedoc_exit_status=$?

if [ "$stash_working" != "_no" ]; then
   git stash pop
fi

exit $typedoc_exit_status

Unfortunately, this isn't working as hoped: in particular, despite "not being included" in the stash, the stashed changes are ... interfering with themselves(?) when being popped. For instance, if the staged content (that I do want in the docs) contains this line,

 * This is some fake documentation for testing purposes.

... but there's an unstaged-change updating that to

 * THIS IS A CHANGE TO THAT DOCUMENTATION

... then I get the following conflict, and that git stash pop fails:

<<<<<<< Updated upstream
/**
 * This is some fake documentation for testing purposes.
 */
export const Foo = {}
||||||| constructed merge base
=======
/**
 * THIS IS A CHANGE TO THAT DOCUMENTATION
 */
export const Foo = {}
>>>>>>> Stashed changes

I've also tried git stash pop --index, but that has the same effect, in this case.

Is there a Git-centric way (i.e. not scripting the creation of a temporary working-directory, checking out, running docgen, copying docs back to original project, type-of-thing) for me to improve on this automation so that such merge-conflicts don't occur, and the working-directory is transparently kept in the state it was at during the

0 Answers
Related