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>
For a detached HEAD, the row in the branches panel was missing
information. Its hash column was empty if `gui.showBranchCommitHash` was
on or the panel was in half or full screen mode. In half or full screen
mode, the row also had no commit subject. Both are shown now.
Pressing `n` on that row opened a prompt titled "New branch name (branch
is off of '<40-character hash>')". The prompt now shows the short hash
and subject instead, the same as when you press `n` on the commit in the
commits panel.
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>
When HEAD is detached, the status panel shows the full 40-character
commit hash after the repo name. This also happens during a rebase.
Before 5dec080719, the status panel showed the name of the branch being
rebased in that case. That commit made the name of a detached HEAD the
full hash, but the status panel kept displaying it.
During a rebase, we now show the name of the branch being rebased again,
e.g. "(rebasing) repo → mybranch". For any other detached HEAD, we show
the same text as the top row of the branches panel, e.g. "repo → (HEAD
detached at v1.2)".
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>
Lazygit refused to open the merge menu while HEAD is detached. This
includes the middle of a rebase or a bisect, because HEAD is detached
then too. I don't see a reason for this restriction. 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.
The use case that made me look into this is squash-merging another
branch at the base of the current one. You can now do this by pressing
`i` to start an interactive rebase, squash-merging the branch, and
continuing the rebase. If the squash merge conflicts, the `m` menu
continues the merge first, and then the rebase. The previous workaround
was to squash-merge at the tip and move the commit down. If the merged
changes conflict with the branch's own commits, this means resolving the
conflicts twice.
It is also handy for checking out master as a throwaway detached HEAD
and merging a few branches into it to see whether they conflict with
each other.
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.
In extreme cases, when you have a really large number of search results
(e.g. when searching for a single letter in a long branch log and there
are like 40'000 matches), scrolling the view can become laggy. This PR
improves this to match the performance without a search.
Along the way we fix a bug with highlighting search results when the
custom patch gutter is showing and the view is scrolled sideways.
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.
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>
When pasting a commit message into the commit message editor (either via
the `ctrl+o` menu or with the terminal's Command-V), we now remove the
hard line breaks so that the text reflows again when you edit it, like
we already do when rewording a message.
When pasting with Command-V, we now avoid the extra blank line at the
beginning of the description; this wasn't really a problem because it
got stripped when committing, but it looked ugly.
Along the way, fix a few minor issues when pasting text into various
views, e.g. the search prompt or a "New branch name" text field.
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>