How can I set up an alias for "git last" that accepts a number as an argument? - linux

In Pro Git Scott Chacon gives some nice examples of some alias which might be helpful, including one that shows the last commit: git last which is the equivalent of log -1 HEAD:
git config --global alias.last 'log -1 HEAD'
It will show something like this:
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <dreamer3#example.com>
Date: Tue Aug 26 19:48:51 2008 +0800
test for current head
I read a few similar questions on stack overflow like Pass an argument to a Git alias command but have not still not been able to figure this out.
The best I could come up with was to modify my .gitconfig file as follows:
[alias]
last = log -1 HEAD
mylast = "!sh -c 'echo /usr/local/bin/git last -$0 HEAD'"
Then if I run this at the command line:
$ git mylast 12
I get this:
/usr/local/bin/git last -12 HEAD
That actually looks right. But if I remove the echo in front, it just hangs like it is waiting for input. I tried switching $0 for $1 but that didn't seem to help either.
What am I doing wrong?
Also, is there a way to set it up so that if I just type git last with no number then it would default to "1" ?

last = !sh -c 'git log "-${1:-1}" HEAD' -
This takes advantage of the shell parameter interpolation default syntax, ${var:-default} which substitutes default if the variable var is not set.

Related

Get cloc (count lines of code ) difference between 2 commits by date Git

i tried the below command in git to get cloc difference between two commits using commit ids and i got the result in beautiful table.
cloc --git --diff <commit id1> <commit id2>
But i need a CLOC difference between commits but instead of using commit ids i need to use dates as a parameter to get the result.
Use git rev-list to get the commit SHAs you want for a particular date range.
For example, suppose you want to use the date range Jan 20, 2021 to Feb 20, 2021
branch=master
start_date=2021-01-20
end_date=2021-02-20
start_commit="$(git rev-list -n1 --before=${start_date} ${branch})"
end_commit="$(git rev-list -n1 --before=${end_date} ${branch})"
echo "Start commit for ${start_date} is ${start_commit}
echo "End commit for ${end_date} is ${end_commit}
Using this, you can plug $start_commit and $end_commit into your cloc command:
loc_count="$(cloc --git --diff ${start_commit} ${end_commit})"
echo "$loc_count"

Is there a way to use "git log --oneline" filtering by commit hash while keeping the branch names?

I was looking for a way to highlight a specific commit hash when using git log --oneline, and I managed to do that by using:
# consider that 000000000 is the first 9 digits of the commit hash
git log --oneline | grep --color=always -E '^|000000000' | less -R
This actually works in a very similar way to simply git log --oneline and it indeed highlights the commit 000000000. The only problem though, is that it ends up losing all the information regarding my branches that git log --oneline gives me.
Examples:
# input:
git log --oneline
# output:
000000000 (myRemote/myBranch) my commit message
# input:
git log --oneline | grep --color=always -E '^|000000000' | less -R
# output:
000000000 my commit message
While the latter example comes with a highlighted 000000000, it lacks the (myRemote/myBranch) information.
So, is there a way to modify the input I'm using so that I can get both the highlight and branch info?
You can add the flag --decorate to your log, it'll do the job, I just tried it (git version 2.21.0.windows.1).
Optionnally, you might want to make an alias to which you pass the hash as a parameter, for convenience :
git config --global alias.find '!f() { git log --oneline --decorate | grep --color=always -E "(^|${1})"; }; f'
...and then when you search for commit deadbea7dad, you just type
git find deadbea7dad

Handling expanded git commands with python subprocess module

