atom-git-control icon indicating copy to clipboard operation
atom-git-control copied to clipboard

Compare doesn't work

Open sixrandanes opened this issue 9 years ago • 12 comments

Hello,

When I click on compare command, I have that issue : cannot read property 'push' of undefined. I'am using atom version 0.184.0. screenshot

Thanks

sixrandanes avatar Mar 06 '15 06:03 sixrandanes

Same issue, when I installed git:control, it worked, now, I get this issue. Atom Windows 0.194.0, git-control 0.2.0

kylekatarnls avatar Apr 30 '15 14:04 kylekatarnls

Can you post a screenshot like the one above? I have a feeling that this issue is related to Atom not properly recognizing your project as a git repository. ( See the icon in the top image is a normal folder, not a repository icon the way it normally is)

MarcelMue avatar Apr 30 '15 15:04 MarcelMue

@MarcelMue the icon isn't a good indicator of this. I have a project some levels deep inside a git repository and Atom has no problem displaying the changes in the left margin. Oh and it also does not display it as a book.

hellerbarde avatar Jul 15 '15 13:07 hellerbarde

Same issue.

Cannot read property 'push' of undefined

codingsteff avatar Jul 20 '15 08:07 codingsteff

I have the same problem. running the command "git --no-pager diff" from both git bash and the Command Promp seems to work fine.

PlaidPhantom avatar Jul 26 '15 01:07 PlaidPhantom

any updates on that issue? it's still occuring...

kantholy avatar May 20 '16 09:05 kantholy

same occurs on my project. It is not 100% reproducible though.

hellt avatar Jul 25 '16 07:07 hellt

Hello, I debugged this issue and think that I know what's going on.

When Compare button is clicked git-control will run git diff command and parse its output. The following is an excerpt of that output.

warning: LF will be replaced by CRLF in src/elm/Main.elm.
The file will have its original line endings in your working directory.
diff --git a/README.md b/README.md
index a72397a..9867dcd 100644
--- a/README.md
+++ b/README.md
@@ -159,3 +159,51 @@ according to the model.
           div.ball

And the relevant parsing code can located at git.coffee#42, in a function that is quoted below for reference.

parseDiff = (data) -> q.fcall ->
  diffs = []
  diff = {}
  for line in data.split('\n') when line.length
    switch
      when /^diff --git /.test(line)
        diff =
          lines: []
          added: 0
          removed: 0
        diff['diff'] = line.replace(/^diff --git /, '')
        diffs.push diff
      when /^index /.test(line)
        diff['index'] = line.replace(/^index /, '')
      when /^--- /.test(line)
        diff['---'] = line.replace(/^--- [a|b]\//, '')
      when /^\+\+\+ /.test(line)
        diff['+++'] = line.replace(/^\+\+\+ [a|b]\//, '')
      else
        diff['lines'].push line
        diff['added']++ if /^\+/.test(line)
        diff['removed']++ if /^-/.test(line)

  return diffs

The first two lines git prints are just warnings regarding line ending handling in Windows, however they are not being filtered. Thus, the first iteration of the for loop will enter the switch in the else branch while diff is still uninitialized (it will not be assigned valid data until handling line 3 of the output) which leads to trying to call push of undefined.

What I think it would help is to do as in the following "pseudo-patch":

         diff['+++'] = line.replace(/^\+\+\+ [a|b]\//, '')
-      else
+      when /^[ +-]/.test(line)
         diff['lines'].push line
         diff['added']++ if /^\+/.test(line)
         diff['removed']++ if /^-/.test(line)
+      else
+        # Do whatever you want with non-diff lines
+        continue # such as ignoring them

diegonc avatar Aug 23 '16 02:08 diegonc

another option would be to "initialize" the object before the switch statement - or am I wrong?

parseDiff = (data) -> q.fcall ->
  diffs = []
- diff = {}
+ diff = {
+   lines: [],
+   added: 0,
+   removed: 0
+ }
  for line in data.split('\n') when line.length
    switch
      when /^diff --git /.test(line)
-       diff =
-         lines: []
-         added: 0
-         removed: 0
        diff['diff'] = line.replace(/^diff --git /, '')
        diffs.push diff

kantholy avatar Aug 23 '16 10:08 kantholy

That will break when there are many diff lines in the same output. Each of them start a new diffobject that needs to be pushed into diffsarray.

diegonc avatar Aug 23 '16 14:08 diegonc

+1 bump same issue even with a similar package

karneaud avatar Nov 09 '16 19:11 karneaud

I've tested your patch and it solves the issue. Thanks

mikolaj-kow avatar Apr 04 '17 14:04 mikolaj-kow