Skip to content

Commit bfd83b4

Browse files
authored
Add support for main branch in addition to master (#194)
1 parent 9cb9ab6 commit bfd83b4

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

.github/workflows/ci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,42 @@ jobs:
8585
cd wpiformat
8686
python -m wpiformat -f wpiformat -v
8787
88+
# Verify wpiformat reports an error if no master or main branch exists
89+
- name: Git repo with no branches
90+
shell: bash
91+
run: |
92+
rm -rf branch-test
93+
mkdir branch-test && cd branch-test && git init
94+
if wpiformat; then
95+
exit 1
96+
fi
97+
98+
# Verify wpiformat reports success if "master" exists
99+
- name: Git repo with master branch
100+
shell: bash
101+
run: |
102+
rm -rf branch-test
103+
mkdir branch-test && cd branch-test && git init
104+
git checkout -b master
105+
touch .styleguide
106+
git add .styleguide && git commit -q -m "Initial commit"
107+
wpiformat
108+
109+
# Verify wpiformat reports success if "main" exists
110+
- name: Git repo with main branch
111+
shell: bash
112+
run: |
113+
rm -rf branch-test
114+
mkdir branch-test && cd branch-test && git init
115+
git checkout -b main
116+
touch .styleguide
117+
git add .styleguide && git commit -q -m "Initial commit"
118+
wpiformat
119+
120+
- name: Delete branch-test folder
121+
shell: bash
122+
run: rm -rf branch-test
123+
88124
- name: Ensure formatter made no changes
89125
run: git --no-pager diff --exit-code HEAD
90126

wpiformat/wpiformat/__init__.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def proc_pipeline(name):
9797
try:
9898
lines = file.read()
9999
except UnicodeDecodeError:
100-
print("Error: " + name + " contains characters not in UTF-8. "
101-
"Should this be considered a generated file?")
100+
print(
101+
f"error: {name} contains characters not in UTF-8. Should this be considered a generated file?"
102+
)
102103
return False
103104

104105
# The success flag is aggregated across multiple file processing results
@@ -311,9 +312,25 @@ def main():
311312
# Don't check for changes in or run tasks on ignored files
312313
files = filter_ignored_files(files)
313314

315+
# Determine name of main branch for generated file comparisons
316+
branch_options = ["master", "main"]
317+
main_branch = ""
318+
for branch in branch_options:
319+
proc = subprocess.run(["git", "rev-parse", "-q", "--verify", branch],
320+
stdout=subprocess.DEVNULL)
321+
if proc.returncode == 0:
322+
main_branch = branch
323+
break
324+
325+
if not main_branch:
326+
print(
327+
f"error: One of the following branches is required for generated file comparisons, but none exist: {branch_options}."
328+
)
329+
sys.exit(1)
330+
314331
# Create list of all changed files
315332
output_list = subprocess.check_output(
316-
["git", "diff", "--name-only", "master"], encoding="ascii").split()
333+
["git", "diff", "--name-only", main_branch], encoding="ascii").split()
317334
changed_file_list = [
318335
root_path + os.sep + line.strip() for line in output_list
319336
]

0 commit comments

Comments
 (0)