Skip to content

Commit 677f15c

Browse files
Add --diverge-at option to allow specification of where branches diverge from main
Signed-off-by: Jacob Stopak <[email protected]>
1 parent d3f56ba commit 677f15c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
Generate dummy Git repositories and populate them with the desired number of commits, branches, and structure.
99

10-
Example: `$ git-dummy --commits=10 --branches=2`
10+
Example: `$ git-dummy --commits=10 --branches=2 --diverge-at=3`
1111

1212
This will initialize a new Git repo in the current directory with 2 branches, each containing 10 commits.
1313

14-
Note: All generated dummy repos have at minimum 1 branch called `main`. For dummies with multiple branches, branches are named `branch1, branch2, ..., branchN`. Each branch currently branches off of `main` at a randomly chosen commit. The length of each branch is capped at the number of commits specified by `--commits`.
14+
Note: All generated dummy repos have at minimum 1 branch called `main`. For dummies with multiple branches, branches are named `branch1, branch2, ..., branchN`. Each branch currently branches off of `main` at `--diverge-at` if supplied, or else a randomly chosen commit. The length of each branch is capped at the number of commits specified by `--commits`.
1515

1616
## Use cases
1717
- Programatically generate Git repos for functional testing of Git tools
@@ -61,6 +61,7 @@ Available options and flags include:
6161
`--name`: The name of the dummy Git repo, defaults to "dummy".
6262
`--commits`: The number of commits to populate in the dummy Git repo, defaults to 5.
6363
`--branches`: The number of branches to generate in the dummy Git repo, defaults to 1.
64+
`--diverge-at`: The commit number at which branches diverge from `main`.
6465
`--git-dir`: The path at which to store the dummy Git repo, defaults to current directory.
6566

6667
## Command examples
@@ -70,6 +71,12 @@ Generate a dummy Git repo called "cheese" on your Desktop, with 2 branches and 1
7071
$ git-dummy --name=cheese --branches=2 --commits=10 --git-dir=~/Desktop
7172
```
7273

74+
Generate a dummy repo with 4 branches `main`, `branch1`, `branch2`, and `branch3`. Branches diverge from `main` after the 2nd commit:
75+
76+
```console
77+
$ git-dummy --branches=4 --diverge-at=2
78+
```
79+
7380
For convenience, environment variables can be set for any command-line option available in git-dummy. All environment variables start with `git_dummy_` followed by the name of the option.
7481

7582
For example, the `--git-dir` option can be set as an environment variable like:

git_dummy/__main__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ def main(
2727
settings.branches,
2828
help="The number of branches to generate",
2929
),
30+
diverge_at: int = typer.Option(
31+
settings.diverge_at,
32+
help="The point branches diverge from main at",
33+
),
3034
):
3135
settings.name = name
3236
settings.git_dir = os.path.join(os.path.expanduser(git_dir), name)
3337
settings.commits = commits
3438
settings.branches = branches
39+
settings.diverge_at = diverge_at
3540

3641
repo = git.Repo.init(settings.git_dir)
3742
repo.config_writer().set_value("init", "defaultBranch", "main").release()
@@ -43,7 +48,11 @@ def main(
4348

4449
while settings.branches - 1 > 0:
4550
repo.git.checkout("main")
46-
r = random.choice(range(1, settings.commits))
51+
r = (
52+
(settings.commits - settings.diverge_at)
53+
if settings.diverge_at
54+
else random.choice(range(1, settings.commits))
55+
)
4756
repo.git.checkout(f"HEAD~{r}")
4857
branch_name = f"branch{settings.branches - 1}"
4958
repo.create_head(branch_name)

git_dummy/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Settings(BaseSettings):
77
git_dir = pathlib.Path().cwd()
88
commits = 5
99
branches = 1
10+
diverge_at = 0
1011

1112
class Config:
1213
env_prefix = "git_dummy_"

0 commit comments

Comments
 (0)