I'm trying to retrieve and work with data from historical versions of files in a git repo. I'd like to have something like a dictionary that holds <hash>, <time of commit>, <value retrieved from contents of a file revision>, <commit message> for each entry.
I figured the data I retrieve from each file revision, and any calculations done with them, would be best handled using python. And the subprocess module appeared to be the best fit to integrate my git commands.
Below I show how I'm defining a function getval(key, filename) that I had hoped would output <SHA-1 hash>:<Value> to console, but would like to have a dict with more info... also with <time>, and <commit message>.
I help operate an ion accelerator, where we store 'savesets'--or values relevant to a given accelerator tune--using git. Of the values in these files, are things like charge(Q) and mass(A). Ultimately, I want to retrieve both values, get the ratio (Q/A), and display a list of file revision hashes sorted by the charge:mass ratio of the ion we delivered with the settings in that file's revision.
Sample of file (for 56Fe17+):
# Date: 2018-12-21 01:49:16.888
PV,SELECTED,TIMESTAMP,STATUS,SEVERITY,VALUE_TYPE,VALUE,READBACK,READBACK_VALUE,DELTA,READ_ONLY
REA_EXP:LINE,0,1544047322.881066957,NO_ALARM,NONE,enum,"JENSA~[UDF;AT-TPC;GPL;JENSA]",,"---",,true
REA_BTS19:BEAM:OPTICSFILE,0,1541798820.065952460,NO_ALARM,NONE,string,"BTS19_test3.data",,"---",,true
REA_BTS19:BEAM:A_BOOK,0,1545322510.562031883,NO_ALARM,NONE,double,"56.0",,"---",,true
REA_BTS19:BEAM:Z_BOOK,0,1545322567.544226340,NO_ALARM,NONE,double,"26.0",,"---",,true
REA_BTS19:BEAM:Q_BOOK,0,1545322512.701768974,NO_ALARM,NONE,double,"17.0",,"---",,true
So far--and with the help of others here--I've figured out a git one-liner that greps the revision history of a given file for a key[a string] and uses sed and awk to output <hash>:<val associated with the key>.
Git Oneliner I'm Starting with:
git grep 'BTS19:BEAM:A_BOOK' $(git rev-list --all) -- ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp | sed 's/:/,/' | awk -F, '{print $1 ":" $8}'
Oneliner's Output
e78f73fe6f90e93d5b3ccf90975b0e540d12ce09:"56.0"
4b94745bd0a6594bb42a774c95b5fc0847ef2d82:"56.0"
f2d5e263deac1d9112be791b39f4ce1b1b34e55d:"56.0"
c03800de52143ddb2abfab51fcc665ff5470e363:"56.0"
4a3a564a6d87bc6ff5f3dc7fec7670aeecfe6a79:"58.0"
d591941e51c4eab1237ce726a2a49448114b8f26:"58.0"
a9c8f5cdf224ff4fd94514c33888796760afd792:"58.0"
2f221492beea1663216dcfb27da89343817b11fd:"58.0"
I've also started playing with the subprocess python module. But I'm struggling to figure out how to handle my more complicated git commands. Generally, I'll want to be able to pass a key, and a file.. something like getval(key, filename).
When my cmd string was ['git', 'grep', str, '$(git rev-list --all)', '--', pathspec], it returned errors stating that '$(git rev-list --all)' was ambiguous. Thinking it wasn't being expanded, I added a separate process to execute the nested command, but I'm not sure I'm doing this correctly.
My Python file (gitfun.py): which I'm currently running the function from
import sys, os
import subprocess
def getval(str, pathspec, repoDir='/mnt/d/stash.projects/rea'):
p1 = subprocess.Popen(["git", "rev-list", "--all"], stdout=subprocess.PIPE)
output, err = p1.communicate()
cmd = ['git', 'grep', str, output, '--', pathspec]
p2 = subprocess.Popen(cmd, cwd=repoDir)
p2.wait()
cwd = '/mnt/d/stash.projects/rea'
filename = 'ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp'
os.chdir(cwd)
getval('BTS19:BEAM:A_BOOK', filename)
Currently it is returning 'file name too long' so (even though I'm not convinced it really is too long) I tried changing my core.longpaths in git config to true, however this had no effect. Again why I suspect I'm not handling my replacement of the $(git rev-list --all) expansion correctly.
For this code, I expect something that looks like this:
522628b8d3db01ac330240b28935933b0448649c:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185
5,NO_ALARM,NONE,double,"58.0",,"---",,true
2557c599d2dc67d80ffc5b9be3f79899e0c15a10:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185
5,NO_ALARM,NONE,double,"58.0",,"---",,true
7fc97ec2aa76f32265196c42dbcd289c49f0ad93:ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp:REA_BTS19:BEAM:A_BOOK,0,1545240215.74320185
5,NO_ALARM,NONE,double,"58.0",,"---",,true
...
But I ultimately want an output to console that looks identical to the git one-liner above, or better yet, a dict that I can print to console or do other things with.
Remember that your shell tokenizes the command line using white space.
When you run git rev-list --all, you get output like:
2a4be2748fad885f88163a5b9b1b438fe3cb2ece
c1a30c743eb810fbefe1dc314277931fa33842b3
b2e5c75131e94a3543e5dcf9fb641ccd553906b4
95718f7e128a8b36ca93d6589328cc5b739668b1
87a9ada188a8cd1c13e48c21f093be7027d61eca
When you substitute that into your git grep command...
git grep 'BTS19:BEAM:A_BOOK' $(git rev-list --all) -- \
ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp
...each line is a separate argument. That is, if the output of git rev-list --all was exactly what I've shown above, then your one-liner would be tokenized into the following arguments, which I have listed one per line for clarity:
git
grep
BTS19:BEAM:A_BOOK
2a4be2748fad885f88163a5b9b1b438fe3cb2ece
c1a30c743eb810fbefe1dc314277931fa33842b3
b2e5c75131e94a3543e5dcf9fb641ccd553906b4
95718f7e128a8b36ca93d6589328cc5b739668b1
87a9ada188a8cd1c13e48c21f093be7027d61eca
--
ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp
But you're not doing this in your Python code! You're pasing the entire output of git rev-list --all as a single argument. That means the command you're trying to execute has a fixed number (6) of arguments:
git
grep
BTS19:BEAM:A_BOOK
2a4be2748fad885f88163a5b9b1b438fe3cb2ece c1a30c743eb810fbefe1dc314277931fa33842b3 b2e5c75131e94a3543e5dcf9fb641ccd553906b4 95718f7e128a8b36ca93d6589328cc5b739668b1 87a9ada188a8cd1c13e48c21f093be7027d61eca
--
ReAccelerator/Snapshots/RFQ-JENSA_Setpoints.snp
All those revisions are getting bundled together in a single argument, which is where the "filename too long" error comes from. You need to split that output into multiple arguments just like the shell does:
p1 = subprocess.Popen(["git", "rev-list", "--all"], stdout=subprocess.PIPE)
output, err = p1.communicate()
cmd = ['git', 'grep', str] + output.splitlines() + ['--', pathspec]
p2 = subprocess.Popen(cmd, cwd=repoDir)
p2.wait()

Git aliases - command line autocompletion of branch names

If I run a regular git command such as git checkout I get helpful autocompletion of branch names when hitting the tab key.
I have a few git aliases which take branch names as parameters, and I'm wondering if there's a way of getting the branch name autocompletion to work with them?
Edit:
Just to provide some clarification from the discussion in the comments, aliases with a direct mapping work fine, i.e.:
ci = commit
co = checkout
It's ones that are a bit more involved and use $1 as a parameter that don't, for example:
tagarchive = !f() { git tag archive/$1 origin/$1 && git push origin :$1 && git push origin archive/$1 && git branch -d $1; }; f
For git aliases, the autocomplete function for the git command (__git()) uses a call to git config --get "alias.$1" to determine that equivalent autocomplete function. This works for simple mappings but will choke on more complex aliases.
To get around this, define an autocomplete function with a name that matches your alias, i.e. _git_tagarchive(). The autocomplete function for git should pick that up and use it for autocompletion.
For example:
[me#home]$ git tagarchive <TAB><TAB>
AUTHORS gentleSelect/ .gitignore LICENSE test_multiple.html
cron/ .git/ index.html README.md
[me#home]$ _git_tagarchive() {
> _git_branch # reuse that of git branch
> }
[me#home]$ git tagarchive <TAB><TAB>
enable_multiple master origin/gh-pages v0.1 v0.1.3
FETCH_HEAD ORIG_HEAD origin/HEAD v0.1.1 v0.1.3.1
HEAD origin/enable_multiple origin/master v0.1.2
For a more permanent solution simply add the function definition to your bashrc file. Eg:
_git_tagarchive()
{
_git_branch
}
Note that I've simply reused the autocomplete function for git branch; you may wish to change this to something more suitable or write your own.
More info
This solution was identified based on an exploration of /etc/bash_completion.d/git.
Typically, aliased git commands are handled by the __git_aliased_commands() function which parses the output of git config --get "alias.$1" to decide on the autocomplete function to use. Using a more complex shell command as the alias target would understandably foil this approach.
Looking further, it appears the autocomplete function for git (_git()) chains in autocomplete function for subcommands by simple prepending the function with _git_ (with dashes (-) in the command replaced by underscores). This is done before __git_aliased_command() is checked so this is something we could use.
_git ()
{
# .....
local completion_func="_git_${command//-/_}"
declare -f $completion_func >/dev/null && $completion_func && return
local expansion=$(__git_aliased_command "$command")
if [ -n "$expansion" ]; then
completion_func="_git_${expansion//-/_}"
declare -f $completion_func >/dev/null && $completion_func
fi
}
The approach I've gone for is therefore to ensure that a function that matches your alias exists, i.e. _git_tagarchive().
Update July 2015 (Git 2.5): getting the git aliases is easier in contrib/completion/git-completion.bash.
See commit 12bdc88, commit e8f9e42 (10 May 2015) by SZEDER Gábor (szeder).
(Merged by Junio C Hamano -- gitster -- in commit 935d937, 22 May 2015)
~~~~~~~~~~~~~~
Git completion should work better with complex aliases in Git 2.1 (August 2014).
See commit 56f24e8 by Steffen Prohaska (sprohaska)
completion: handle '!f() { ... }; f' and "!sh -c '...' -" aliases
'!f() { ... }; f' and "!sh -c '....' -" are recommended patterns for declaring more complex aliases (see git wiki).
This commit teaches the completion to handle them.
When determining which completion to use for an alias, an opening brace or single quote is now skipped, and the search for a git command is continued.
For example, the aliases '!f() { git commit ... }' or "!sh -c 'git commit ...'" now trigger commit completion.
Previously, the search stopped on the opening brace or quote, and the completion tried
it to determine how to complete, which obviously was useless.
The null command ':' is now skipped, so that it can be used as a workaround to declare the desired completion style.
For example, the aliases:
!f() { : git commit ; if ... } f
!sh -c ': git commit; if ...' -
now trigger commit completion.
Shell function declarations now work with or without space before the parens, i.e. '!f() ...' and '!f () ...' both work.

How can I find the svn diff between working copy and arbitrary rev?

I'm trying to write a multi-file patch for an open-source project, but the master copy has changed since I started working. I need to get the SVN difference (just the files under version control) between my uncommitted version and the revision from which it was checked. Which SVN command can I use to find the difference?
Edit: I'm sorry, I must have been using the term "working copy" improperly. I need to compare my uncommitted changes to the revision off which they are based. In other words, I checked out revision 1000 and changed files foo and bar. The rev number is now up to 1015, but I need to compare my version of foo and bar to the version of revision 1000. Is there an easy command to do this (compare my altered copy of a program with a past revision)?
You can use -rN:M parameter with diff command which specifies the revisions you want to compare. Just provide revision from which your working copy was checked out (you can omit M as it defaults to working copy) and you should get what you need.
If you don't remember the original revision number try to run svn status -v and first column should show it.
More info svn help diff...
svn diff takes a -rN:M argument which defaults to N == BASE and M == working copy. Will svn diff -r REV where REV is the revision you want not work?
To answer your edit, suppose you have the following:
$ ls
foo bar baz
$ svn st -u
Status against revision: 1071
$ echo "more stuff" >> foo
$ svn diff -r 1000 foo
Index: foo
===================================================================
--- foo (revision 1000)
+++ foo (working copy)
...
I believe this is what you are after, yes?
If your goal was to get a report of just the filenames where the contents has changed, this should do the trick:
svn diff | grep 'Index: ' .

Resources