How to run a script (for version number generation) every time autotools is run - eclipse

I am using autotools/eclipse/linux.
I want to run a script to increment the build number in a header file every time I hit the build button. Do I add it in the Makefile.am? What is the syntax for this?

You can do it like this: add it to the all target so that it gets run every time, and declare it as .PHONY so that make doesn't try to relate it to an existing file.
all: update-build-number
.PHONY: update-build-number
update-build-number:
$(srcdir)/my_increment_script

This may be useful to others trying to do version control with git and automate their version numbering
Here is my number generator:
#!/bin/sh
#echo "Test version of version_script runs OK!"
majorversion=1
#echo "Commits"
#git rev-list HEAD
lastmerge=`git rev-list --merges HEAD | head -n1`
#echo "Last Merge"
#echo $lastmerge
#echo "Merges (Sub version)"
#git rev-list --merges HEAD
subvn=`git rev-list --merges HEAD | wc -l`
#echo $subvn
#echo "Commits+1 since last merge (Sub sub version)"
subsubvn=`git rev-list HEAD | grep -B99999 -e$lastmerge - | wc -l`
#echo $subsubvn
#echo "No merges"
#git rev-list --no-merges HEAD
#git rev-list --no-merges HEAD | wc -l
#echo $majorversion.$subvn.$subsubvn > versionnumfile
echo $majorversion.$subvn.$sub
Major version is hard coded (at the moment), sub version is number of merges, sub-sub version is number of commits (+1) since last merge

Related

Capturing (git) command output in github actions

I am trying to capture the output of this git command (or any for that matter).
git diff-tree --no-commit-id --patch-with-raw -r HEAD # HEAD or some commit SHA
However, unlike an echo, the following command does not log any output in the GitHub actions log. Nor does it streams the output to a variable. On my local, the same command logs the changes made in the last commit.
# result is empty
result=$(git diff-tree --no-commit-id --patch-with-raw -r HEAD)
What am I missing? How do I capture the output of the above git command?
Are you using the checkout action to checkout your code? git diff-tree probably doesn't output anything if you're not fetching the history. Try
- uses: actions/checkout#v2
with:
fetch-depth: 0
Probably something alike this... in every case with echo:
echo $(git diff-tree --no-commit-id --patch-with-raw -r HEAD)

Log all committed file

