Domanda

I have a problem similar to the one described in question How do I change the author of a commit in git? and the solution I'm trying is using git filter-branch but it doesn't work because it won't match the string literal with spaces i.e. I need contains:

git filter-branch --commit-filter '
    if [ "$GIT_AUTHOR_NAME" = "giovanni azua" ];
    then
            GIT_COMMITTER_NAME="Giovanni Azua";
            GIT_COMMITTER_EMAIL="giovanni.azua@xxx.com";
            GIT_AUTHOR_NAME="Giovanni Azua";
            GIT_AUTHOR_EMAIL="giovanni.azua@xxx.com";
            git commit-tree "$@";
    else
            git commit-tree "$@";
    fi' HEAD

The equal sign will not work if there are spaces in between and I have no idea how to escape it. I have also tried escaping it like giovanni\ azua but doesn't work. I also could not find any reference to what this scripting is i.e. how do you do string contains or substring match? I always get the output WARNING: Ref 'refs/heads/master' is unchanged.

UPDATE: It would also check my box to have a not if [ "$GIT_AUTHOR_NAME" ~= "giovanni azua" ]; but the operator ~= does not work.

È stato utile?

Soluzione

The scripting used by git filter-branch is simply a snippet of sh, which means that all capabilities of sh and any commands you can call from it (echo, grep, sed, etc.) are at your disposal. To implement wildcard matches, you don't need to invoke external commands, as sh provides the somewhat clumsy but useful case command. In your case (no pun intended), it could look like this:

git filter-branch --commit-filter '
    case "$GIT_AUTHOR_NAME" in
      *[Gg]iovanni*[Aa]zua*)
          GIT_COMMITTER_NAME="Giovanni Azua"
          GIT_COMMITTER_EMAIL="giovanni.azua@xxx.com"
          GIT_AUTHOR_NAME="Giovanni Azua"
          GIT_AUTHOR_EMAIL="giovanni.azua@xxx.com"
          git commit-tree "$@"
          ;;
      *)
          git commit-tree "$@"
          ;;
      esac
' HEAD

The above will change all commits whose author name contains giovanni or Giovanni and azua or Azua (in that order) anywhere in the author name.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top