Skip to content

Demo a multi-repo task with 'git switch somebranch' #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
WesleyMConner opened this issue Jan 5, 2023 · 7 comments
Open

Demo a multi-repo task with 'git switch somebranch' #1

WesleyMConner opened this issue Jan 5, 2023 · 7 comments

Comments

@WesleyMConner
Copy link

Is your feature request related to a problem? Please describe

I have not found a reliable way to set specific/different git branches for participating repositories using tasks in the multi-repo front end.

Describe the behaviour you'd like

On multi-repo startup, I would like to manually set the git branch of certain participants to a branch that is NEITHER the same as the branch of the multi-repo front end NOR the default branch of the participant.

Describe alternatives you've considered

I have tried creating tasks in the multi-repo front end's .gitpod.yml file that:

  cd ../<proj-handle>          # Switch to a participant repo workspace
  git switch <desired-branch>  # Change the participant repo branch 
  npm install                  # Install NPM dependencies

I change directory command is respected as is the npm install. The git switch command fails with "invalid reference". I have tried writing the task as a multi-line 'command' AND by breaking the lines into appropriate 'before', 'init' and 'command' components.

Additional context

I can perform the git switch manually, in a shell AFTER the new Multi-Repo gitpod session is fully launched. [It's just something else to remember to do.]

I suspect, but cannot confirm, that the timing of Multi-Repo's git clone and set branch activities may not be complete when the tasks are run.

@WesleyMConner
Copy link
Author

I found an ugly solution. If I do everything in a single front-end task, the git switch statements are respected.

- name: <front-end>
    command: |
      npm install
      pushd .; cd ../<proj-1>; git switch <proj-1-branch>; npm install; popd
      pushd .; cd ../<proj-2>; git switch <proj-2-branch>; npm install; popd
        :
      pushd .; cd ../<proj-N>; git switch <proj-N-branch>; npm install; popd

@axonasif
Copy link
Member

axonasif commented Jan 6, 2023

@WesleyMConner you can make it a bit more readable like such:

tasks:
  - name: Set custom branches
    command: |
      # Array for REPO:BRANCH name(s).
      # REPO resolves to `/workspace/REPO` directory.
      repo_branch=(
          proj-1:staging
          proj-2:dev
          proj-3:next
          website:temp
      )

      for item in "${repo_branch[@]}"; do {
          IFS=':' read -r repo branch <<<"${item}" || true
          (
            cd "/workspace/${repo}"
            
            # Switch branch
            git switch "${branch}"

            # npm tasks
            npm install
          )
      } done

@axonasif
Copy link
Member

axonasif commented Jan 6, 2023

Also, would it have been helpful if there was a branch parameter for additionalRepositories like this?

additionalRepositories:
  - url: https://github.com/gitpod-io/demo-multi-repo-backend
    checkoutLocation: backend
    branch: dev # Non-existent currently

@WesleyMConner
Copy link
Author

A 'branch' option on additionalRepositories would be ideal for my use case. I was killing and re-creating a number of Gitpod instances working through the logic. That churn would be less necessary with a built-in facility.

As a rule, there are a few additional things I do, including: (1) Specify the workspaceLocation for vsCode and add subsequently add each participating repo to the referenced file. (2) Setting up a default per-repo terminal (in the proper directory).

I suspect there will always be some of this tailoring. Any out-of-the-box automation is a plus.

I am curious why the git switch couldn't be performed in a per-repo command section. It might be helpful to understand the startup model better to know why insertion in a per-repo command fails. Currently, I'm not using teams; so, haven't played with the teams-specific behavior.

@axonasif
Copy link
Member

axonasif commented Jan 6, 2023

I am curious why the git switch couldn't be performed in a per-repo command section.

If you mean the snippet I shared, it's obviously doable, you're already following a similar method on your initial snippet. My last snippet was aimed at having the whole git switch logic in one task.

Here's another simpler example tailored to address (2) Setting up a default per-repo terminal (in the proper directory).

tasks:
  - name: Frontend
    command: |
      cd /workspace/frontend
      git switch <branch>
      npm install
  - name: Backend
    command: |
      cd /workspace/backend
      git switch <branch>
      npm install

(1) Specify the workspaceLocation for vsCode and add subsequently add each participating repo to the referenced file.

Is this what you mean? (I'm assuming you already saw this)

Any out-of-the-box automation is a plus.

Agree! Could you please raise a feature request at https://github.com/gitpod-io/gitpod/issues/new/choose 🙏 ?

@WesleyMConner
Copy link
Author

Thanks for all of the follow up!
I have created the feature request: gitpod-io/gitpod#15608

When I tried the following (yesterday):

  1. the cd worked,
  2. the git switch failed with the message "invalid reference"
  3. the npm install occurred on the current branch (as opposed to the branch named in the switch statement).
tasks:
  - name: Frontend
    command: |
      cd /workspace/frontend
      git switch <branch>
      npm install
  - name: Backend
    command: |
      cd /workspace/backend
      git switch <branch>
      npm install

Yes, workspaceLocation is working fine as documented. [It's just a matter of remembering to add all repos and reload (not rebuild) the Gitpod instance.]

@axonasif
Copy link
Member

axonasif commented Jan 13, 2023

the git switch failed with the message "invalid reference"

That's odd 😅
Does git checkout work? (edit: it works! We need to use switch -c if we want to use switch command)

What's the output of git branch -v for that git repo directory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants