Skip to content

Fix Ignore doing nothing once a .gitignore is already there - #604

Merged
hannesa2 merged 2 commits into
gitx:masterfrom
goneng:fix-ignoring-a-second-file
Sep 12, 2026
Merged

hannesa2 merged 2 commits into
gitx:masterfrom
goneng:fix-ignoring-a-second-file

Conversation

@goneng

@goneng goneng commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #597.

Ignoring a file worked the first time and never again.
Appending to the .gitignore that the first one left behind measured the length of the
string being built rather than the one read off disk:

NSString *ignoreFile;                                    // still nil here
...
if ([currentFile characterAtIndex:([ignoreFile length] - 1)] != '\n')

[nil length] is 0, and 0 - 1 on an NSUInteger wraps around, so the call raised NSRangeException.
That branch runs only when a .gitignore already exists, which is why
the first file always worked and every one after it did nothing.
The exception reached AppKit, which logs it and carries on, so no error sheet was ever shown either.

The second commit catches anything raised there and hands the caller an error, which
it already knows how to show.

Test plan

  • New PBGitRepositoryIgnoreTests: 6 tests, of which 4 fail on master
    with the exception above and one covers the empty .gitignore that
    would have overflowed the same way
  • make unit-test: 55 tests, 0 failures
  • Ran the steps from the issue against a build: a fresh repository with
    f1 and f2, ignore f1, then ignore f2, and the .gitignore holds both

@goneng
goneng marked this pull request as ready for review September 11, 2026 22:50
@goneng
goneng force-pushed the fix-ignoring-a-second-file branch from 3addef6 to 47ebe9d Compare September 12, 2026 08:42
…tx#597)

Ignoring a file worked the first time and never again, because appending
to the .gitignore left behind measured the length of the string being
built rather than the one read off disk, and indexing past the end of it
raised an exception the menu item quietly swallowed.

* Ask the file read off disk whether it already ends in a newline
* Leave an empty .gitignore without a newline in front of the first
  path written into it
Writing the .gitignore can raise rather than return, and an exception
leaving this call reaches AppKit, which logs it and carries on, so the
menu item looks to the user as though it simply did nothing.

* Catch anything raised while the file is written and hand the caller
  an error, which it already knows to show in a sheet
* Name the raised reason in that error, so a report of "nothing
  happened" arrives with something to go on
@goneng
goneng force-pushed the fix-ignoring-a-second-file branch from 47ebe9d to 04ab711 Compare September 12, 2026 09:36
@hannesa2
hannesa2 merged commit 1e9121e into gitx:master Sep 12, 2026
3 checks passed
@goneng
goneng deleted the fix-ignoring-a-second-file branch September 12, 2026 15:19
@goneng goneng added the bugfix label Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Ignore file" context menu doesnt work

2 participants