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

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"

Related

How can I get the last merged branch name in git

How can i get the last merged branch name in git from the remote
Tried
git log --first-parent --merges -1 --oneline
But not getting the branch name
Please help
In general, you cannot.
A merge commit may have, as its commit message, text of the form merge branch foo or merge branch foo of someurl, but to read that message, you must obtain the commit from the remote. Even so, there's no guarantee that branch foo exists any more, or that the name means anything if it does exist. Merging really works by commit hash IDs, not by branch names; branch names may evanesce.
If you think you need a branch name here, you are probably doing something wrong. Branch names do make sense here in other version control systems, but in Git, depending on them is unwise.
Here is the command you need to give (change the branch name from origin/master to whichever branch you're checking merges for):
git log --merges origin/master --oneline --grep='^Merge' -1 | grep -oe "[^']*[^']" | sed -n 2p
I had quite a bit of a hard time trying to solve this issue.
I had to get this information for my CircleCi pipeline, and to solve my problem I used the GitHub cli.
Specifically the pr list command: https://cli.github.com/manual/gh_pr_list
The following command gives you all the information of the last PR you merged into main
gh pr list -s merged -B main -L 1
Then you need to manipulate the string and get only the branch name. Which is the text before "MERGED"
I took it splitting the string and taking the penultimate element.

how to use "--no-ff --no-commit" using GitPython while merging?

When i use commandline git merge with --no-ff --no-commit works fine. But using GitPython i am unable to dos.
May be i am not finding a correct documentation or forums for the below equivalent operation using GitPython.
Am i missing any ?
command line:
git merge --no-ff --no-commit "<TAG Name> or <Feature branch>"
GitPython:
print(f"Merging from tag '{input_tag}' into 'master'...")
repo = Repo(constants.git_clone_local_path)
repo.git.checkout('master')
repo.git.pull()
repo.git.merge(f'--no-ff --no-commit {input_tag}')
# repo.git.merge(f'{input_tag}', no_ff=True)
print(repo.git.status())
print('Done')
Python gives below error
cmdline: git merge --no-ff --no-commit test_tag
stderr: 'error: unknown option `no-ff --no-commit test_tag'
usage: git merge [<options>] [<commit>...]
or: git merge --abort
or: git merge --continue
-n do not show a diffstat at the end of the merge
--stat show a diffstat at the end of the merge
--summary (synonym to --stat)
--log[=<n>] add (at most <n>) entries from shortlog to merge commit message
--squash create a single commit instead of doing a merge
--commit perform a commit if the merge succeeds (default)
-e, --edit edit message before committing
--cleanup <mode> how to strip spaces and #comments from message
--ff allow fast-forward (default)
--ff-only abort if fast-forward is not possible
--rerere-autoupdate update the index with reused conflict resolution if possible
--verify-signatures verify that the named commit has a valid GPG signature
-s, --strategy <strategy>
merge strategy to use
-X, --strategy-option <option=value>
option for selected merge strategy
-m, --message <message>
merge commit message (for a non-fast-forward merge)
-F, --file <path> read message from file
-v, --verbose be more verbose
-q, --quiet be more quiet
--abort abort the current in-progress merge
--quit --abort but leave index and working tree alone
--continue continue the current in-progress merge
--allow-unrelated-histories
allow merging unrelated histories
--progress force progress reporting
-S, --gpg-sign[=<key-id>]
GPG sign commit
--overwrite-ignore update ignored files (default)
--signoff add Signed-off-by:
--verify verify commit-msg hook
This should work
git.merge('args', no_ff=True)
replace 'args' by yours arguments like your branch...
I didn't find any links in the documentation. But this code worked for me.
repo = Repo(constants.git_clone_local_path)
repo.git.merge(<source_branch>, no_ff=True, no_commit=True)

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

Git command to get SVN style diff; modified file list between revisions

I used the command "git diff --name-only" and got output like below
FilePath/FilePath/FileName.ext
FilePath/FilePath1/FileName1.ext
I need SVN style output like
M FilePath/FilePath/FileName.ext
A FilePath/FilePath1/FileName1.ext
How to achive this?
Try the "--name-status" option instead of "--name-only":
git diff --name-status
Example output:
git diff --name-status HEAD~1 HEAD
M .gitignore
A src/File1.java
A src/File2.java
M src/File3.java

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

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.

Resources