Was wondering how could I log, on the output, all the files and paths that were committed using the pre-push hook.
Of all the docs online I couldn't find an example on how to access the committed files list.
Does anyone know how to do it?
Thanks in advance
To anyone looking for the same thing, I've managed to do it by fetching all the files from a commit and iterating it
#!/bin/bash
echo "===================="
findMe="ROOT.war/"
replaceWith="webapps/ROOT/"
for file in $(git diff-tree --no-commit-id --name-only -r HEAD)
do
# echo "file: " $file
echo ${file//$findMe/$replaceWith}
done
echo "======================================="
exit 0

Is there a linter check to confirm that a related file has been updated in the commit?

I once saw (can't remember where) a lint check in a file that looked something like:
"If this file is modified, I will complain if files /somehwere/a and /somehwere/b aren't modified in the same commit".
Do you know of any linter capable of handling something like this? Maybe not a linter but something that can be integrated to a github repo?
If you have a CI system set up for your repository, you can run a command like the following, which will exit nonzero if there's a problem commit, and zero if all commits are fine (with $BASE and $HEAD set appropriately):
git rev-list $BASE..$HEAD | \
xargs -L1 sh -c 'lines=$(git show --name-only $0 | grep -e somewhere/a -e somewhere/b | wc -l); \
test $lines -ne 1 || { echo "bad commit $0"; false; }'
Any suitable CI system can test this by running a shell script and failing if it exits nonzero.

Why doesn't git diff work between two dates?

I checked out a project from an internal GitLab server using Eclipse, then I pulled all the changes. When I view the history from Eclipse. (Team > show in history), it displays the full history of the project.
Now I go to the relevant project from the terminal.
/home/workspace/ProjectX/
I am trying to get the differences between 2 dates with the following command:
git diff --name-only master#{2015-10-10}..master#{2015-11-10} > /home/results/ProjectX/Changes.txt
It wont display any result for that. It displays:
warning: Log for 'master' only goes back to Tue, 10 Nov 2015.
How can I get all the differences in that date range?
In addition to that, how does Eclipse request its history from the remote server. If we can run the same command from the terminal, that should work.
Git parses dates like master#{2015-10-10} using your reflog, which doesn't appear to go back as far as you're searching. But, you can find commits for that date range anyway with rev-list:
git rev-list --since='2015-10-10' --until='2015-11-10' master
You want the files changes between the most recent and the oldest commit in that list, which we can get using head and tail. I'd like to use -n1 and --reverse, but --reverse applies after -n, so we can't.
first=$(git rev-list --since='2015-10-10' --until='2015-11-10' master | tail -1)
last=$(git rev-list --since='2015-10-10' --until='2015-11-10' master | head -1)
git diff --name-only $first..$last
Setting variables and duplicating the rev-list feels clumsy, but the pipe-y version I can come up with is sort of worse. It picks the first and last commits, converts the newline to a space using tr, replaces the new space with .. using sed, then passes the pair off to git diff.
git rev-list --since='2015-10-10' --until='2015-11-10' master | \
(head -n1 && tail -n1) | \
tr '\n' ' ' | \
sed 's/ /../' | \
xargs git diff --name-only

Commit hash of the revision about to be deployed?

I want to add a hook that logs something to the effect of "Hey, I'm about to deploy such-and-such commit." Something like:
before "deploy:update_code" do
logger.info "Deploying #{revision}"
end
Except "revision" in this context seems to yield a ref name (i.e. "master") rather than a commit ID. What construct can I use to get the sha1?
To get the ref, you'll need to shell out to Git:
Here's an example from one of my own projects, where master is fully up-to-date and pushed, and my clean_architecture branch isn't.
~/api git:(clean_architecture) $ git show-ref master
349dabbffec0713ac0fc70cf991dbaff6412ad2b refs/heads/master
349dabbffec0713ac0fc70cf991dbaff6412ad2b refs/remotes/origin/master
~/api git:(clean_architecture) $ git show-ref clean_architecture
14afae560ace128a13336ca01ff2391b678fadaf refs/heads/clean_architecture
bc78906ad0b2814dbc6225b2e14155b66eedffd0 refs/remotes/origin/clean_architecture
Taking that on-board, I'd suggest something like the following to grab the remotely pushed ref hash (as that's the only one the Capistrano 3 can see, Capistrano will do a check like this internally, but you can't access the ref, and will complain if these two values differ, anyway)
First, on the command line:
$ git show-ref clean_architecture | tail -1 | cut -f1 -d ' '
bc78906ad0b2814dbc6225b2e14155b66eedffd0
$ git show-ref clean_architecture | tail -1 | awk '{print $1}'
bc78906ad0b2814dbc6225b2e14155b66eedffd0
(there's about a million ways to do this on linux)
Secondly in Ruby:
$ irb --simple-prompt
>> `git show-ref #{fetch(:branch)}`
=> "349dabbffec0713ac0fc70cf991dbaff6412ad2b refs/heads/master\n349dabbffec0713ac0fc70cf991dbaff6412ad2b refs/remotes/origin/master\n"
Which let's us know we can split this up really easily in Ruby land, and not need cut or awk:
$ irb --simple-prompt
>> `git show-ref #{fetch(:branch)}`.split.first
That should be pretty close, and pretty-portable (where as cut and awk, and splitting that up in the shell with pipes, etc is quite *nix specific and unlikely to work well on Windows)
Drop that in your before task, and you should be set.