Skip to content

Commit d591157

Browse files
committed
When running the upload script, look for uncommitted changes and check before upload if necessary.
1 parent 809476c commit d591157

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

scripts/upload.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,41 @@
44
from getpass import getpass
55
import json
66
import requests
7+
import subprocess
78
import sys
89

10+
def has_uncommitted_changes():
11+
"""Check if there are uncommitted changes in a jj or git repository."""
12+
# Try jj first — works for both native jj repos and git-backed jj repos.
13+
try:
14+
result = subprocess.run(
15+
["jj", "diff", "--stat"],
16+
capture_output=True,
17+
text=True,
18+
)
19+
if result.returncode == 0:
20+
# Successfully queried a jj repo; jj only tracks non-ignored files
21+
# so no extra filtering is needed.
22+
return bool(result.stdout.strip())
23+
# jj is installed but this is not a jj repository; fall back to git.
24+
except FileNotFoundError:
25+
pass # jj is not installed
26+
27+
# Fall back to git, ignoring untracked files.
28+
try:
29+
result = subprocess.run(
30+
["git", "status", "--porcelain", "--untracked-files=no"],
31+
capture_output=True,
32+
text=True,
33+
)
34+
if result.returncode == 0:
35+
return bool(result.stdout.strip())
36+
except FileNotFoundError:
37+
pass # git is not installed
38+
39+
return False
40+
41+
942
API_ENDPOINTS = {
1043
"dev": "https://remote-settings-dev.allizom.org/v1/",
1144
"stage": "https://remote-settings.allizom.org/v1/",
@@ -37,6 +70,13 @@
3770
parser.print_help()
3871
sys.exit(1)
3972

73+
if has_uncommitted_changes():
74+
reply = input(
75+
"\n\nWARNING: There are local uncommitted changes. Are you sure you wish to proceed? (y/N):"
76+
).lower().strip()
77+
if not reply or reply[0] != "y":
78+
sys.exit(1)
79+
4080
# workspace = 'main' if args.server == 'dev' else 'main-workspace'
4181

4282
API_ENDPOINT = API_ENDPOINTS[args.server] + "buckets/%s/collections/%s/records" % (
@@ -57,12 +97,6 @@
5797

5898
existingRecords = response.json()
5999

60-
# Handle python 2 backwards compatibility.
61-
if sys.version_info[0] < 3:
62-
inputFn = raw_input
63-
else:
64-
inputFn = input
65-
66100

67101
def getIdForRecord(record):
68102
if args.collection == "search-config":
@@ -88,7 +122,7 @@ def findRecord(id, recordSet):
88122

89123
def yes_or_no(question):
90124
while "the answer is invalid":
91-
reply = str(inputFn(question + " (y/n): ")).lower().strip()
125+
reply = str(input(question + " (y/n): ")).lower().strip()
92126
if reply[0] == "y":
93127
return True
94128
if reply[0] == "n":
@@ -145,7 +179,7 @@ def strip_record(record):
145179
recordsToRemove.append(record)
146180

147181
if len(recordsToRemove) > 0:
148-
print("\Records to Remove:\n")
182+
print("\nRecords to Remove:\n")
149183

150184
for record in recordsToRemove:
151185
print(getIdForRecord(record))

0 commit comments

Comments
 (0)