Skip to content

Commit eee9ccf

Browse files
Add --merge option to select which branches get merged back into main
Signed-off-by: Jacob Stopak <[email protected]>
1 parent 677f15c commit eee9ccf

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
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 --diverge-at=3`
10+
Example: `$ git-dummy --commits=10 --branches=4 --merge=1`
1111

12-
This will initialize a new Git repo in the current directory with 2 branches, each containing 10 commits.
12+
This will initialize a new Git repo in the current directory with 4 branches, each containing 10 commits, 1 of which is merged back into `main`.
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 `--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`.
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`. Use `--merge=x,y,...,n` to select which branches get merged back into `main`.
1515

1616
## Use cases
1717
- Programatically generate Git repos for functional testing of Git tools
1818
- Decide how many commits and branches are generated
19+
- Select which branches get merged back into `main`
1920
- Mimic scenarios in real Git repos to practice on without touching real data
2021
- Generate Git demo repos to teach or learn from
2122

2223
## Features
2324
- Run a one-liner git-dummy command in the terminal to generate a dummy Git repo based on your parameters
24-
- Customize the repo name, path, number of commits, branches, and structure
25+
- Customize the repo name, path, number of commits, branches, merges, and structure
2526

2627
## Quickstart
2728

@@ -61,7 +62,8 @@ Available options and flags include:
6162
`--name`: The name of the dummy Git repo, defaults to "dummy".
6263
`--commits`: The number of commits to populate in the dummy Git repo, defaults to 5.
6364
`--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`.
65+
`--diverge-at`: The commit number at which branches diverge from `main`.
66+
`--merge`: A comma separated list of branch postfix ids to merge back into `main`.
6567
`--git-dir`: The path at which to store the dummy Git repo, defaults to current directory.
6668

6769
## Command examples
@@ -77,6 +79,12 @@ Generate a dummy repo with 4 branches `main`, `branch1`, `branch2`, and `branch3
7779
$ git-dummy --branches=4 --diverge-at=2
7880
```
7981

82+
Generate a dummy repo with 4 branches, so that `branch1` and `branch3` are merged back into `main`:
83+
84+
```console
85+
$ git-dummy --branches=4 --merge=1,3
86+
```
87+
8088
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.
8189

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

git_dummy/__main__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ def main(
3131
settings.diverge_at,
3232
help="The point branches diverge from main at",
3333
),
34+
merge: str = typer.Option(
35+
settings.merge,
36+
help="Comma separated branch ids to merge back into main",
37+
),
3438
):
3539
settings.name = name
3640
settings.git_dir = os.path.join(os.path.expanduser(git_dir), name)
3741
settings.commits = commits
3842
settings.branches = branches
3943
settings.diverge_at = diverge_at
44+
settings.merge = merge
4045

4146
repo = git.Repo.init(settings.git_dir)
4247
repo.config_writer().set_value("init", "defaultBranch", "main").release()
@@ -61,6 +66,19 @@ def main(
6166
open(os.path.join(settings.git_dir, f"{branch_name}.{d}"), "a").close()
6267
repo.index.add([f"{branch_name}.{d}"])
6368
repo.index.commit(f"Dummy commit #{d} on {branch_name}")
69+
if settings.merge:
70+
to_merge = settings.merge.split(",")
71+
if str(settings.branches - 1) in to_merge:
72+
repo.git.checkout("main")
73+
main = repo.branches["main"]
74+
branch = repo.branches[branch_name]
75+
base = repo.git.merge_base(main, branch)
76+
repo.index.merge_tree(branch, base=base)
77+
repo.index.commit(
78+
f"Merge {branch_name} into main",
79+
parent_commits=(branch.commit, main.commit),
80+
)
81+
main.checkout(force=True)
6482

6583
settings.branches -= 1
6684

git_dummy/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Settings(BaseSettings):
88
commits = 5
99
branches = 1
1010
diverge_at = 0
11+
merge = ""
1112

1213
class Config:
1314
env_prefix = "git_dummy_"

0 commit comments

Comments
 (0)