1

I want to construct and execute the following git log statement to get summary of commits for yesterday for specific authors: name1 and name2, so the final desired executed git log should be as follows:

git log --author name1 --author name2 
        --since yesterday --reverse
        --pretty=format:"%h - %an , %ad : %s" > commits.txt

As you may notice output is redirected to commits.txt file.

The output commits.txt file should finally contain some text like this:

350411e - name1, Mon Apr 18 18:03:01 2016 +0200 : Fix bug X
366411e - name2, Mon Apr 18 18:05:06 2016 +0200 : Introduce feature Y
752411e - name1, Mon Apr 18 18:11:12 2016 +0200 : Merge version Z

Here's my code in script.sh

team=( name1 name2 )

function create_commits_log_file () {
    authors=''
    for author in "${@}"
    do
        authors="$authors --author $author "
    done
    git_log_statement="git log $authors --since yesterday --reverse --pretty=format:\"%h - %an , %ad : %s\" > commits.txt"
    echo "$git_log_statement"
    $git_log_statement
}

create_commits_log_file "${team[@]}"

Unfortunately when I run it as bash script.sh, it throws an error:

git log --author name1 --author name2 --since yesterday --reverse --pretty=format:"%h - %an , %ad : %s" > commits.txt

fatal: ambiguous argument '%an': unknown revision or path not in the working tree.

Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'

Despite that if I run the echo'ed git command on console it works perfectly!

What is the problem ? and how may I fix it ?


UPDATE - Attempt:

Based on comments, I also tried to execute the git log command directly without storing it in a variable as follows :

team=( ashraf.bashir diego.palacios )

function create_commits_log_file () {
    authors=''
    for author in "${@}"
    do
        authors="$authors --author $author "
    done
    git log $authors --since yesterday --reverse --pretty=format:\"%h - %an , %ad : %s\" >commits.txt
}

create_commits_log_file "${team[@]}"

But I am still getting the same error

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82

1 Answers1

1

This is one of those cases in which we hit what is described in I'm trying to put a command in a variable, but the complex cases always fail!: you store a command in a variable and when you try to execute it, fails.

Instead of saying

var=command   # store command
$var          # execute it

Just execute it:

command

In your case, instead of

git_log_statement="git log $authors --since yesterday --reverse --pretty=format:\"%h - %an , %ad : %s\" > commits.txt"
$git_log_statement

Just say.

git log $authors --since yesterday --reverse --pretty=format:"%h - %an , %ad : %s" > commits.txt

I tested it locally and it worked well.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Same error: fatal: ambiguous argument '%an': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]', I updated the question with this attempt – Ashraf Bashir Apr 19 '16 at 11:44
  • 1
    @AshrafBashir my fault. I missed unescaping those `"`. See my updated attempt using `--pretty=format:"%h - %an , %ad : %s"` instead. – fedorqui Apr 19 '16 at 12:29