1

I am following good practices on How to write a Python script header. I liked the idea to include a version number so each of my Python files has a line that says: __version__ = '1.0.0', different from each other (files version are uncorrelated with each other). However, changing each file manually to edit the version number before the commit is very tedious and I will eventually stop doing it.

Is there a way to search for each file that changed on the new commit and update its version number?


This question has been marked as a duplicate of Automatic version number both in setup.py (setuptools) AND source code?. However, this case is different because of the following:

  1. I have several files, each file has it's own version (as a matter of fact, I don't even have a setup.py yet). The code should change the version of each file SEPARATELY, meaning one file can be on version 50.3.4 and the other on 0.0.2.
  2. I don't care about the tag of git. I don't save versions on git at all.
  3. I want to avoid setting the version number myself. The question he wants to either set the version on Setup or git and then the other one be the same. I want the version number to be increased automatically. Example. If version was 1.0.2 I want it to ve 1.0.3 after the commit (if that particular file has indeed changed).

The question wanted to either choose (manually) a version on setup.py file or git and then update the other accordingly. I have not version on git at all.

The accepted solution (that is indeed a solution to the question) is not helpful at all for example because I have to manually change the file's version and then it will add that version to git (which has nothing to do with what I want for all the listed reasons).

J Agustin Barrachina
  • 3,501
  • 1
  • 32
  • 52
  • See: [Automatic version number both in setup.py (setuptools) AND source code?](https://stackoverflow.com/questions/6786555) – Flux Jan 21 '20 at 10:34

1 Answers1

0

So I did my own shell script. The script checks for the version line of each file from the output of git status to see the files that changed and increases the number by one.

if [ "$1" != '' ]; then
IFS="
"
git status -s
for p in `git status -s`    # For each file in git status
do
    file=`echo "$p" | rev | cut -d ' ' -f1 | rev`       # Take the name of the file
    if [ "$file" != "auto-version-commit.sh" ]; then    # Ignores himself
        if grep -q "__version__ = " $file; then     # Check version is in the file
            echo "Increasing version of file $file"
            oldver=`grep "__version__ = " $file`    # oldver=__version__ = 'z.y.x'
            verf1=`echo $oldver | cut -d '.' -f1`   # __version__ = 'z
            verf2=`echo $oldver | cut -d '.' -f2`   # y
            verf3=`echo $oldver | cut -d '.' -f3`   # x'
            oldnum=`echo $verf3 | sed 's/.$//'` # removes the quote from verf3
            newnum=`expr $oldnum + 1`       # x += 1
            newver="$verf1.$verf2.$newnum\'"    # joins string again.
            sed -i "s/$oldver\$/$newver/g" $file    # replaces line with the new one
        fi
    fi
done

git add -A
git commit -m $1
else
echo No commit message found, please add a message.
fi

I understand this code is HORRIBLE. I am not very good with Linux commands and shell scripting in general. Please, any correction to the code will be very much appreciated (And of course any alternative solution to this).I believe with a better understanding of Regex, this file can be significantly reduced.

J Agustin Barrachina
  • 3,501
  • 1
  • 32
  • 52