Capturing (git) command output in github actions - github

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)

Related

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

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

Mercurial show diff against 2 parents or base during merge

Our teem recently faced with merge that removes one leaf of merge and we "lost" changes (as if you perform hg merge --tool internal:local).
This happen because we don't experienced with hg merge command.
hg diff shown only one difference, but not other.
BASE --- HEAD1 --- MERGE
\---- HEAD2 --/
Suppose in HEAD1 I merge HEAD2 but has not yet commit changes.
HEAD2 diff against MERGE I see by hg diff. It is -r BASE:HEAD2 patch.
How can I see diff between current local merge state with HEAD1 as if we merge from HEAD2
How can I see diff between current local merge state with BASE?
Thanks #Vince for suggestion. I reread hg help diff and hg help revset and get that I want.
Assume that you at MERGE before commit and perform merge from HEAD1.
To compare diff against HEAD1 use one of:
hg diff
hg diff -r .
hg diff -r HEAD1
Check:
hg log -r .
To compare diff against HEAD1 use one of:
hg diff -r HEAD2
If you have only 2 heads in current branch last expression can be written without HEAD2 name:
hg diff -r 'branch(.) & head() - .'
Check:
hg log -r 'branch(.) & head() - .'
To compare against BASE:
hg log -r 'ancestor(HEAD1, HEAD2)'
If you have only 2 heads in current branch last expression can be written without HEAD1/HEAD2 names::
hg diff -r 'ancestor(branch(.) & head())'
Check:
hg log -r 'ancestor(branch(.) & head())'
I wander if there are any shortcut for second parent of current merge. For first - just dot sign...
UPDATE Hm... p1() and p2() are awesome! I rewrote my examples in way that they have no concrete names HEAD1/HEAD2:
hg diff -r 'p1()'
hg diff -r 'p2()'
hg diff -r 'ancestor(p1(), p2())'

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

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

How to find out what commit a checked out file came from

When I check out a file with git checkout $commit $filename and I forget $commit but still remember $filename, how do I find out what $commit was?
First a non-git answer. Check your shell command history. Well, if you didn't use a shell with command history then you don't...
The git answer. You generally cannot find THE $commit. Generally the same contents might have been part of many commits and I don't think git keeps a log of what single file you have checked out (it keeps a log of previous values of HEAD)
Here is a brute force script git-find-by-contents. Call it with your $filename as parameter, and it will show you all commits where this file was included. As the name says
it searches by contents. So it will find files with any name, as long as the contents matches.
#! /bin/sh
tmpdir=/tmp/$(basename $0)
mkdir $tmpdir 2>/dev/null
rm $tmpdir/* 2>/dev/null
hash=$(git hash-object $1)
echo "finding $hash"
allrevs=$(git rev-list --all)
# well, nearly all revs, we could still check the log if we have
# dangling commits and we could include the index to be perfect...
for rev in $allrevs
do
git ls-tree --full-tree -r $rev >$tmpdir/$rev
done
cd $tmpdir
grep $hash *
rm -r $tmpdir
I would not be surprised if there is a more elegant way, but this has worked for me a couple of times in similar situations.
EDIT: a more techy version of the same problem appears here: Which commit has this blob?
I don't think you can. Git just loads that version of the file into the index and your working dir. There is no reference keeping track of what version it loaded
If this is something that you do often, you could write up a script that could do it using git diff --cached and march through all the commits that changed that file.
You can use
git log <filename>
to find out which commit you want to checkout
If you're lucky, you could maybe try a non-git way:
history | grep "git checkout.*filename"
Try this (untested ;):
$ for commit in $(git log --format=%h $filename); do
> if diff <(git show $commit:$filename) $filename >/dev/null; then
> echo $commit
> fi
> done
Simple and elegant:
$ git log -S"$(cat /path/to/file)"
Only works if the content is unique, then again that's the same for the hash-comparison answers that came before.
It also displays only the first version that matches, rather than all.
Here are the details of a script I polished up as the answer to a similar question, and here you can see it in action:
(source: adamspiers.org)