lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

Copying committed file name crashes if running lazygit through ssh

Open karjonas opened this issue 3 years ago • 10 comments

Describe the bug Copying committed file name crashes if running lazygit through ssh.

To Reproduce Steps to reproduce the behavior:

  1. Connect to a computer through ssh
  2. Run lazygit in a git repo
  3. Select a commit and a file name
  4. Click ctrl+o
  5. Crash

Expected behavior The name of the committed name should be copied.

Desktop (please complete the following information):

  • OS: Arch Linux
  • Lazygit Version commit=d2ea5dd8b7fe62228854ad7f1a3a1d4681378b69, build date=2020-10-14T08:13:53Z, build source=binaryRelease, version=0.23.5, os=linux, arch=amd64|

karjonas avatar Oct 16 '20 11:10 karjonas

I'm not able to replicate this on mac. Do you have the stack trace handy?

jesseduffield avatar Nov 28 '20 08:11 jesseduffield

I am now using this version: commit=4b55b2e7fca96dcdf2a25c6d66e4545a4f184db1, build date=2020-11-10T09:06:27Z, build source=binaryRelease, version=0.23.7, os=linux, arch=amd64

stackTrace:

2020/11/28 12:44:27 An error occurred! Please create an issue at https://github.com/jesseduffield/lazygit/issues

*exec.ExitError exit status 1 github.com/jesseduffield/lazygit/main.go:133 (0x5579d34d0437) runtime/proc.go:204 (0x5579d2f06609) runtime/asm_amd64.s:1374 (0x5579d2f39a61)

karjonas avatar Nov 28 '20 10:11 karjonas

I wonder if the machine you've ssh'd onto lacks the required utilities to copy to clipboard. I also wonder how clipboard stuff generally works when dealing with ssh.

jesseduffield avatar Sep 25 '21 03:09 jesseduffield

It requires xsel or something of sorts and that the ssh connection forwards X, which means one needs to connect with ssh -X <server>, as mentioned here. I guess this doesn't have to be a hard crash, but a helpful message?

mark2185 avatar Aug 07 '22 20:08 mark2185

It would be great if there had a custom config property to say to lazygit how to copy to the clipboard. This way we could to define a command to, for example,

os:
  copyShaCommitToClipboard: ssh user@my-machine 'echo "${commit}" | pbcopy'

mestihudson avatar Jun 27 '23 00:06 mestihudson

^ +1. OSC52 is my preferred way of copying. Would be great to have a config option to use this.

redstreet avatar Jul 15 '23 00:07 redstreet

This should be easy to implement, just a matter of going to pkg/commands/oscommands/os.go and updating the CopyToClipboard function to support a configured command

diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go
index 78dad7a8e..f51539a78 100644
--- a/pkg/commands/oscommands/os.go
+++ b/pkg/commands/oscommands/os.go
@@ -267,6 +267,14 @@ func (c *OSCommand) CopyToClipboard(str string) error {
 	escaped := strings.Replace(str, "\n", "\\n", -1)
 	truncated := utils.TruncateWithEllipsis(escaped, 40)
 	c.LogCommand(fmt.Sprintf("Copying '%s' to clipboard", truncated), false)
+
+	if c.UserConfig.OS.CopyToClipboard != "" {
+		cmdStr := utils.ResolvePlaceholderString(c.UserConfig.OS.CopyToClipboard, map[string]string{
+			"text": c.Cmd.Quote(str),
+		})
+		return c.Cmd.NewShell(cmdStr).Run()
+	}
+
 	return clipboard.WriteAll(str)
 }

The CopyToClipboard key can be defined in pkg/config/user_config.go

Then it's a matter of updating the docs in docs/Config.md to add the os.copyToClipboard key.

I'll chuck a good-first-issue label on this. Do either of you want to take this on @mestihudson @redstreet ?

jesseduffield avatar Jul 15 '23 00:07 jesseduffield

You pretty much included the whole code there (thank you!), and I wish I could do this, but I've never worked with Go, or with docker, so I'm unlikely to be able to find the time to set myself up adequately :(.

I'll offer this proposal instead, if someone else is interested in considering it:

~Config wise, there could be two keys:~

  • os.enableOsc52Clipboard = true/false key, which when set to true, would use this osc52 go library to copy
  • os.copyToClipboardCommand, when set, would run a custom command to copy to clipboard, and would ignore os.enableOsc52Clipboard

Update: the simpler solution is to include the command to do an OSC52 copy in the doc.

redstreet avatar Jul 16 '23 05:07 redstreet

Just noticed the Github Codespaces in CONTRIBUTING. Cool. Let me take a shot at this.

redstreet avatar Jul 16 '23 08:07 redstreet

I am using a terminal emulator that supports OSC52, am SSHing into a computer and am inside of a tmux session.

The given example of using OSC52 doesn't seem to work, does anyone know why? I assume its something not related to lazygit, but just wondering if anyone else has ran into this.

My workaround is to use ssh, but that doesn't work when I'm actually on the computer

os:
  copyToClipboardCmd: "echo {{text}} | ssh \"mitchell@$(echo $SSH_CONNECTION | awk '{ print $1}')\" pbcopy"

mhanberg avatar Apr 22 '24 14:04 mhanberg