-1

bash is sending me over the edge again.

This:

echo $desc $commit_string

Prints this:

description "tmux split-window subcommand.md"

But this, run from a script (or even from the command line):

git commit -m "$desc" "$commit_string"

Results in this:

error: pathspec '"tmux split-window subcommand.md"' did not match any file(s) known to git

But this, run from the directory the file is in, works fine:

git commit -m 'description' "tmux split-window subcommand.md"

I've spent at least an hour on this and have tried everything under the sun.

StevieD
  • 6,925
  • 2
  • 25
  • 45

3 Answers3

2

In git commit -m "$desc" "$commit_string", because of the space between "$desc" and "$commit_string", "$commit_string" is interpreted as a path parameter.

Try

git commit -m "$desc"\ "$commit_string"

or

git commit -m "$desc $commit_string"

The parameter name $commit_string suggests it's a commit message. But after reading your comments, I find out that it also includes the paths of the changed files.

So if tmux split-window belongs to the message, and subcommand.md belongs to the path, you need to split them. Compose tmux split-window with -m and leave subcommand.md alone.

If tmux split-window subcommand.md all belongs to paths, try:

git commit -m "$desc" -- "$commit_string"
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Tried both of those. The commit doesn't happen. I get "Changes not staged for commit...no changes added to commit." – StevieD Apr 17 '19 at 04:24
  • @StevieD then either you have committed before or no new changes have been added. Run `git log` to see if you've already made a commit. – ElpieKay Apr 17 '19 at 04:27
  • git reports the file has been modified. It definitely was not committed. – StevieD Apr 17 '19 at 04:28
  • 2
    @StevieD have the changes been added? If not, nothing is going to be committed. – ElpieKay Apr 17 '19 at 04:30
  • That is not correct. You do not have to add a file that has already been added to the git repository every time you make a commit. – StevieD Apr 17 '19 at 04:32
  • 2
    @StevieD When a file is modified, and you want git to record the changes, you need to run `git add` first, and then `git commit`. Imagine that a modified file is different from the currently tracked file. You could consider it as a new file. – ElpieKay Apr 17 '19 at 04:35
  • 2
    @StevieD - @ElpieKay is completely right, Read the docs https://git-scm.com/docs/git-add `Thus after making any changes to the working tree, and before running the commit command, you must use the add command to add any new or modified files to the index.` – Yuri G. Apr 17 '19 at 04:35
  • `git commit -am "$desc $commit_string"` (note the extra `a` in `-am`) – David C. Rankin Apr 17 '19 at 04:37
  • I'll bet you guys $100,000 you are wrong. I do it all the time. You aren't interpreting the docs correctly. – StevieD Apr 17 '19 at 04:39
  • 1
    @StevieD Maybe, as David's comment says, you always use `git commit -am`. `-a` is almost a shortcut for `git add`. – ElpieKay Apr 17 '19 at 04:40
  • No, I don't use that. I'm telling you, you don't have to add the file every time you make a commit. You just have to add it to repo once. – StevieD Apr 17 '19 at 04:41
  • 2
    @StevieD If the file is modified and you want git to track the changes, you need to add it again. If the file is not modified, then you don't need to add it again. If the file is modified and you don't want git to track the changes, you don't need to add it again. Or maybe you are not using the command line. Some GUI tools automatically add the files, which is equivalent to `git commit -am`. – ElpieKay Apr 17 '19 at 04:44
  • Not correct. Changes are still tracked. I do it all the time. See item 3 on https://git-scm.com/docs/git-commit. One of the ways to commit a file is to add it as an argument to commit after the options. – StevieD Apr 17 '19 at 04:56
  • @StevieD I know where the divergence is. I'll update my answer. – ElpieKay Apr 17 '19 at 05:00
  • The $commit_string is the name of the file I'm trying to commit. – StevieD Apr 17 '19 at 05:14
  • Sorry, it's a poorly named variable. It gets generated by a bash function for me. The string, representing a file, contains actual double quotes. – StevieD Apr 17 '19 at 05:15
  • @StevieD put `--` between `"$desc"` and `"$commit_string"`, so that the paths would be interpreted correctly and separately. – ElpieKay Apr 17 '19 at 05:15
  • Same error. I think I just have to pull the double quotes out of the file name. Because when I do `git commit -m 'blah' "\"tmux split-window subcommand.md\""` I get the same exact error. Is there an easy way to do that with bash? – StevieD Apr 17 '19 at 05:23
  • @StevieD how about `git commit -m "$desc" $commit_string`? – ElpieKay Apr 17 '19 at 05:28
  • OK, posted answer. Thanks for your time and helping me work though this. I appreciate. – StevieD Apr 17 '19 at 05:29
  • When I did that, bash split up the $commit_string into three lines. I forget why bash does that. I just know it's confusing. – StevieD Apr 17 '19 at 05:34
