Log all committed file - atlassian-sourcetree

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

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)

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.

zsh: Find out VCS of current directory

I'd like to create a zsh widget to perform common tasks of a version control systems with a single button. E.g. pressing F1 should call "svn status", if the current directory is part of a Subversion checkout. If it is in a git repository, it should call "git status -s".
Now, creating the widget is no big deal. But how do I determine which VCS is in the current directory?
I know about vcs_info and I use a lot. But I couldn't find any way to retrieve the most basic information, it provides. Any ideas?
What about testing the existence of the specific meta data directories?
if test -d CVS; then
# CVS
elif test -d .hg; then
# Mercurial
elif test -d .git; then
# Git
elif test -d .svn; then
# Subversion
else
# unknown
fi
Thanks to all for comments. My solution now looks like this:
vcs-status() {
setopt no_autopushd
cmd=""
cur_dir=`pwd`
while [[ `pwd` != "/" ]]; do
if [[ -d .svn ]]; then
cmd="svn status^M"
break
elif [[ -d .git ]]; then
cmd="git status -s^M"
break
fi
cd ..
done
cd $cur_dir
if [[ -n $cmd ]]; then
zle -U $cmd
fi
setopt autopushd
}
zle -N vcs-status
bindkey "\eOP" vcs-status
Suggestions to cool zsh'ish improvements are welcome :-)
I also implemented the suggestion of Thomas, using svn info and git rev-parse. But then I noticed a problem: If I have an SVN working copy checked out somewhere inside a GIT working copy (yes, this is necessary occationally), or vice versa, I would only find the first one I'm checking. With the solution above, I find the "inner-most", which is what I want. In fact, this is a problem that also has been annoying me about vcs_info for a while.

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)