Skip to content

Conversation

@nakabonne
Copy link
Member

What this PR does / why we need it:
On MacOS, BSD's cp command is installed by default. So the behavior is slightly different from GNU's one.
This PR will cause the build of the binary for Windows to fail. I don't mind add command_windows.go if it seems to be a problem.

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?:

NONE

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 34.25%. This pull request decreases coverage by -0.00%.

File Function Base Head Diff
pkg/git/command_linux.go copyCommand -- 0.00% +0.00%


func copyCommand(src, dst string) *exec.Cmd {
src = strings.TrimSuffix(src, "/")
return exec.Command("cp", "-rf", src+"/.", dst)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits, does the cp command not work without the . at the end of src path? 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too. Not sure why do we need this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS's cp -r puts the directory you're copying into the destination directory.

❯ ls -a /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/srcdir/
./             ../            .git/          Dockerfile     client/        client2/       go.mod         go.sum         main.go

❯ cp -rf /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/srcdir /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/dstdir

❯ ls -a /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/dstdir
./                      ../                     srcdir/

We can copy only the files under the srcdir by keeping the trailing slash for srcdir:

❯ cp -rf /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/srcdir/ /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/dstdir

❯ ls -a /var/folders/dn/4wqz0bq15x5_5zpxf1grd455pmlkzw/T/dstdir
./             ../            .git/          Dockerfile     client/        client2/       go.mod         go.sum         main.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the trailing . is unneeded.

Copy link
Member

@khanhtc1202 khanhtc1202 Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then this part is to ensure the last character of src path is / if I guess right? 🤔
besides, I checked on my macOS environment, src/path/ and src/path// have the same effect to the cp command 😅 I think we could simplify this logic here as just add the '/' character to the end of src path. How do you think about that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I'll do so after gaining everyone's agreements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked on my Linux environment, looks like the cp Linux command has the same behavior as macOS command, which means cp -r will copy the entry directory as a subdirectory of the dist path. If you want to just copy all subdirectories and files of the src directory to the dist path, you should add * to the end of the src path in case of using the Linux cp command.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it true? Seems like I need to look into it more.

@nakabonne
Copy link
Member Author

/hold

}

// Copy does copying the repository to the given destination.
func (r *repo) Copy(dest string) (Repo, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nghialv btw, perhaps was this method originally created for copying the repo root to the given dest as a subdirectory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nghialv If so, my usage is just evil.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even if so, it's not appropriate to give back Repo. Well, it's nothing!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Copy function copies all the data of the original repository into a new directory.
And it expects the destination directory was not created before passing to this function.
I am sorry for the lack of explanation. We should have at least a comment.

From the current situation, I think we have 2 options:

  • keep the current behavior and add a comment to explain that the dest directory must not be created before
  • require the user to ensure that the directory must be created before passing to this function and change the command to cp -rf src/* dest

Copy link
Member Author

@nakabonne nakabonne Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One vote for the first option. In the second context, The behaviors are slightly different depending on the cp command implementation (mostly BSD and GNU). Actually cp -rf src/* dest doesn't copy the hidden files including .git/ and so on.

Let me keep the current behavior and just add a comment!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pipecd-bot pipecd-bot added size/XS and removed size/M labels Dec 22, 2020
@nakabonne nakabonne changed the title Use OS-specific command to copy directory Add notes on copying git repository Dec 22, 2020
@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 34.25%. This pull request does not change code coverage.

@nakabonne
Copy link
Member Author

Fixed done.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 34.25%. This pull request does not change code coverage.

pipecd-bot pushed a commit that referenced this pull request Dec 22, 2020
**What this PR does / why we need it**:
Usage of copying repo became clear from the discussion on #1296

**Which issue(s) this PR fixes**:

Fixes #

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
@nakabonne
Copy link
Member Author

/hold cancel

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by nghialv.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants