Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git cz -a just differently broken now #543

Closed
mysterycommand opened this issue Jul 17, 2018 · 7 comments · Fixed by #573
Closed

git cz -a just differently broken now #543

mysterycommand opened this issue Jul 17, 2018 · 7 comments · Fixed by #573

Comments

@mysterycommand
Copy link

Hello there, thanks for this project. I've really enjoyed getting changelogs out of my commit messages. I think a recent change to git cz -a (to fix a perhaps over exuberant git add .) has unfortunately just broken things differently. It's late where I am, so I'll have to try and come up with an example repo tomorrow, but I'm currently running into an issue where I get the following output in a terminal:

$ git --version
git version 2.18.0
$ node --version
v8.11.3
$ npm --version
6.2.0
$ git status
On branch feat/whatever
Your branch is up to date with 'origin/feat/whatever'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   src/index.html
        modified:   src/main.css

no changes added to commit (use "git add" and/or "git commit -a")
$ git cz -a
No files added to staging! Did you forget to run git add?
$ git commit -am "feat(html): whatever"
[feat/something-idk 89f79ac] feat(html): whatever
 2 files changed, 2 insertions(+)

So, it seems to me something is still not quite right with the -a flag. Thanks again for your help. I'll be happy to try and test out any steps anyone might recommend for me.

@leotian
Copy link

leotian commented Jul 18, 2018

same error in my computer
image
it can be successful when I change the achievement version
image

@mysterycommand
Copy link
Author

Related to #376 and #471

@mysterycommand
Copy link
Author

