views:

58

answers:

2

I'm trying to figure out how to work git filter-branch and I need help with some basic Linux scripting commands.

'git ls-files -s | sed "s-\t-&newsubdir/-" |
                GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                        git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

Can anyone break this down for me and explain each part?

I'm most interested in the '|' notation, the && notation, and the sed command.

+1  A: 

"|" is the unix pipe command which connects the command on the left sides output to the command on the right side's input.

"&&" is the unix "and" command which will execute the command on its right if and only if the command on it's left completes successfully

"sed" is the unix "stream editor" a powerful editing tool which parses and edits it's input

ennuikiller
+1  A: 

| connects stdout of the preceding command to stdin of the following command.

&& executes the following command if the return code of the preceding command is 0 (that is, the command succeeded).

The s command in sed is a substitution. It searches for a regex match for the first argument and replaces it with the second. The argument separator is traditionally /, but it will use the first character that follows, - in this case. The & in the replacement is replaced with the entire match.

Ignacio Vazquez-Abrams
Thanks!So just to clarify:The very first command will list the files, from that output sed will replace \t with "newsubdir" and then the output from sed will be set as the new $GIT_INDEX_FILE.new.
dirtytofu
Not quite. sed will replace `\t` with `\tnewsubdir`, then will pipe it into `git update-index --index-info`, and if that succeeds will rename a file. The prologue to `git update-index` is used to define an environment variable only for the execution of the command.
Ignacio Vazquez-Abrams
Won't the result of `sed` be piped to `GIT_INDEX_FILE=$GIT_INDEX_FILE.new` first before `git update-index --index-info`?
dirtytofu
No, because `GIT_INDEX_FILE=$GIT_INDEX_FILE.new` is not the command; it merely modifies the actual command that follows. The `\\` is used to continue the command on the next line (it "escapes the newline").
Ignacio Vazquez-Abrams
Thanks for the clarification!
dirtytofu