os.WriteFile empties state.yml and then writes the new contents. If
lazygit crashes or the disk is full in between, the file is left empty
or partly written. An older lazygit version that runs at the same time
doesn't take the lock, so it can also read the file in this state.
Write the new contents to a temp file in the same folder and rename it
over state.yml. Sync the temp file before the rename. Otherwise the
rename can reach the disk before the data. If the machine then crashes
or loses power, state.yml is left filled with zero bytes; several users
on Windows had this (#3023).
Renaming replaces a symlink, gives the file new permissions, and
ignores a read-only state.yml. Keep the behavior of the in-place write
for these cases: replace the file that a symlink points to, give the
new file the permissions of the old one, and leave a read-only file
alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two lazygit instances can update state.yml at the same moment. Both
read the same contents, and the second write discards the change of the
first. Also, os.WriteFile empties the file before it writes the new
contents. An instance that reads the file at that moment gets an empty
file, which reads as the default state, or a partial one, which doesn't
parse. This can already happen when several instances start at once,
for example when a tmux session is restored, because each of them
updates state.yml at startup.
Take an exclusive lock on a separate lock file while updating, and a
shared one while reading. Each lock call opens the lock file anew, so
the lock also keeps goroutines of the same instance apart; the test
relies on this.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lazygit writes state.yml on every startup to record the current version,
although it's usually the same as the stored one. Switching to the repo
that is already the most recent one rewrites it too. Every write is a
chance for a crash to leave a broken file behind, and the follow-up
work on update checks adds updates that often decide not to change
anything.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A common workflow is to keep lazygit open in several terminal tabs, one
per repo. Each instance reads state.yml once at startup and then works
on its own copy. So an instance doesn't see what other instances
changed after it started, and when it saves, it overwrites their
changes. For example, running a shell command with `:` in one tab drops
the commands that another tab added to the history. Switching repos in
one tab drops the repos that another tab added to the recent repos.
Make ReadAppState read the file every time, and make UpdateAppState
apply its change to what it finds in the file. With a full shell
command history the file has about 70 KB, and reading and parsing it
takes less than a millisecond, so we don't need a cache.
Two instances can still update the file at the same moment, and a read
can see a half-written file. The following commits lock the file and
write it atomically.
Since the file is now read at any time and not only at startup, a file
that can't be parsed reads as the defaults, and the next update
replaces it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each lazygit instance reads state.yml once at startup. Callers change
that copy and then save all of it. So an instance doesn't see what
other instances changed after it started, and when it saves, it
overwrites their changes.
To fix this, the following commits make ReadAppState read the file and
UpdateAppState apply the change to what it finds in the file. For this
to work, callers must express a change as an operation on the current
state: add a command to the front of the history, remove a history
entry by its value, move the current repo to the front of the recent
repos. The call sites already compute the new value from the current
one; move that code into the update function.
This commit still works on the in-memory copy, so it doesn't change
behavior.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The menu leaves out the first entry of the recent repos list because
that's the repo we're in. This is true as long as each lazygit instance
keeps its own copy of state.yml. The following commits make instances
read the list from the file. The first entry is then the repo that any
instance opened most recently, and that may be a different one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If saving state.yml fails while lazygit adds the current repo to the
recent repos list, lazygit stops loading the repo. At startup it exits.
A user on Windows got "The requested operation cannot be performed on
a file with a user-mapped section open" when lazygit opened state.yml
for writing, so lazygit exited on every launch (#4489). Confirming the
intro popup also shows a failed save as an error.
Nothing in state.yml is important enough for this. Log the error
instead, like the other places that save state.yml already do.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing reads it. It's a pointer to the AppConfig's in-memory copy of
state.yml, which the following commits replace with fresh reads of the
file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Several users on Windows reported that lazygit wouldn't start any more
and failed with "yaml: control characters are not allowed" (#3023).
Their state.yml was filled with zero bytes, most likely because the
machine crashed or lost power shortly after lazygit had written the
file. They had to find the file and delete it to make lazygit start
again.
Start with the defaults in this case. The file holds things like the
shell command history and the recent repos list; losing them is better
than not starting. The next save replaces the file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The following commits add locking and atomic writes for state.yml;
keep that code apart from the user config loading and migration code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If you press `n` on the row of a detached HEAD in the branches panel,
the title of the prompt contains the full 40-character commit hash.
Since 5dec080719, the Name of a detached HEAD is the full hash.
Describe the commit in the same way as when you press `n` on it in the
commits panel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A later commit describes a detached HEAD in the same way. Make it work
for hashes shorter than 7 characters. The Name of a detached HEAD is
"HEAD" if `git branch --points-at=HEAD` doesn't report it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In the branches panel, the row of a detached HEAD has an empty hash
column if gui.showBranchCommitHash is on. In half or full screen mode,
it also has no subject. The branch loader creates this row without the
hash and subject of the commit.
Ask git for the subject too. Show the Name of the row in the hash
column; for a detached HEAD, the Name is the full hash. Leave the
row's CommitHash empty. Otherwise, code that looks for branches at a
commit would take the detached HEAD for a branch. For example, the
checkout menu of the HEAD commit would offer to check it out.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since 5dec080719, the Name of a detached HEAD is the full commit hash,
not the part of the DisplayName after "detached at".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If HEAD is detached, the status panel shows the full 40-character
commit hash after the repo name. This also happens during a rebase,
where it used to show the name of the branch being rebased. Since
5dec080719, the Name of a detached HEAD in the model is the full hash.
That commit made the name work as a ref for git commands, but the
status panel still shows it to the user.
During a rebase, show the name of the branch being rebased again. For
any other detached HEAD, show the same text as the top row of the
branches panel, e.g. "(HEAD detached at v1.2)" or "(no branch, bisect
started on master)".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pressing the merge key while HEAD is detached shows an error. This also
happens during a rebase and during a bisect, because HEAD is detached
then. As a result, you can't squash merge another branch at the base of
the current one by starting an interactive rebase with `i`. The
workaround is to squash merge the branch at the tip and move the commit
down. If the merged changes conflict with the branch's own commits, you
have to resolve those conflicts twice this way.
Remove the check. A merge on a detached HEAD is no riskier than a
commit, a cherry-pick or a revert, and lazygit allows all of those
there. It is also useful outside of a rebase, for example to check out
master as a throwaway detached HEAD and merge several branches into it
to see whether they conflict. There is no reason to prevent the user
from doing these things.
If the merge conflicts during a rebase, the merge takes precedence over
the rebase in the working tree state. So continuing goes to the merge
first and then to the rebase.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If HEAD is detached, the title of the rebase menu contains the full
40-character commit hash. The name comes from the checked-out branch in
the model, and its Name is a commit hash for a detached HEAD.
Use "HEAD" instead, like git does in the messages of merge commits. If
HEAD is detached because of a rebase, use the name of the branch being
rebased.
The merge menu gets the same name for its tooltips and for the squash
merge message. It can't be opened on a detached HEAD yet; the next
commit changes that.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If mouse support is on (the default), the right end of the status bar
shows "Donate" and "Ask Question" links in front of the version number.
They take space away from the keybinding options on the left, so fewer
of those fit before they get cut off.
Remove both links and keep only the version number. Also remove the
paragraph of the intro popup that asks users to click the donate
button.
If the search string can overlap itself, as "aa" can in "aaaa", the
matches overlap too. A cell that is part of two matches shows as part
of the first one. If the second one is the current match, some of its
cells then show in the color of the other matches, and it is hard to
tell where the current match is.
Show a cell as part of the current match whenever it is.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If a search has many matches, scrolling the view becomes laggy. For
example, a search for "e" in a long branch log can find 44000 matches.
With that many, drawing a 100x40 view takes 40ms, where it takes 0.14ms
without a search.
For every cell it draws, draw looks through all the matches to see if
the cell is part of one.
The matches are sorted in the same order as draw visits the cells, so
let draw step through them along with the cells. It finds the first
match on screen with a binary search. Drawing that view now takes
0.14ms with or without a search.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No test checks yet which cells draw shows as search matches. The next
commit changes how draw works this out.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If a view shows the inclusion gutter and is scrolled sideways, the
gutter shows the highlight of a search match that begins in the columns
scrolled off to the left. The same goes for the underline of a hovered
hyperlink.
Let draw check only the content cells against the search matches and
the hovered hyperlink, and pass the results to setCharacter. The gutter
cells pass neither.
Since draw now works out the content column of a cell once, and not
again for every search match, this also makes drawing with many search
matches faster. With 44000 matches, drawing a 100x40 view took 265ms
and now takes 40ms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If a view shows the inclusion gutter and is scrolled sideways, the
gutter shows the search highlight of a match that begins in the
columns scrolled off to the left. It also shows the underline of a
hovered hyperlink that begins there. setCharacter checks every cell it
draws against the search matches and the hovered hyperlink, the cells
of the gutter included. For a gutter cell, the content column it works
out is one of the columns scrolled off to the left.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If a search has many matches, scrolling the view becomes laggy because
every draw takes long. Add a benchmark for this, so that later commits
can show how much faster they make drawing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sync/lock_error_partway_through_pull_with_rebase failed on CI: every
attempt to continue the rebase ran into index.lock, as if the lock was
never released.
The test hook that holds index.lock starts a background process that
removes the lock once the git command that ran the hook has exited. If
lazygit runs the command in a pty, as it does for a pull, the command's
exit sends SIGHUP to that process. The process ignored SIGHUP, but only
once it got to run. On a busy machine the command can exit before that;
SIGHUP then kills the process and leaves the lock behind. Delaying the
start of the process by 0.3s makes the test fail every time.
Ignore SIGHUP in the hook before starting the background process, so
that the process inherits it. With this, the test passes even with the
delay.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every view that takes text has an OnPaste handler now, and views that
don't take text already ignored pasted keys. Make this a single rule.
While a paste is in progress, keys go to the focused view's OnPaste
handler or nowhere. This replaces the check in execKeybindings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Treat pasted text like in the other views that take a single line of
text. Before, a pasted newline confirmed the search, and the rest of
the pasted text was lost.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like a menu's filter, a prompt takes a single line of text, so treat
pasted text the same way. Before, the prompt ignored pasted newlines.
This joined the lines without a space. It also ran the keybindings for
other keys in the text, so a pasted Esc closed the prompt, and a pasted
tab switched to the suggestions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The filter takes a single line of text. Drop the newlines at the start
and end of the pasted text, and replace the remaining newlines and tabs
with spaces. A pasted newline therefore no longer runs the selected
menu item.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The keybindings menu filters as you type. If you paste text that ends
with a newline into it, the text filters the menu, and the newline then
runs the selected item as if you had pressed Enter. The same happens in
the other menus that filter as you type.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Do the same as when pasting a commit message with the commit menu, so
that the paragraphs of the body reflow when you edit them. This also
covers a commit message pasted into the summary, since everything after
its first line goes into the description.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If you paste a commit message into the summary with the terminal's
paste command, the body keeps its hard line breaks. This happens even
where auto-wrapping would break the line at the same place. If you
then edit a paragraph of the body, it doesn't reflow. Pasting with the
commit menu already removes these line breaks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It returned a driver for the summary. This made it impossible to use
the description's driver methods after a test step that moves the
focus to the description, such as pasting a multi-line text into the
summary.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If you paste text into an empty description, drop the newlines at its
start. This happens when you paste a whole commit message into the
summary with the terminal's paste command. The part after the first
line goes into the description, and it starts with the blank line
between subject and body.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Use the new OnPaste handler for the summary and the description. This
replaces the IsPasting checks in the keybindings for confirming and for
switching between summary and description. These checks only worked for
the default keys of those keybindings. If a user had bound Enter or Tab
to something else, pasting a newline or a tab ran that command.
Otherwise the behavior stays the same, except that the panel now drops
Esc and control characters in pasted text. Before, it executed them;
for example, a pasted Esc closed the panel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Editable views receive pasted text as one key press per character, so a
newline or a tab in the text triggers the keybinding for Enter or Tab.
The commit message panel works around this by checking IsPasting in
these keybindings. This is fragile, and it gives the panel no way to
look at the pasted text as a whole.
Add an OnPaste handler to View. For a view that has one, collect the
pasted characters, newlines and tabs while the paste lasts, and pass
them to the handler when it ends. Drop all other keys of the paste. They
are unlikely to occur in pasted text, and dispatching keys like Esc in
the middle of a paste would run them as commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If you paste a commit message into the summary with the terminal's
paste command, the description starts with an empty line. The first
newline of the pasted text moves to the description, and the second
one (the blank line between subject and body) is typed into the
description. Git strips the empty line when committing, but it looks
broken in the panel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It checks that the text goes in at the cursor, and that a tab turns
into spaces instead of switching to the summary.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The commit message panel handles pasted text differently from typed
text (see ba6cfc1f85), but integration tests couldn't simulate a
paste, so this had no tests. Let tests replay the events that a
terminal sends for a bracketed paste: the start of the paste, a key
press for each character, and the end of the paste.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Do the same as when rewording a commit, so that the paragraphs of the
body reflow when you edit them.
This also matters for the "Copy commit message to clipboard" entry of
the commit menu. It copies the message with the line breaks that
auto-wrapping inserted. Without this change, pasting the copied
message back into the panel would turn these into hard line breaks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>