Okay, here are some repro steps and a possible solution (though I don't know enough about how other people use Git to know if there are any ramifications to my approach):

Repro:

~/Sites/spikes/cz-cli
12:15:00 $ git init && yarn init -y
Initialized empty Git repository in ~/Sites/spikes/cz-cli/.git/
yarn init v1.7.0
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.
success Saved package.json
✨  Done in 0.11s.

~/Sites/spikes/cz-cli (master #%)
12:15:50 $ git add package.json

~/Sites/spikes/cz-cli (master +)
12:15:57 $ git commit -am "chore: git init && yarn init -y"
[master (root-commit) 14cad29] chore: git init && yarn init -y
 1 file changed, 7 insertions(+)
 create mode 100644 package.json

~/Sites/spikes/cz-cli (master)
12:15:58 $ echo "node_modules" > .gitignore

~/Sites/spikes/cz-cli (master %)
12:16:18 $ git add .gitignore

~/Sites/spikes/cz-cli (master +)
12:16:25 $ git commit -am "chore: ignore node_modules"
[master b583ffa] chore: ignore node_modules
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

~/Sites/spikes/cz-cli (master)
12:16:43 $ yarn add -D commitizen cz-conventional-changelog

# =======================================
#              REMOVED YARN LOGS FOR SPACE
# =======================================

~/Sites/spikes/cz-cli (master *%)
12:17:33 $ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        yarn.lock

no changes added to commit (use "git add" and/or "git commit -a")

~/Sites/spikes/cz-cli (master *%)
12:17:38 $ git add yarn.lock

~/Sites/spikes/cz-cli (master +)
12:17:44 $ git commit -am "chore: add commitizen and cz-conventional-changelog to dev-deps"
[master ad04fc9] chore: add commitizen and cz-conventional-changelog to dev-deps
 2 files changed, 963 insertions(+), 1 deletion(-)
 create mode 100644 yarn.lock

# =======================================
#  ADD COMMITIZEN CONFIG TO PACKAGE.JSON
# =======================================

~/Sites/spikes/cz-cli (master)
12:18:06 $ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")

~/Sites/spikes/cz-cli (master *)
12:19:24 $ git diff
diff --git a/package.json b/package.json
index 97b6eb9..91fef3d 100644
--- a/package.json
+++ b/package.json
@@ -7,5 +7,10 @@
   "devDependencies": {
     "commitizen": "^2.10.1",
     "cz-conventional-changelog": "^2.1.0"
+  },
+  "config": {
+    "commitizen": {
+      "path": "cz-conventional-changelog"
+    }
   }
 }

~/Sites/spikes/cz-cli (master *)
12:19:25 $ git commit -am "config: add commitizen config to package.json"
[master 2c2d272] config: add commitizen config to package.json
 1 file changed, 5 insertions(+)

~/Sites/spikes/cz-cli (master)
12:19:50 $ touch foo.txt

~/Sites/spikes/cz-cli (master %)
12:20:05 $ git add foo.txt

~/Sites/spikes/cz-cli (master +)
12:20:17 $ git commit -am "feat: foo.txt"
[master d2b899c] feat: foo.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo.txt

~/Sites/spikes/cz-cli (master)
12:20:23 $ echo "bar" > foo.txt

~/Sites/spikes/cz-cli (master *)
12:20:32 $ git diff
diff --git a/foo.txt b/foo.txt
index e69de29..5716ca5 100644
--- a/foo.txt
+++ b/foo.txt
@@ -0,0 +1 @@
+bar

~/Sites/spikes/cz-cli (master *)
12:20:36 $ git cz -a
No files added to staging! Did you forget to run git add?

~/Sites/spikes/cz-cli (master *)
12:20:42 $ git commit -am "feat: add bar to foo.txt"
[master a4e286e] feat: add bar to foo.txt
 1 file changed, 1 insertion(+)

I think the problem is here src/commitizen/staging.js#L9 … isClean should not run git diff with the --cached flag. Here's a couple more steps to verify that it would behave as I'd expect if that flag were removed:

~/Sites/spikes/cz-cli (master)
12:21:01 $ touch baz.txt

~/Sites/spikes/cz-cli (master %)
12:31:20 $ echo "qux" > foo.txt

~/Sites/spikes/cz-cli (master *%)
12:31:29 $ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   foo.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        baz.txt

no changes added to commit (use "git add" and/or "git commit -a")

~/Sites/spikes/cz-cli (master *%)
12:31:32 $ git diff --cached --name-only

~/Sites/spikes/cz-cli (master *%)
12:32:02 $ git diff --name-only
foo.txt

Without the --cached flag Git lists the expected/tracked list of files which would return false, and the commit operation would run with the appropriate passed-in flags. If you think that's a viable solution I'd be happy to open a PR some time later this evening. Please let me know what you think!

✨❤️🌟

@thierrymichel
Copy link
Contributor

@mysterycommand not sure if your suggestion will work with "manually added files", like with git add some-file
I've you tried?

@thierrymichel
Copy link
Contributor

I made a PR. I am not used to contributing but I did my best… 🙏

@mysterycommand
Copy link
Author

mysterycommand commented Oct 1, 2018

Hey @thierrymichel, ugh! you're right … doing something like this demonstrates that my solution doesn't work when the commit would contain only new files:

$ touch foo.txt
$ touch bar.txt
$ git add foo.txt
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   foo.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        bar.txt

$ git diff --name-only
$ git diff --cached --name-only
staging.js
$ git cz
No files added to staging! Did you forget to run git add?
$ git cz -a
No files added to staging! Did you forget to run git add?

😭

As for your PR, I'd be kind of hesitant to add that commitAll arg for I think 2 reasons:

  1. I need to understand --cached better I think … what does it mean really, how was it intended, etc.
  2. I have a slight feeling that it's not necessary … I have this feeling that isClean ought to tell us if there are changes staged (e.g. manually added) or tracked (e.g. previously added, and since modified).

I have an idea that borrows from git-prompt.sh that I think will work, but I won't have a chance to try it out until Wednesday of this week.

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   baz.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   foo.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        bar.txt

$ git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only
foo.txt
baz.txt

So, I think if instead of exec('git diff --cached --name-only', { we run exec('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', { here: src/commitizen/staging.js#L9 … we should get the answer I'd expect from isClean without needing the extra arg, no need to change the function signature. Anyway, like I said, I think I can put together a PR for this on Wednesday if you don't beat me to it.

@thierrymichel
Copy link
Contributor

@mysterycommand easier and cleverer (and working)! 👍
As I have already forked the project, it's easy for me to submit a new PR.
But I do not want to "jump the queue" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants