Git Log in Powershell - powershell

My command is to pull log from repository based on a previous tag till the latest HEAD. Below is the command i run in powershell
git log 10.01.39.000..head --oneline --name-only --pretty=format:
The same when i try to do it by substituting a variable in place of the tag name does not give me the output. Should the .. be escaped in powershell? Powershell is considering the ..Head as a method for the variable.
git log $tag..head --oneline --name-only --pretty=format:

In this case, you want to:
Force expansion of $tag before passing the argument to git
Prevent evaluation of the .. operator
The easiest way to do this is to construct an expandable string literal using ":
git log "$tag..head" --oneline --name-only --pretty=format:...

Related

How to show a diff for a given SVN revision

I am using git-svn and I would like to show the changes that were introduced by a certain SVN revision.
How to do that?
Manual way
Convert the revision number to a Git hash:
git svn find-rev r1234
Show a diff to the previous commit (replace thehash with the output from previous command):
git diff thehash^..thehash
Using git alias
You can get it all in one with this git alias.
Edit ~/.gitconfig (section [alias]), by adding the line
sdiff = "!f() { r=r"$(echo $1 | tr -d 'r')"; h="$(git svn find-rev $r)"; echo $r=${h}; git diff ${h}^..${h} ; } ; f ${1}"
Get the diff for a specific revision by using either of the following syntaxes:
git sdiff 1234
git sdiff r1234
Notice how the alias also prints the correspondence between revision number and hash. If you don't like that, remove the part echo $r=${h};

Azure Pipelines Get Latest files only

I'm looking for a way to create an artifact that I can attach to a deployment pipeline that only contains the files that were changed in the commits that triggered this build.
What I have is a repo that has change scripts for database objects, so I want to package up only the change scripts for the last commit into a zip file and attach it to the build outputs. That way I can take zip file and apply each of the files on top of the database, this will be done later in a different step, right now I'm just trying to get all of the files that were changed.
Editted
I have created the following step in the YAML file based on the comments below
- powershell: |
#get the changed template
echo "git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)"
$a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)
#assign the filename to a variable
echo "Files"
echo "##vso[task.setvariable variable=fileName]$a"
- powershell: |
#Print Files
$fileName: echo "$env:fileName"
Below is the result, you can see that no files are changed. Here I changed the Readme file, which triggered the build.
Not sure if this would help you, but hopefully will point in the right direction.
Assuming you have Git as source control. Have you considered to query those changes using Git instead? (I haven't tried, but I bet you'll be able to find in the pipeline metadata which merge triggered the build, and then use it to query Git for the file changes.
Have a look at this question in Stackoverflow
Hope it will help.
If you are using Git version control, you could try to add a script task to get the changed file names in your pipeline, copy them to artifact directory and then publish them.
It is easy to get the changed files using git commands git diff-tree --no-commit-id --name-only -r commitId. When you get the changed file's name, you need to assign it to a variable using expression ##vso[task.setvariable variable=VariableName]value. Then you can use this variable in the copy and publish task.
You can check below yaml pipeline for example:
- powershell: |
#get the changed template
$a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)
#assign the filename to a variable
echo "##vso[task.setvariable variable=fileName]$a"
- powershell: |
echo "$env:fileName"

How can I git add, commit and push in one line with multiple specified file?

I want to combine the git push command to one line, such as gitpush -m "fix bug #123" -f file1.html, file2.html ...
only file1.html and file2.html will commit and update to the server
how to make it?
try adding the following function to your .bashrc (or .bash_profile if Mac):
function allinone() {
for i in ${#:2}
do
git add $i
done
git commit -a -m "$1"
git push
}
Then you just can add, commit and push typing:
allinone "Adding, commiting and pushing 3 files at once" file1.html file2.html file3.html
The first parameter must be the comment, and the rest of the parameters are the files that you want to add, commit and push (notice that files are separated by spaces)
I hope this helps!

SCM environment variables missing

Usually, when using SCM like the Git Plugin, there are a bunch of environment variables that you can use (e.g. see these)
But neither the Git Step nor the Generic SCM seem to do that.
Is there a way to get these variables into the groovy env.* so that they can be used?
Something like this would be useful:
def commitMessage = sh 'git log --max-count=1 --oneline --no-merges | cut -b9-'
I can think of writing the results to a file and read them via the readFile() mehtod -- but is there an easier way to achieve this?
For the Record: I have the following code to get the branch-name:
stage 'preparation'
node {
// checkout branch
git branch: 'origin/master', url: 'git#example.net:project.git'
// write current branch-name to file
sh 'git branch -a --contains `git rev-parse HEAD` | grep origin | sed \'s!\\s*remotes/origin/\\(.*\\)!\\1!\' > git-branch.txt'
// read data from file into environment-variable
env.gitBranch = readFile('git-branch.txt').trim()
// let people know what's up
echo "testing branch ${env.gitBranch}"
}
The remainder of the flow-script is comprised of serveral parametrized jobs which get the env.gitBranch passed as parameter (among others, if needed).
Be sure to allow concurrent builds for the workflow to catch every updated branch.
See JENKINS-24141; these variables are not yet available from Workflow.
In the meantime, you are on the right track: run a git command to record any information you need, and use readFile to load it (see also JENKINS-26133).

Referring to the current bookmark in mercurial

I am using bookmarks in mercurial to emulate a git branches-like workflow.
One thing I'm finding is that whenever I push, I invariably want to push just the current bookmark. Rather than typing
hg push -B <bookmark_name>
all the time, I'd like to alias hg push to just push the current bookmark. To do that, I need a way of referring to the current bookmark without mentioning its name. Is there a way to do that?
I understand it was asked two years ago, but I found this page in Google and none of the answers helped. So here's what did the trick for me (Linux):
[alias]
currentbranch = !cd `$HG root` && cat .hg/bookmarks.current
pushb = !$HG push -B `$HG currentbranch`
cd is required for this to work from non-root directories.
Current bookmark name stored in .hg/bookmarks.current file of your repository. As an alias you can use something like this:
pushb = push -B `cat .hg/bookmarks.current`
Also note that when you update you repository state to any other revision, there won't be file .hg/bookmarks.current.
OK, platform independent solution, somehow ugly
pushb = push -B `hg log --template "{bookmarks}\n" -r "bookmark() & ."`
or, with nested command in more natural way it must be: hg parents --template="{bookmarks}\n"
Ugly because pure Mercurial-way using nested shell-aliases in hgrc
[alias]
cb = !$HG log --template "{bookmarks}\n" -r "bookmark() & ."
pushb = push -B cb
does not work for me
>hg pushb
...
bookmark cb does not exist on the local or remote repository!
Edit
Long time later, with new solution. According to hg help push
If -B/--bookmark is used, the specified bookmarked revision, its
ancestors, and the bookmark will be pushed to the remote repository.
Specifying "." is equivalent to specifying the active bookmark's name.
if order to "push the current bookmark" you can use just
hg push -B .
This works for me (and handles bookmarks with spaces):
alias pushb="hg push -B \"\$(hg bookmarks 2> /dev/null | awk '/\*/ { \$1=\"\"; \$NF=\"\"; printf }' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
You can use a shell call substitution:
hg push -B `hg bookmark --active`
This can be put into an alias (see alias section of hg help config)