Skip to content

Commit

Permalink
Merge pull request #248 from gunechristensen/main
Browse files Browse the repository at this point in the history
Add support for multiple symbolic links per repo
  • Loading branch information
jacebrowning committed Feb 12, 2021
2 parents df0d406 + 3ed613f commit b7b59f8
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 38 deletions.
43 changes: 43 additions & 0 deletions docs/use-cases/multiple-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Using multiple links

This feature can be used to create as many symbolic links as you need from one repository.

## The syntax

Let's say we have a simple project structure.

|- include
|- src
|- docs

```yaml
location: .gitman

sources:
- repo: <URL of my_dependency repository>
name: my_dependency
rev: v1.0.3
links:
- source: include
target: vendor/partial_repo
- target: vendor/full_repo
```

This will result in the following symbolic links:
- `<root>/vendor/partial_repo` -> `<root>/.gitman/my_dependency/include`
- `<root>/vendor/full_repo` -> `<root>/.gitman/my_dependency`

## Alternative syntax

```yaml
location: vendor

sources:
- repo: <URL of my_dependency repository>
name: my_dependency
rev: v1.0.3
links:
- { source: src, target: partial_repo }
- { target: full_repo }
```

64 changes: 40 additions & 24 deletions gitman.yml
Original file line number Diff line number Diff line change
@@ -1,80 +1,96 @@
location: demo
sources:
- name: gitman_1
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_1
rev: example-branch
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: example-branch
link:
links:
-
scripts:
- cat .noserc
- make foobar
- name: gitman_2
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_2
rev: example-tag
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: example-tag
link:
links:
-
scripts:
-
- name: gitman_3
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_3
rev: master@{2015-06-18 11:11:11}
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: master@{2015-06-18 11:11:11}
link:
links:
-
scripts:
- echo "Hello, World!"
- pwd
- name: gitman_4
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_4
rev: example-branch-2
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: example-branch-2
link:
links:
-
scripts:
-
sources_locked:
- name: gitman_1
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_1
rev: dfd561870c0eb6e814f8f6cd11f8f62f4ae88ea0
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: dfd561870c0eb6e814f8f6cd11f8f62f4ae88ea0
link:
links:
-
scripts:
- cat .noserc
- make foobar
- name: gitman_2
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_2
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: 7bd138fe7359561a8c2ff9d195dff238794ccc04
link:
links:
-
scripts:
-
- name: gitman_3
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_3
rev: 2da24fca34af3748e3cab61db81a2ae8b35aec94
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: 2da24fca34af3748e3cab61db81a2ae8b35aec94
link:
links:
-
scripts:
- echo "Hello, World!"
- pwd
- name: gitman_4
- repo: https://github.com/jacebrowning/gitman-demo
name: gitman_4
rev: f50c1ac8bf27377625b0cc93ea27f8069c7b513a
type: git
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
-
rev: f50c1ac8bf27377625b0cc93ea27f8069c7b513a
link:
links:
-
scripts:
-
groups:
Expand Down
1 change: 1 addition & 0 deletions gitman/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def install_dependencies(
skip_changes=skip_changes,
)
source.create_link(self.root, force=force)
source.create_links(self.root, force=force)
common.newline()
count += 1

Expand Down
49 changes: 35 additions & 14 deletions gitman/models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from .. import common, exceptions, git, shell


@dataclass
class Link:
source: str = ''
target: str = ''


@dataclass
class Source:
"""A dictionary of `git` and `ln` arguments."""
Expand All @@ -18,6 +24,7 @@ class Source:
type: str = 'git'
sparse_paths: List[str] = field(default_factory=list)
link: Optional[str] = None
links: List[Link] = field(default_factory=list)

scripts: List[str] = field(default_factory=list)

Expand Down Expand Up @@ -138,26 +145,25 @@ def update_files(
self.type, self.repo, self.name, fetch=fetch, clean=clean, rev=self.rev
)

def create_links(self, root, force=False):
"""Create links from the source to target directory."""
if not self.links:
return

for link in self.links:
target = os.path.join(root, os.path.normpath(link.target))
relpath = os.path.relpath(os.getcwd(), os.path.dirname(target))
source = os.path.join(relpath, os.path.normpath(link.source))
create_sym_link(source, target, force)

def create_link(self, root, force=False):
"""Create a link from the target name to the current directory."""
if not self.link:
return

log.info("Creating a symbolic link...")

target = os.path.join(root, self.link)
target = os.path.join(root, os.path.normpath(self.link))
source = os.path.relpath(os.getcwd(), os.path.dirname(target))

if os.path.islink(target):
os.remove(target)
elif os.path.exists(target):
if force:
shell.rm(target)
else:
msg = "Preexisting link location at {}".format(target)
raise exceptions.UncommittedChanges(msg)

shell.ln(source, target)
create_sym_link(source, target, force)

def run_scripts(self, force=False, show_shell_stdout=False):
log.info("Running install scripts...")
Expand Down Expand Up @@ -271,3 +277,18 @@ def _invalid_repository(self):
path
)
return exceptions.InvalidRepository(msg)


def create_sym_link(source, target, force):
log.info("Creating a symbolic link...")

if os.path.islink(target):
os.remove(target)
elif os.path.exists(target):
if force:
shell.rm(target)
else:
msg = "Preexisting link location at {}".format(target)
raise exceptions.UncommittedChanges(msg)

shell.ln(source, target)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
- Build System Integration: use-cases/build-integration.md
- Sparse Checkouts: use-cases/sparse-checkouts.md
- Default Groups: use-cases/default-groups.md
- Using Multiple Links: use-cases/multiple-links.md
- Extras:
- Git SVN Bridge: extras/git-svn-bridge.md
- Bundled Application: extras/bundled-application.md
Expand Down

0 comments on commit b7b59f8

Please sign in to comment.