-
Notifications
You must be signed in to change notification settings - Fork 318
/
git.py
1107 lines (974 loc) · 40.3 KB
/
git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Module for executing git commands, sending results back to the handlers
"""
import os
import re
import subprocess
from urllib.parse import unquote
import pexpect
import tornado
import tornado.locks
import datetime
# Git configuration options exposed through the REST API
ALLOWED_OPTIONS = ['user.name', 'user.email']
# Regex pattern to capture (key, value) of Git configuration options.
# See https://git-scm.com/docs/git-config#_syntax for git var syntax
CONFIG_PATTERN = re.compile(r"(?:^|\n)([\w\-\.]+)\=")
DEFAULT_REMOTE_NAME = "origin"
# How long to wait to be executed or finished your execution before timing out
MAX_WAIT_FOR_EXECUTE_S = 20
# Ensure on NFS or similar, that we give the .git/index.lock time to be removed
MAX_WAIT_FOR_LOCK_S = 5
# How often should we check for the lock above to be free? This comes up more on things like NFS
CHECK_LOCK_INTERVAL_S = 0.1
execution_lock = tornado.locks.Lock()
async def execute(
cmdline: "List[str]",
cwd: "str",
env: "Optional[Dict[str, str]]" = None,
username: "Optional[str]" = None,
password: "Optional[str]" = None,
) -> "Tuple[int, str, str]":
"""Asynchronously execute a command.
Args:
cmdline (List[str]): Command line to be executed
cwd (Optional[str]): Current working directory
env (Optional[Dict[str, str]]): Defines the environment variables for the new process
username (Optional[str]): User name
password (Optional[str]): User password
Returns:
(int, str, str): (return code, stdout, stderr)
"""
async def call_subprocess_with_authentication(
cmdline: "List[str]",
username: "str",
password: "str",
cwd: "Optional[str]" = None,
env: "Optional[Dict[str, str]]" = None,
) -> "Tuple[int, str, str]":
try:
p = pexpect.spawn(
cmdline[0], cmdline[1:], cwd=cwd, env=env, encoding="utf-8", timeout=None
)
# We expect a prompt from git
# In most of cases git will prompt for username and
# then for password
# In some cases (Bitbucket) username is included in
# remote URL, so git will not ask for username
i = await p.expect(["Username for .*: ", "Password for .*:"], async_=True)
if i == 0: # ask for username then password
p.sendline(username)
await p.expect("Password for .*:", async_=True)
p.sendline(password)
elif i == 1: # only ask for password
p.sendline(password)
await p.expect(pexpect.EOF, async_=True)
response = p.before
returncode = p.wait()
p.close()
return returncode, "", response
except pexpect.exceptions.EOF: # In case of pexpect failure
response = p.before
returncode = p.exitstatus
p.close() # close process
return returncode, "", response
def call_subprocess(
cmdline: "List[str]",
cwd: "Optional[str]" = None,
env: "Optional[Dict[str, str]]" = None,
) -> "Tuple[int, str, str]":
process = subprocess.Popen(
cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env
)
output, error = process.communicate()
return (process.returncode, output.decode("utf-8"), error.decode("utf-8"))
try:
await execution_lock.acquire(timeout=datetime.timedelta(seconds=MAX_WAIT_FOR_EXECUTE_S))
except tornado.util.TimeoutError:
return (1, "", "Unable to get the lock on the directory")
try:
# Ensure our execution operation will succeed by first checking and waiting for the lock to be removed
time_slept = 0
lockfile = os.path.join(cwd, '.git', 'index.lock')
while os.path.exists(lockfile) and time_slept < MAX_WAIT_FOR_LOCK_S:
await tornado.gen.sleep(CHECK_LOCK_INTERVAL_S)
time_slept += CHECK_LOCK_INTERVAL_S
# If the lock still exists at this point, we will likely fail anyway, but let's try anyway
if username is not None and password is not None:
code, output, error = await call_subprocess_with_authentication(
cmdline,
username,
password,
cwd,
env,
)
else:
current_loop = tornado.ioloop.IOLoop.current()
code, output, error = await current_loop.run_in_executor(
None, call_subprocess, cmdline, cwd, env
)
finally:
execution_lock.release()
return code, output, error
def strip_and_split(s):
"""strip trailing \x00 and split on \x00
Useful for parsing output of git commands with -z flag.
"""
return s.strip("\x00").split("\x00")
class Git:
"""
A single parent class containing all of the individual git methods in it.
"""
def __init__(self, contents_manager):
self.contents_manager = contents_manager
self.root_dir = os.path.expanduser(contents_manager.root_dir)
async def config(self, top_repo_path, **kwargs):
"""Get or set Git options.
If no kwargs, all options are returned. Otherwise kwargs are set.
"""
response = {"code": 1}
if len(kwargs):
output = []
for k, v in filter(
lambda t: True if t[0] in ALLOWED_OPTIONS else False, kwargs.items()
):
cmd = ["git", "config", "--add", k, v]
code, out, err = await execute(cmd, cwd=top_repo_path)
output.append(out.strip())
response["code"] = code
if code != 0:
response["command"] = " ".join(cmd)
response["message"] = err.strip()
return response
response["message"] = "\n".join(output).strip()
else:
cmd = ["git", "config", "--list"]
code, output, error = await execute(cmd, cwd=top_repo_path)
response = {"code": code}
if code != 0:
response["command"] = " ".join(cmd)
response["message"] = error.strip()
else:
raw = output.strip()
s = CONFIG_PATTERN.split(raw)
response["options"] = {k:v for k, v in zip(s[1::2], s[2::2]) if k in ALLOWED_OPTIONS}
return response
async def changed_files(self, base=None, remote=None, single_commit=None):
"""Gets the list of changed files between two Git refs, or the files changed in a single commit
There are two reserved "refs" for the base
1. WORKING : Represents the Git working tree
2. INDEX: Represents the Git staging area / index
Keyword Arguments:
single_commit {string} -- The single commit ref
base {string} -- the base Git ref
remote {string} -- the remote Git ref
Returns:
dict -- the response of format {
"code": int, # Command status code
"files": [string, string], # List of files changed.
"message": [string] # Error response
}
"""
if single_commit:
cmd = ["git", "diff", "{}^!".format(single_commit), "--name-only", "-z"]
elif base and remote:
if base == "WORKING":
cmd = ["git", "diff", remote, "--name-only", "-z"]
elif base == "INDEX":
cmd = ["git", "diff", "--staged", remote, "--name-only", "-z"]
else:
cmd = ["git", "diff", base, remote, "--name-only", "-z"]
else:
raise tornado.web.HTTPError(
400, "Either single_commit or (base and remote) must be provided"
)
response = {}
try:
code, output, error = await execute(cmd, cwd=self.root_dir)
except subprocess.CalledProcessError as e:
response["code"] = e.returncode
response["message"] = e.output.decode("utf-8")
else:
response["code"] = code
if code != 0:
response["command"] = " ".join(cmd)
response["message"] = error
else:
response["files"] = strip_and_split(output)
return response
async def clone(self, current_path, repo_url, auth=None):
"""
Execute `git clone`.
When no auth is provided, disables prompts for the password to avoid the terminal hanging.
When auth is provided, await prompts for username/passwords and sends them
:param current_path: the directory where the clone will be performed.
:param repo_url: the URL of the repository to be cloned.
:param auth: OPTIONAL dictionary with 'username' and 'password' fields
:return: response with status code and error message.
"""
env = os.environ.copy()
if auth:
env["GIT_TERMINAL_PROMPT"] = "1"
code, _, error = await execute(
["git", "clone", unquote(repo_url), "-q"],
username=auth["username"],
password=auth["password"],
cwd=os.path.join(self.root_dir, current_path),
env=env,
)
else:
env["GIT_TERMINAL_PROMPT"] = "0"
code, _, error = await execute(
["git", "clone", unquote(repo_url)],
cwd=os.path.join(self.root_dir, current_path),
env=env,
)
response = {"code": code}
if code != 0:
response["message"] = error.strip()
return response
async def status(self, current_path):
"""
Execute git status command & return the result.
"""
cmd = ["git", "status", "--porcelain", "-u", "-z"]
code, my_output, my_error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path),
)
if code != 0:
return {
"code": code,
"command": " ".join(cmd),
"message": my_error,
}
# Add attribute `is_binary`
command = [ # Compare stage to an empty tree see `_is_binary`
"git",
"diff",
"--numstat",
"-z",
"--cached",
"4b825dc642cb6eb9a060e54bf8d69288fbee4904"
]
text_code, text_output, _ = await execute(
command, cwd=os.path.join(self.root_dir, current_path),
)
are_binary = dict()
if text_code == 0:
for line in filter(lambda l: len(l) > 0, strip_and_split(text_output)):
diff, name = line.rsplit("\t", maxsplit=1)
are_binary[name] = diff.startswith("-\t-")
result = []
line_iterable = (line for line in strip_and_split(my_output) if line)
for line in line_iterable:
name = line[3:]
result.append({
"x": line[0],
"y": line[1],
"to": name,
# if file was renamed, next line contains original path
"from": next(line_iterable) if line[0]=='R' else name,
"is_binary": are_binary.get(name, None)
})
return {"code": code, "files": result}
async def log(self, current_path, history_count=10):
"""
Execute git log command & return the result.
"""
cmd = [
"git",
"log",
"--pretty=format:%H%n%an%n%ar%n%s",
("-%d" % history_count),
]
code, my_output, my_error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path),
)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": my_error}
result = []
line_array = my_output.splitlines()
i = 0
PREVIOUS_COMMIT_OFFSET = 4
while i < len(line_array):
if i + PREVIOUS_COMMIT_OFFSET < len(line_array):
result.append(
{
"commit": line_array[i],
"author": line_array[i + 1],
"date": line_array[i + 2],
"commit_msg": line_array[i + 3],
"pre_commit": line_array[i + PREVIOUS_COMMIT_OFFSET],
}
)
else:
result.append(
{
"commit": line_array[i],
"author": line_array[i + 1],
"date": line_array[i + 2],
"commit_msg": line_array[i + 3],
"pre_commit": "",
}
)
i += PREVIOUS_COMMIT_OFFSET
return {"code": code, "commits": result}
async def detailed_log(self, selected_hash, current_path):
"""
Execute git log -1 --numstat --oneline -z command (used to get
insertions & deletions per file) & return the result.
"""
cmd = ["git", "log", "-1", "--numstat", "--oneline", "-z", selected_hash]
code, my_output, my_error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path),
)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": my_error}
total_insertions = 0
total_deletions = 0
result = []
line_iterable = iter(strip_and_split(my_output)[1:])
for line in line_iterable:
is_binary = line.startswith("-\t-\t")
insertions, deletions, file = line.split('\t')
insertions = insertions.replace('-', '0')
deletions = deletions.replace('-', '0')
if file == '':
# file was renamed or moved, we need next two lines of output
from_path = next(line_iterable)
to_path = next(line_iterable)
modified_file_name = from_path + " => " + to_path
modified_file_path = to_path
else:
modified_file_name = file.split("/")[-1]
modified_file_path = file
result.append({
"modified_file_path": modified_file_path,
"modified_file_name": modified_file_name,
"insertion": insertions,
"deletion": deletions,
"is_binary": is_binary
})
total_insertions += int(insertions)
total_deletions += int(deletions)
modified_file_note = "{num_files} files changed, {insertions} insertions(+), {deletions} deletions(-)".format(
num_files=len(result),
insertions=total_insertions,
deletions=total_deletions)
return {
"code": code,
"modified_file_note": modified_file_note,
"modified_files_count": str(len(result)),
"number_of_insertions": str(total_insertions),
"number_of_deletions": str(total_deletions),
"modified_files": result,
}
async def diff(self, top_repo_path):
"""
Execute git diff command & return the result.
"""
cmd = ["git", "diff", "--numstat", "-z"]
code, my_output, my_error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": my_error}
result = []
line_array = strip_and_split(my_output)
for line in line_array:
linesplit = line.split()
result.append(
{
"insertions": linesplit[0],
"deletions": linesplit[1],
"filename": linesplit[2],
}
)
return {"code": code, "result": result}
async def branch(self, current_path):
"""
Execute 'git for-each-ref' command & return the result.
"""
heads = await self.branch_heads(current_path)
if heads["code"] != 0:
# error; bail
return heads
remotes = await self.branch_remotes(current_path)
if remotes["code"] != 0:
# error; bail
return remotes
# all's good; concatenate results and return
return {
"code": 0,
"branches": heads["branches"] + remotes["branches"],
"current_branch": heads["current_branch"],
}
async def branch_heads(self, current_path):
"""
Execute 'git for-each-ref' command on refs/heads & return the result.
"""
# Format reference: https://git-scm.com/docs/git-for-each-ref#_field_names
formats = ["refname:short", "objectname", "upstream:short", "HEAD"]
cmd = [
"git",
"for-each-ref",
"--format=" + "%09".join("%({})".format(f) for f in formats),
"refs/heads/",
]
code, output, error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path)
)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
current_branch = None
results = []
try:
for name, commit_sha, upstream_name, is_current_branch in (
line.split("\t") for line in output.splitlines()
):
is_current_branch = bool(is_current_branch.strip())
branch = {
"is_current_branch": is_current_branch,
"is_remote_branch": False,
"name": name,
"upstream": upstream_name if upstream_name else None,
"top_commit": commit_sha,
"tag": None,
}
results.append(branch)
if is_current_branch:
current_branch = branch
# Above can fail in certain cases, such as an empty repo with
# no commits. In that case, just fall back to determining
# current branch
if not current_branch:
current_name = await self.get_current_branch(current_path)
branch = {
"is_current_branch": True,
"is_remote_branch": False,
"name": current_name,
"upstream": None,
"top_commit": None,
"tag": None,
}
results.append(branch)
current_branch = branch
return {
"code": code,
"branches": results,
"current_branch": current_branch,
}
except Exception as downstream_error:
return {
"code": -1,
"command": " ".join(cmd),
"message": str(downstream_error),
}
async def branch_remotes(self, current_path):
"""
Execute 'git for-each-ref' command on refs/heads & return the result.
"""
# Format reference: https://git-scm.com/docs/git-for-each-ref#_field_names
formats = ["refname:short", "objectname"]
cmd = [
"git",
"for-each-ref",
"--format=" + "%09".join("%({})".format(f) for f in formats),
"refs/remotes/",
]
code, output, error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path)
)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
results = []
try:
for name, commit_sha in (
line.split("\t") for line in output.splitlines()
):
results.append(
{
"is_current_branch": False,
"is_remote_branch": True,
"name": name,
"upstream": None,
"top_commit": commit_sha,
"tag": None,
}
)
return {"code": code, "branches": results}
except Exception as downstream_error:
return {
"code": -1,
"command": " ".join(cmd),
"message": str(downstream_error),
}
async def show_top_level(self, current_path):
"""
Execute git --show-toplevel command & return the result.
"""
cmd = ["git", "rev-parse", "--show-toplevel"]
code, my_output, my_error = await execute(
cmd,
cwd=os.path.join(self.root_dir, current_path),
)
if code == 0:
return {"code": code, "top_repo_path": my_output.strip("\n")}
else:
return {
"code": code,
"command": " ".join(cmd),
"message": my_error,
}
async def show_prefix(self, current_path):
"""
Execute git --show-prefix command & return the result.
"""
cmd = ["git", "rev-parse", "--show-prefix"]
code, my_output, my_error = await execute(
cmd,
cwd=os.path.join(self.root_dir, current_path),
)
if code == 0:
result = {"code": code, "under_repo_path": my_output.strip("\n")}
return result
else:
return {
"code": code,
"command": " ".join(cmd),
"message": my_error,
}
async def add(self, filename, top_repo_path):
"""
Execute git add<filename> command & return the result.
"""
if not isinstance(filename, str):
# assume filename is a sequence of str
cmd = ["git", "add"] + list(filename)
else:
cmd = ["git", "add", filename]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def add_all(self, top_repo_path):
"""
Execute git add all command & return the result.
"""
cmd = ["git", "add", "-A"]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def add_all_unstaged(self, top_repo_path):
"""
Execute git add all unstaged command & return the result.
"""
cmd = ["git", "add", "-u"]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def add_all_untracked(self, top_repo_path):
"""
Find all untracked files, execute git add & return the result.
"""
status = await self.status(top_repo_path)
if status["code"] != 0:
return status
untracked = []
for f in status["files"]:
if f["x"]=="?" and f["y"]=="?":
untracked.append(f["from"].strip('"'))
return await self.add(untracked, top_repo_path)
async def reset(self, filename, top_repo_path):
"""
Execute git reset <filename> command & return the result.
"""
cmd = ["git", "reset", "--", filename]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def reset_all(self, top_repo_path):
"""
Execute git reset command & return the result.
"""
cmd = ["git", "reset"]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def delete_commit(self, commit_id, top_repo_path):
"""
Delete a specified commit from the repository.
"""
cmd = ["git", "revert", "--no-commit", commit_id]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def reset_to_commit(self, commit_id, top_repo_path):
"""
Reset the current branch to a specific past commit.
"""
cmd = ["git", "reset", "--hard"]
if commit_id:
cmd.append(commit_id)
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def checkout_new_branch(self, branchname, startpoint, current_path):
"""
Execute git checkout <make-branch> command & return the result.
"""
cmd = ["git", "checkout", "-b", branchname, startpoint]
code, my_output, my_error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path),
)
if code == 0:
return {"code": code, "message": my_output}
else:
return {
"code": code,
"command": " ".join(cmd),
"message": my_error,
}
async def _get_branch_reference(self, branchname, current_path):
"""
Execute git rev-parse --symbolic-full-name <branch-name> and return the result (or None).
"""
code, my_output, _ = await execute(
["git", "rev-parse", "--symbolic-full-name", branchname],
cwd=os.path.join(self.root_dir, current_path),
)
if code == 0:
return my_output.strip("\n")
else:
return None
async def checkout_branch(self, branchname, current_path):
"""
Execute git checkout <branch-name> command & return the result.
Use the --track parameter for a remote branch.
"""
reference_name = await self._get_branch_reference(branchname, current_path)
if reference_name is None:
is_remote_branch = False
else:
is_remote_branch = self._is_remote_branch(reference_name)
if is_remote_branch:
cmd = ["git", "checkout", "--track", branchname]
else:
cmd = ["git", "checkout", branchname]
code, my_output, my_error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path)
)
if code == 0:
return {"code": 0, "message": my_output}
else:
return {"code": code, "message": my_error, "command": " ".join(cmd)}
async def checkout(self, filename, top_repo_path):
"""
Execute git checkout command for the filename & return the result.
"""
cmd = ["git", "checkout", "--", filename]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def checkout_all(self, top_repo_path):
"""
Execute git checkout command & return the result.
"""
cmd = ["git", "checkout", "--", "."]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def commit(self, commit_msg, top_repo_path):
"""
Execute git commit <filename> command & return the result.
"""
cmd = ["git", "commit", "-m", commit_msg]
code, _, error = await execute(cmd, cwd=top_repo_path)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
async def pull(self, curr_fb_path, auth=None, cancel_on_conflict=False):
"""
Execute git pull --no-commit. Disables prompts for the password to avoid the terminal hanging while waiting
for auth.
"""
env = os.environ.copy()
if auth:
env["GIT_TERMINAL_PROMPT"] = "1"
code, output, error = await execute(
["git", "pull", "--no-commit"],
username=auth["username"],
password=auth["password"],
cwd=os.path.join(self.root_dir, curr_fb_path),
env=env,
)
else:
env["GIT_TERMINAL_PROMPT"] = "0"
code, output, error = await execute(
["git", "pull", "--no-commit"],
env=env,
cwd=os.path.join(self.root_dir, curr_fb_path),
)
response = {"code": code}
if code != 0:
output = output.strip()
has_conflict = "automatic merge failed; fix conflicts and then commit the result." in output.lower()
if cancel_on_conflict and has_conflict:
code, _, error = await execute(
["git", "merge", "--abort"],
cwd=os.path.join(self.root_dir, curr_fb_path)
)
if code == 0:
response["message"] = "Unable to pull latest changes as doing so would result in a merge conflict. In order to push your local changes, you may want to consider creating a new branch based on your current work and pushing the new branch. Provided your repository is hosted (e.g., on GitHub), once pushed, you can create a pull request against the original branch on the remote repository and manually resolve the conflicts during pull request review."
else:
response["message"] = error.strip()
elif has_conflict:
response["message"] = output
else:
response["message"] = error.strip()
return response
async def push(self, remote, branch, curr_fb_path, auth=None):
"""
Execute `git push $UPSTREAM $BRANCH`. The choice of upstream and branch is up to the caller.
"""
env = os.environ.copy()
if auth:
env["GIT_TERMINAL_PROMPT"] = "1"
code, _, error = await execute(
["git", "push", remote, branch],
username=auth["username"],
password=auth["password"],
cwd=os.path.join(self.root_dir, curr_fb_path),
env=env,
)
else:
env["GIT_TERMINAL_PROMPT"] = "0"
code, _, error = await execute(
["git", "push", remote, branch],
env=env,
cwd=os.path.join(self.root_dir, curr_fb_path),
)
response = {"code": code}
if code != 0:
response["message"] = error.strip()
return response
async def init(self, current_path):
"""
Execute git init command & return the result.
"""
cmd = ["git", "init"]
code, _, error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path)
)
if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
return {"code": code}
def _is_remote_branch(self, branch_reference):
"""Check if given branch is remote branch by comparing with 'remotes/',
TODO : Consider a better way to check remote branch
"""
return branch_reference.startswith("refs/remotes/")
async def get_current_branch(self, current_path):
"""Use `symbolic-ref` to get the current branch name. In case of
failure, assume that the HEAD is currently detached, and fall back
to the `branch` command to get the name.
See https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
"""
command = ["git", "symbolic-ref", "HEAD"]
code, output, error = await execute(
command, cwd=os.path.join(self.root_dir, current_path)
)
if code == 0:
return output.split("/")[-1].strip()
elif "not a symbolic ref" in error.lower():
current_branch = await self._get_current_branch_detached(current_path)
return current_branch
else:
raise Exception(
"Error [{}] occurred while executing [{}] command to get current branch.".format(
error, " ".join(command)
)
)
async def _get_current_branch_detached(self, current_path):
"""Execute 'git branch -a' to get current branch details in case of detached HEAD
"""
command = ["git", "branch", "-a"]
code, output, error = await execute(
command, cwd=os.path.join(self.root_dir, current_path)
)
if code == 0:
for branch in output.splitlines():
branch = branch.strip()
if branch.startswith("*"):
return branch.lstrip("* ")
else:
raise Exception(
"Error [{}] occurred while executing [{}] command to get detached HEAD name.".format(
error, " ".join(command)
)
)
async def get_upstream_branch(self, current_path, branch_name):
"""Execute 'git rev-parse --abbrev-ref branch_name@{upstream}' to get
upstream branch name tracked by given local branch.
Reference : https://git-scm.com/docs/git-rev-parse#git-rev-parse-emltbranchnamegtupstreamemegemmasterupstreamememuem
"""
command = [
"git",
"rev-parse",
"--abbrev-ref",
"{}@{{upstream}}".format(branch_name),
]
code, output, error = await execute(
command, cwd=os.path.join(self.root_dir, current_path)
)
if code == 0:
return output.strip()
elif "fatal: no upstream configured for branch" in error.lower():
return None
elif "unknown revision or path not in the working tree" in error.lower():
return None
else:
raise Exception(
"Error [{}] occurred while executing [{}] command to get upstream branch.".format(
error, " ".join(command)
)
)
async def _get_tag(self, current_path, commit_sha):
"""Execute 'git describe commit_sha' to get
nearest tag associated with lastest commit in branch.
Reference : https://git-scm.com/docs/git-describe#git-describe-ltcommit-ishgt82308203
"""
command = ["git", "describe", "--tags", commit_sha]
code, output, error = await execute(
command, cwd=os.path.join(self.root_dir, current_path)
)
if code == 0:
return output.strip()
elif "fatal: no tags can describe '{}'.".format(commit_sha) in error.lower():
return None
elif "fatal: no names found" in error.lower():
return None
else:
raise Exception(
"Error [{}] occurred while executing [{}] command to get nearest tag associated with branch.".format(
error, " ".join(command)
)
)
async def show(self, filename, ref, top_repo_path):
"""
Execute git show <ref:filename> command & return the result.
"""
command = ["git", "show", "{}:{}".format(ref, filename)]
code, output, error = await execute(command, cwd=top_repo_path)
error_messages = map(
lambda n: n.lower(),
[
"fatal: Path '{}' exists on disk, but not in '{}'".format(
filename, ref
),
"fatal: Path '{}' does not exist (neither on disk nor in the index)".format(
filename
),
],
)
lower_error = error.lower()
if code == 0:
return output
elif any([msg in lower_error for msg in error_messages]):
return ""