Git Product home page Git Product logo

git-worktree-relative's Introduction

git-worktree-relative

Background

My solution

  • Bash script to replace the content of {worktree}/.git file and {repo}/.git/worktrees/{wtname}/gitdir
  • Why bash: almost everyone who use git will use it in some kind of bash-shell-like environment (ex: bash shell in linux, git bash in windows)
  • Requirements (should be available on every bash shell):
  • Another bash script to change it back to absolute path (since git worktree remove may refuse on relative path)

Usage

  • Execute the script in your worktree (or supply the worktree directory path in -w options)
  • It will read path to repository from {worktree}/.git file
  • Options:
    • -v = verbose
    • -d = dry run (do not write any change, use with verbose to show what this script do)
    • -w worktree_target = directory of worktree to be made relative (will default to current directory if not supplied)
    • -r repository_target = directory of repository (including worktree directory inside .git, will be read from {worktree_target}/.git file if not supplied)
    • -h = show help
  • This solution works for broken link (ex: worktree directory moved OR parent git directory moved): just supply the repository path in -r repositor_target flag
  • This solution works for worktree inside parent repository
  • example:
    • repository in /home/myuser/repo/myproject ; worktree in /home/myuser/www/myproject ; worktree is connected with repository (link is not broken)
      cd /home/myuser/www/myproject
      git worktree-relative
      # OR
      git worktree-relative -w /home/myuser/www/myproject
    • repository in /home/myuser/repo/myproject ; worktree in /home/myuser/www/myproject ; worktree is NOT connected with repository (link broken)
      cd /home/myuser/www/myproject
      git worktree-relative -r /home/myuser/repo/myproject/.git/worktrees/myproject
      # OR
      git worktree-relative -w /home/myuser/www/myproject -r /home/myuser/repo/myproject/.git/worktrees/myproject
    • to detect if link is broken, run command 'git status' in worktree directory
  • Reversing relative worktree back to absolute: just change git worktree-relative command with git worktree-absolute (same command line argument)
    • command git worktree remove requires the path to be absolute: you can use this reverse script to revert it back to absolute path before removing

Installation

Automatic Installation

  • copy paste below command into your terminal:
git clone https://github.com/Kristian-Tan/git-worktree-relative.git
cd git-worktree-relative
sudo bash install.sh
  • or this one-line: git clone https://github.com/Kristian-Tan/git-worktree-relative.git ; cd git-worktree-relative ; sudo bash install.sh
  • or another one-line: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Kristian-Tan/git-worktree-relative/HEAD/get)"

Manual Installation

  • installation for all users:
    • copy git-worktree-relative.sh and git-worktree-absolute.sh to /usr/bin or /bin (you can also remove the extension)
    • give other user permission to execute it
    • example:
      cp git-worktree-relative.sh /usr/bin/git-worktree-relative
      cp git-worktree-absolute.sh /usr/bin/git-worktree-absolute
      chown root:root /usr/bin/git-worktree-relative
      chown root:root /usr/bin/git-worktree-absolute
      chmod 0755 /usr/bin/git-worktree-relative
      chmod 0755 /usr/bin/git-worktree-absolute
  • installation for one user:
    • copy it to any directory that is added to your PATH variable

Uninstallation

  • just remove copied files (or just use uninstall.sh script: git clone https://github.com/Kristian-Tan/git-worktree-relative.git ; sudo bash git-worktree-relative/uninstall.sh)
  • or another one-line: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Kristian-Tan/git-worktree-relative/HEAD/remove)"
Installation on macOS
  • installation on macOS might encounter problem such as tac: command not found or readlink: illegal option -- f
  • the cause is because macOS does not have tac and have their own implementation of readlink
  • easiest workaround is by installing gnu coreutils with homebrew:
      brew install coreutils
      ln -s /usr/local/bin/greadlink /usr/local/bin/readlink
  • after that, you can continue installation and usage as normal

Contributing

  • Feel free to create issue, pull request, etc if there's anything that can be improved

Credits

git-worktree-relative's People

Contributors

kristian-tan avatar

Stargazers

Liang Qi avatar  avatar Tianzuo Shen avatar João Vítor Carli Pereira avatar Paolo G. Giarrusso avatar Daehyun Kim avatar Tsubasa Takayama avatar Rylie Pavlik avatar Pablo Arias avatar Olivier Jaquemet avatar fantouch avatar

Watchers

 avatar

Forkers

mralusw fantouch

git-worktree-relative's Issues

fails if not in worktree (PR)

Running any of the scripts in the main repo (instead of a worktree) result in nasty compounded errors.

There are absolutely no checks that any of the readlink etc commands succeed. I would suggest #!/bin/bash -eu, or at least -u, but for the time being I'll add a sanity check if cat .gitdir fails (this catches most problems).

Also, verbose_output should go to stderr, not stdout; I'll make a separate PR.

.git file content in worktree not correct

if my repos looks like:

/Users/Downloads
├── myrepo
│   ├── .git
│       └── worktrees
│           └── myrepo--dev
│               └── gitdir
└── myrepo--dev
    └── .git

original file content of myrepo-dev/.git is:

gitdir: /Users/Downloads/myrepo/.git/worktrees/myrepo-dev

and after running git-worktree-relative.sh, the expected content should be:

gitdir: ../myrepo/.git/worktrees/myrepo-dev

but I got:

gitdir: ../myrepo/.git/

after debugging, I found out the problem is that the reverse function sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta' did not work on macOS,
and I found that the rev command do the same work in a simple way and works fine under both Linux(Ubuntu) and macOS.

so, just replace sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta' with rev and this bug fixed.

verbose_output -> stderr

It's better to leave stdout for the script output (in case it needs to be reused by another script, e.g. you may wish to output some final path) and print diagnostics / debugging / logging to stderr.

Also on PR #2 since for some reason gh doesn't create new PRs

tac: command not found

$ git-worktree-relative -r ./my-repo -d
git-worktree-relative: line 127: tac: command not found
git-worktree-relative: line 132: tac: command not found
git-worktree-relative: line 142: tac: command not found
readlink: illegal option -- f
usage: readlink [-n] [file ...]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.