0

I believe 'tmux split-window subcommand.md' is the name of the file you want to commit. If that variable already includes quotes, apparently it does, then you might use

git commit -m "$desc" $commit_string

Update: How is that variable defined, commit_string? Did you put those quotes yourself? You said echo $desc $commit_string prints description "tmux split-window subcommand.md". Maybe you should declare it as commit_string="tmux split-window subcommand.md" then use git -m "$desc" "$commit_string".

D. Jones
  • 461
  • 2
  • 16
  • Tried that. Here's the the thing. The file has double quotes around it. If I type in `git commit -m 'blah' "tmux split-window subcommand.md"` it works perfectly with double quotes. When I try to pass it to git as a bash variable, it does not work. – StevieD Apr 17 '19 at 04:58
  • Maybe I have to strip out the double quote from the file name. Maybe there is no way around that. – StevieD Apr 17 '19 at 05:00
  • Are you certain that file name includes quotes? I suspect that they are there because bash puts them there while printing. – D. Jones Apr 17 '19 at 05:04
  • I'm not really sure if the thing has quotes or not. I didn't add them myself. Macs seem to now do this thing where they display *some* files with spaces enclosd with single apostrophes, though you don't really have to type them to do anything to the file. It's weird. I think git must think the file is "blah blah blah" with actual quotes in the file name and is trying that. I don't know anything about bash quoting and just assumed they would be treated as a string surrounded by quotes. Probably because I come from a Perl background. – StevieD Apr 17 '19 at 05:08
  • The name is coming from an awk function which pulls it from a git status message: `M "tmux split-window subcommand.md"`. Notice that it is in double quotes. I wrote a little awk program to pull that string out. – StevieD Apr 17 '19 at 05:17
  • Here is the awk one-liner: `commit_string+=$(git status -s | awk '{ print substr($0, index($0,$2)) }');` I'm trying to learn awk and bash. It's frustrating as fuck. – StevieD Apr 17 '19 at 05:18
  • Your variable is messing with something, then. Maybe you should get rid of the quotes. Use this: `git commit -m "$desc" "${commit_string:1: ${#commit_string} -2}"` – D. Jones Apr 17 '19 at 05:29
  • I posted an answer. I dug up some weird bash syntax for obliterating the quote character from the $commit_string variable. That did the trick. – StevieD Apr 17 '19 at 05:30
  • Me too. Just wish my bash code wasn't a hopeless mess. It's crazy. Thanks for your help. – StevieD Apr 17 '19 at 05:33
-1

Stripping the quotes out of the file name did the trick:

commit_string=${commit_string//\"}
git commit -m "$desc" "$commit_string"

Coming from the Perl world, I am obviously hopelessly confused by how bash displays and handles string quoting.

StevieD
  • 6,925
  • 2
  • 25
  • 45
  • 1
    In the shell, quotes go *around* data, not *in* data. Putting quotes in a variable doesn't do anything useful, because they'll just be treated as data. See: [Why does shell ignore quotes in arguments passed to it through variables?](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables). – Gordon Davisson Apr 17 '19 at 06:04