Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git run shell command for each commit

Tags:

git

shell

I would like to walk through a range of commits and perform a shell command on each. If the command fails, I would like the walk to stop, otherwise keep going. I have looked at filter-branch, but I don't want to re-write the commits, just check them out. for-each-ref does not seem to allow you to specify a range to act on.

My specific problem is that I created a bunch of commits and I would like to ensure that each commit is buildable. I would like to do something like:

git foreach origin/master..master 'git submodule update && make clean && make' 

I could of course write a shell script to do this, but it seems like the sort of thing that git might have a nice way to do.

like image 808
FazJaxton Avatar asked Nov 17 '14 23:11

FazJaxton


People also ask

How do you see the details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How do you jump to a commit?

To jump back to a previous commit, first find the commit's hash using git log . This places you at commit 789abcd . You can now make new commits on top of this old commit without affecting the branch your head is on. Any changes can be made into a proper branch using either branch or checkout -b .

What are git commit commands?

The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.


2 Answers

You can use interactive rebase with an exec option.

git rebase -i --exec <build command> <first sha you want to test>~ 

--exec Append "exec " after each line creating a commit in the final history. will be interpreted as one or more shell commands.

Reordering and editing commits usually creates untested intermediate steps. You may want to check that your history editing did not break anything by running a test, or at least recompiling at intermediate points in history by using the "exec" command (shortcut "x").

The interactive rebase will stop when a command fails (i.e. exits with non-0 status) to give you an opportunity to fix the problem.

like image 55
Andrew C Avatar answered Sep 19 '22 13:09

Andrew C


You probably want rev-list.

#!/usr/bin/env bash # test_commits.sh  while read -r rev; do     git checkout "$rev"     if ! git submodule update && make clean && make; then         >&2 echo "Commit $rev failed"         exit 1     fi done < <(git rev-list "$1") 

Then you can use it with

./test_commits.sh origin/master..master 
like image 26
Justin Howard Avatar answered Sep 20 '22 13:09

Justin Howard