0

I want to extract a file from the last tagged version:

  • if the current head is at a tagged version, then take the previous one
  • else, take the last tagged version.

I am doing something like that :

#!/bin/bash    
newtag=`git name-rev --tags --name-only $(git rev-parse HEAD) | sed 's|\([^\^]*\)\(\^0\)$|\1|g'`
if [[ "$newtag" == undefined ]];then
    # then the current version has no tag
    newtag=`git describe`
    oldtag=`git tag --list | sort -V | tail -n1`
else
    oldtag=`git tag --list | sort -V | tail -n2 | head -n1`
fi
# Now go grab the previous file : 
git stash
git checkout $oldtag --recurse-submodules
file=`latexpand paper.tex`
git checkout $newtag --recurse-submodules
echo "$file" > oldpaper.tex
git stash pop

is there a way to do that more cleanly ? I am sure what i did to find out the right tag to go to is sub-optimal...

Moreover, getting the file is somewhat clunky right now too. The problem is that i need to ran latexexpand paper.tex to get the file from the old verison of the repo, it is not already there. Thus, i need to checkout the older version (and stash what i currently have to do so). Is'nt there a way to ask git to run a command in the state of the oldtag version and give me back the output more cleanly ?

lrnv
  • 1,038
  • 8
  • 19

1 Answers1

0

Is there a way to do that more cleanly?

This sounds like a situation where you preferably could add an additional worktree where you just run latexpand paper.tex, and then have the script fetch the head of the other worktree (e.g. git worktree list | awk '/the_other_worktree/{ print $2 }') instead of git rev-parse HEAD.

That way you can have modified files in the main worktree without it affecting the second one.

Bonus: you could then set it up as a test and have it run automatically on every commit on a branch.


PS You should seriously try to stop using stash and instead use temporary, normal commits and/or temporary branches.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • I'm already having this stuff run automatically through GitHub action to compile my latex and the corresponding latexdiff. since worktree allows to get a branch, I am not sure this is what I need as I need to get a tag, not a branch. But thanks for the links I learned something ! – lrnv Feb 20 '23 at 23:02