Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Last active February 16, 2024 09:56

Revisions

  1. bartoszmajsak revised this gist Feb 16, 2024. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -88,3 +88,12 @@ fi
    exit 0
    ----

    ## Regular branches

    If you want to use the same approach for regular (existing) branches, use this alias instead

    [source,bash]
    ----
    git config --global alias.worktree-add "f() { git worktree add ../"$1" "$1"; }; f"
    ----
  2. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    ## Motivation

    Most of the times, when PR is not trivial in scope and size, Web UI can often lead to loosing focus and context while scrolling down through the blog of changes. It's way more efficient to go through the code in the IDE.
    Most of the times, when PR is not trivial in scope and size, Web UI can lead to loosing focus and context while scrolling down through the blob of changes.

    It's way more efficient to go through the code in the IDE. The GH plugins available for most of them can help with that, but they simply checkout the branch for you...

    I got tired of stashing changes, creating "wip" commits and shuffling branches.

  3. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -22,9 +22,12 @@ And so this workflow was born.
    <1> Common project settings, usually files globally .gitignored. I use this to add my `.autoenv`, `tools-version` and other utilities, so I have consistent set up across the PRs without manual steps.
    <2> Cloned repository, main branch, where you would usually work.

    For the `main` folder, once you put your `+dev-settings+` in the new location, you have to populate them
    For the `main` folder, assuming you have your settings in `dev-settings` folder, you have to populate them

    `find ../dev-settings -type f -exec ln -s '{}' . `
    [source,bash]
    ----
    find ../dev-settings -type f -exec ln -s '{}' .
    ----

    This step is only needed once, for subsequent PR projects it is automated (TODO: add "hook" for clone)

  4. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ And so this workflow was born.
    └── main # <2>
    ----

    <1> Common project settings, usually files globally `+.gitignore+`d. I use this to add my `.autoenv`, `tools-version` and other utilities, so I have consistent set up across the PRs without manual steps.
    <1> Common project settings, usually files globally .gitignored. I use this to add my `.autoenv`, `tools-version` and other utilities, so I have consistent set up across the PRs without manual steps.
    <2> Cloned repository, main branch, where you would usually work.

    For the `main` folder, once you put your `+dev-settings+` in the new location, you have to populate them
  5. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -19,10 +19,10 @@ And so this workflow was born.
    └── main # <2>
    ----

    <1> Common project settings, usually files globally `.gitignore`d. I use this to add my `.autoenv`, `tools-version` and other `.gitignore`d utilities, so I have consistent set up across the PRs without manual steps.
    <1> Common project settings, usually files globally `+.gitignore+`d. I use this to add my `.autoenv`, `tools-version` and other utilities, so I have consistent set up across the PRs without manual steps.
    <2> Cloned repository, main branch, where you would usually work.

    For the `main` folder, once you put your `dev-settings` in the new location, you have to populate them
    For the `main` folder, once you put your `+dev-settings+` in the new location, you have to populate them

    `find ../dev-settings -type f -exec ln -s '{}' . `

  6. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@ And so this workflow was born.
    . Leverages https://opensource.com/article/21/4/git-worktree[git worktree] to have multiple branches within the same cloned repo.
    . Relies on https://cli.github.com/[GH CLI] to easily checkout PRs the same way as branches.
    . Uses simple git hook to populate common project settings into new PR directory (optional)
    . I use this to add my `.autoenv`, `tools-version` and other `.gitignore`d utilities, so I have consistent set up across the PRs without manual steps.

    .Project structure (root folder)
    [source,bash]
    @@ -20,7 +19,7 @@ And so this workflow was born.
    └── main # <2>
    ----

    <1> Common project settings, usually files globally `.gitignore`d
    <1> Common project settings, usually files globally `.gitignore`d. I use this to add my `.autoenv`, `tools-version` and other `.gitignore`d utilities, so I have consistent set up across the PRs without manual steps.
    <2> Cloned repository, main branch, where you would usually work.

    For the `main` folder, once you put your `dev-settings` in the new location, you have to populate them
  7. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 50 additions and 8 deletions.
    58 changes: 50 additions & 8 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -2,20 +2,64 @@

    Most of the times, when PR is not trivial in scope and size, Web UI can often lead to loosing focus and context while scrolling down through the blog of changes. It's way more efficient to go through the code in the IDE.

    I got tired of stashing changes, creating "wip" commints and shuffling branches.
    I got tired of stashing changes, creating "wip" commits and shuffling branches.

    And so this workflow was born.

    ## How it works

    . Leverages git worktree to have multiple branches within the same cloned repo
    . Relies on https://cli.github.com/[GH CLI] for dealing with PR branches
    . Leverages https://opensource.com/article/21/4/git-worktree[git worktree] to have multiple branches within the same cloned repo.
    . Relies on https://cli.github.com/[GH CLI] to easily checkout PRs the same way as branches.
    . Uses simple git hook to populate common project settings into new PR directory (optional)
    . I use this to add my `.autoenv`, `tools-version` and other git-ignored utilities, so I have consistent set up across the PRs without manual steps.
    . I use this to add my `.autoenv`, `tools-version` and other `.gitignore`d utilities, so I have consistent set up across the PRs without manual steps.

    ## Pre requisites
    .Project structure (root folder)
    [source,bash]
    ----
    ├── dev-settings # <1>
    └── main # <2>
    ----

    <1> Common project settings, usually files globally `.gitignore`d
    <2> Cloned repository, main branch, where you would usually work.

    For the `main` folder, once you put your `dev-settings` in the new location, you have to populate them

    `find ../dev-settings -type f -exec ln -s '{}' . `

    This step is only needed once, for subsequent PR projects it is automated (TODO: add "hook" for clone)

    Now, from the `main` folder we can add PR worktrees using special alias:

    [source,bash]
    ----
    ❯ git worktree-add-pr 402
    remote: Enumerating objects: 2, done.
    remote: Counting objects: 100% (1/1), done.
    remote: Total 2 (delta 1), reused 1 (delta 1), pack-reused 1
    Unpacking objects: 100% (2/2), 258 bytes | 258.00 KiB/s, done.
    From github.com:opendatahub-io/opendatahub-operator
    * [new ref] refs/pull/402/head -> pr-402
    Switched to branch 'pr-402'
    Switched to branch 'main'
    Your branch is up to date with 'origin/main'.
    Preparing worktree (checking out 'pr-402')
    HEAD is now at 1a90cd0 Allow multiple component names for Dashboard
    Setting up the worktree...
    ----

    This updates directory structure:

    [source,bash]
    ----
    ├── dev-settings
    ├── main
    └── pr-402
    ----

    Now you can open `pr-402` as a project in your IDE of choice and you can comfortably walk through the code, hack suggestions and run tests!

    To remove, just call regular git command `git worktree remove -f pr-402`

    ## Git configuration

    @@ -35,10 +79,8 @@ checkout_type="$3"
    if [ "$previous_head" = "0000000000000000000000000000000000000000" ]; then
    echo "Setting up the worktree..."
    find ../dev-settings -type f -exec ln -s '{}' . \; // <1>
    find ../dev-settings -type f -exec ln -s '{}' . \;
    fi
    exit 0
    ----

    <1> folder in the main project directory
  8. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 42 additions and 1 deletion.
    43 changes: 42 additions & 1 deletion gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,44 @@
    ## Motivation

    Most of the times, when PR is not trivial in scope and size, Web UI can often lead to loosing focus and context while scrolling down through the blog of changes. It's way more efficient to go through the code in the IDE.

    I got tired of stashing changes, creating "wip" commints and shuffling branches.

    And so this workflow was born.

    ## How it works

    . Leverages git worktree to have multiple branches within the same cloned repo
    . Relies on https://cli.github.com/[GH CLI] for dealing with PR branches
    . Uses simple git hook to populate common project settings into new PR directory (optional)
    . I use this to add my `.autoenv`, `tools-version` and other git-ignored utilities, so I have consistent set up across the PRs without manual steps.

    ## Pre requisites

    . GH CLI[https://cli.github.com/]


    ## Git configuration

    [source,bash]
    ----
    git config --global alias.worktree-add-pr "!f() { gh pr checkout $1 -b pr-$1; git co -; git worktree-add pr-$1; }; f"
    ----

    ..git/hooks/post-checkout
    [source,bash]
    ----
    #!/bin/bash
    previous_head="$1"
    new_head="$2"
    checkout_type="$3"
    if [ "$previous_head" = "0000000000000000000000000000000000000000" ]; then
    echo "Setting up the worktree..."
    find ../dev-settings -type f -exec ln -s '{}' . \; // <1>
    fi
    exit 0
    ----

    <1> folder in the main project directory
  9. bartoszmajsak revised this gist Feb 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    ## Pre requisites

    . [GH CLI](https://cli.github.com/)
    . GH CLI[https://cli.github.com/]
  10. bartoszmajsak created this gist Feb 15, 2024.
    3 changes: 3 additions & 0 deletions gh-pr.adoc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    ## Pre requisites

    . [GH CLI](https://cli.github.com/)