-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.py
More file actions
1227 lines (940 loc) · 41.1 KB
/
Copy pathgit.py
File metadata and controls
1227 lines (940 loc) · 41.1 KB
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
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import shutil
import sys
import tempfile
from cmdrunner import CommandException, default_returncode_handler, \
run_command, run_generator
# Python3 redefined 'unicode' to be 'str'
if sys.version_info[0] >= 3:
unicode = str
COMMIT_TOP_PAT = None
COMMIT_CHG_PAT = None
COMMIT_INS_PAT = None
COMMIT_DEL_PAT = None
ISSUE_OPEN_PAT = None
class GitException(Exception):
"General Git exception"
class GitAddIgnoredException(GitException):
def __init__(self, files):
self.__files = files
msg = "Cannot add files listed in .gitignore: %s" % \
" ,".join(self.__files)
super(GitAddIgnoredException, self).__init__(msg)
@property
def files(self):
for entry in self.__files:
yield entry
class GitBadPathspecException(GitException):
"General Git exception"
def __init__(self, pathspec):
self.__pathspec = pathspec
msg = "Pathspec \"%s\" not found" % (self.__pathspec, )
super(GitBadPathspecException, self).__init__(msg)
@property
def pathspec(self):
return self.__pathspec
class GitUntrackedException(GitException):
"Git exception capturing list of untracked files"
def __init__(self, untracked):
self.__untracked = untracked
msg = "Found untracked files: %s" % ", ".join(self.__untracked)
super(GitUntrackedException, self).__init__(msg)
@property
def files(self):
for name in self.__untracked:
yield name
def __handle_generic_stderr(cmdname, line, verbose=False):
if line[:6].lower().startswith("error:"):
raise GitException(line[6:].strip())
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
class ErrorCatcher(object):
"""
Cache all lines of stderr output and throw a single exception after
the command has finished
"""
def __init__(self):
self.__errors = None
def finalize_stderr(self, cmdname, verbose=False):
if self.__errors is not None:
raise GitException("\n".join(self.__errors))
def flush_errors(self):
self.__errors = None
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
if self.__errors is None:
self.__errors = [line, ]
else:
self.__errors.append(line)
class AddHandler(ErrorCatcher):
"Handle errors for git_add()"
def __init__(self):
self.__saw_ignored_error = False
self.__ignored = None
super(AddHandler, self).__init__()
def finalize_stderr(self, cmdname, verbose=False):
if self.__ignored is not None:
raise GitAddIgnoredException(files=self.__ignored)
super(AddHandler, self).finalize_stderr(cmdname, verbose=True)
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
if self.__saw_ignored_error:
if line.find("Use -f if you really want to add them") >= 0:
self.__saw_ignored_error = False
else:
if self.__ignored is None:
self.__ignored = []
self.__ignored.append(line)
return
if line.find("The following paths are ignored by one of your ") >= 0:
self.__saw_ignored_error = True
return
super(AddHandler, self).handle_stderr(cmdname, line, verbose=False)
def git_add(filelist, sandbox_dir=None, debug=False, dry_run=False,
verbose=False):
"Add the specified files/directories to the GIT commit index"
if isinstance(filelist, (tuple, list)):
cmd_args = ["git", "add"] + filelist
else:
cmd_args = ("git", "add", unicode(filelist))
handler = AddHandler()
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_finalizer=handler.finalize_stderr,
stderr_handler=handler.handle_stderr, debug=debug,
verbose=verbose)
def git_autocrlf(sandbox_dir=None, debug=False, dry_run=False, verbose=False):
"Hack around overeager Git line policing"
cmd_args = ("git", "config", "--global", "core.autocrlf", "false")
run_command(cmd_args, cmdname="GIT AUTOCRLF",
working_directory=sandbox_dir, debug=debug, dry_run=dry_run,
verbose=verbose)
def git_current_branch(sandbox_dir=None, debug=False, dry_run=False,
verbose=False):
"Return the current branch"
cmd_args = ("git", "branch", "--show-current")
branch_name = None
for line in run_generator(cmd_args, cmdname="GIT CURRENT_BRANCH",
working_directory=sandbox_dir, debug=debug,
dry_run=dry_run, verbose=verbose):
if branch_name is None:
branch_name = line.rstrip()
else:
print("WARNING: Ignoring extra line from"
" 'git branch --show-current': %s" % line.rstrip())
return branch_name
class ChkoutHandler(ErrorCatcher):
"Handle errors for git_checkout()"
def __init__(self):
self.__detached = False
super(ChkoutHandler, self).__init__()
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
# ignore 'detached HEAD' message
if line.startswith("You are in 'detached HEAD' state."):
self.__detached = True
self.flush_errors()
return
if self.__detached:
return
if line.startswith("Switched to a new branch"):
return
if line.startswith("Switched to branch "):
return
if line.startswith("Already on "):
return
if line.find("unable to rmdir ") >= 0:
if verbose:
print("%s" % (line, ), file=sys.stderr)
return
no_match_back = line.find("' did not match any ")
if no_match_back > 0:
front_str = " pathspec '"
no_match_front = line.find(front_str)
if no_match_back >= 0:
pathspec = line[no_match_front+len(front_str):no_match_back]
raise GitBadPathspecException(pathspec)
super(ChkoutHandler, self).handle_stderr(cmdname, line)
def git_checkout(branch_name=None, files=None, new_branch=False,
recurse_submodules=False, start_point=None, sandbox_dir=None,
debug=False, dry_run=False, verbose=False):
"Check out a branch of the Git repository"
cmd_args = ["git", "checkout"]
if files is not None:
cmd_args.append("--")
if not isinstance(files, (tuple, list)):
cmd_args.append(files)
else:
cmd_args += files
else:
if new_branch:
cmd_args.append("-b")
if branch_name is not None:
cmd_args.append(branch_name)
if recurse_submodules:
cmd_args.append("--recurse-submodules")
if start_point is not None:
cmd_args.append(unicode(start_point))
handler = ChkoutHandler()
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_finalizer=handler.finalize_stderr,
stderr_handler=handler.handle_stderr, debug=debug,
dry_run=dry_run, verbose=verbose)
class CloneHandler(object):
RECURSE_SUPPORTED = True
def __init__(self):
self.__recurse_error = False
@classmethod
def __disable_recurse(cls):
cls.RECURSE_SUPPORTED = False
def clear_recurse_error(self):
self.__recurse_error = False
@classmethod
def handle_clone_stderr(cls, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
if line.startswith("Cloning into "):
return
if line.find("You appear to have cloned") >= 0:
return
if line.startswith("Updating files: "):
return
if line.startswith("Submodule ") and \
line.find(" registered for path ") > 0:
return
raise GitException("%s failed: %s" % (cmdname, line))
def handle_rtncode(self, cmdname, rtncode, lines, verbose=False):
if not self.__recurse_error:
default_returncode_handler(cmdname, rtncode, lines,
verbose=verbose)
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
if self.__recurse_error:
# ignore all errors after a 'recurse-submodules' error
return
if line.startswith("error: unknown option") and \
line.find("recurse-submodules") > 0:
self.__recurse_error = True
self.__disable_recurse()
return
self.handle_clone_stderr(cmdname, line, verbose=False)
@property
def saw_recurse_error(self):
return self.__recurse_error
def git_clone(url, recurse_submodules=False, sandbox_dir=None, target_dir=None,
debug=False, dry_run=False, verbose=False):
"""
Clone a Git repository
sandbox_dir - if specified, create the cloned directory under `sandbox_dir`
target_dir = if specified, use `target_dir` as the name of the cloned
directory
"""
handler = CloneHandler()
for new_recurse in CloneHandler.RECURSE_SUPPORTED, False:
cmd_args = ["git", "clone"]
if recurse_submodules:
cmd_args.append("--recurse-submodules" if new_recurse
else "--recursive")
cmd_args.append(url)
if target_dir is not None:
cmd_args.append(target_dir)
handler.clear_recurse_error()
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
returncode_handler=handler.handle_rtncode,
stderr_handler=handler.handle_stderr, debug=debug,
dry_run=dry_run, verbose=verbose)
if not handler.saw_recurse_error:
break
class CommitHandler(object):
"Retry 'svn commit' command if it times out"
COMMIT_TOP_PAT = None
COMMIT_CHG_PAT = None
COMMIT_INS_PAT = None
COMMIT_DEL_PAT = None
def __init__(self, sandbox_dir=None, author=None, commit_message=None,
date_string=None, filelist=None, allow_empty=False,
commit_all=False, debug=False, dry_run=False, verbose=False):
self.__init_regexps()
if sandbox_dir is None:
self.__sandbox_dir = "."
else:
self.__sandbox_dir = sandbox_dir
self.__extra_args = []
if author is not None:
self.__extra_args.append("--author=%s" % author)
if allow_empty:
# allow 'empty' commits with no changes from the previous commit
# This is used for the root commit of SVN branches which are
# identical to the commit from which they branches.
self.__extra_args.append("--allow-empty")
if date_string is not None:
os.environ["GIT_COMMITTER_DATE"] = date_string
self.__extra_args.append("--date=%s" % date_string)
if filelist is not None:
self.__extra_args += filelist
elif commit_all:
self.__extra_args.append("-a")
self.__commit_message = commit_message
self.__debug = debug
self.__dry_run = dry_run
self.__verbose = verbose
self.__saw_error = False
self.__auto_pack_err = False
self.__branch = None
self.__hash_id = None
self.__changed = None
self.__inserted = None
self.__deleted = None
@classmethod
def __init_regexps(cls):
if cls.COMMIT_TOP_PAT is None:
cls.COMMIT_TOP_PAT = re.compile(r"^\s*\[(\S+)\s+"
r"(?:\(root-commit\)\s+)?(\S+)\]"
r" (.*)\s*$")
if cls.COMMIT_CHG_PAT is None:
cls.COMMIT_CHG_PAT = re.compile(r"^\s*(\d+) files? changed(.*)$")
if cls.COMMIT_INS_PAT is None:
cls.COMMIT_INS_PAT = re.compile(r" (\d+) insertion")
if cls.COMMIT_DEL_PAT is None:
cls.COMMIT_DEL_PAT = re.compile(r" (\d+) deletion")
def __hndl_rtncd(self, cmdname, rtncode, lines, verbose=False):
if not self.__saw_error:
default_returncode_handler(cmdname, rtncode, lines,
verbose=verbose)
def handle_stderr(self, cmdname, line, verbose=False):
if self.__verbose:
print("%s!! %s" % (cmdname, line), file=sys.stderr)
if line.find("Auto packing the repository") >= 0:
self.__auto_pack_err = True
elif self.__auto_pack_err:
if line.find("for manual housekeeping") < 0:
print("!!AutoPack!! %s" % (line, ), file=sys.stderr)
else:
self.__saw_error = True
raise GitException("Commit failed: %s" % line)
def __process_line(self, line):
# check for initial commit line with Git branch/hash info
if self.__branch is None and self.__hash_id is None:
mtch = self.COMMIT_TOP_PAT.match(line)
if mtch is not None:
self.__branch = mtch.group(1)
self.__hash_id = mtch.group(2)
return
if line.startswith("On branch "):
self.__branch = line[10:]
return
raise GitException("Bad first line of commit: %s" % line)
# check for changed/inserted/deleted line
if self.__changed is None or self.__inserted is None or \
self.__deleted is None:
mtch = self.COMMIT_CHG_PAT.match(line)
if mtch is not None:
self.__changed = int(mtch.group(1))
stuff = mtch.group(2)
srch = self.COMMIT_INS_PAT.search(stuff)
if srch is not None:
self.__inserted = int(mtch.group(1))
else:
self.__inserted = 0
srch = self.COMMIT_DEL_PAT.search(stuff)
if srch is not None:
self.__deleted = int(mtch.group(1))
else:
self.__deleted = 0
return
if line.find("delete mode ") >= 0:
return
if self.__verbose:
print("COMMIT IGNORED>> %s" % (line, ))
def run_handler(self):
logfile = tempfile.NamedTemporaryFile(mode="w", delete=False)
try:
# write log message to a temporary file
if self.__commit_message is None:
commit_file = os.devnull
else:
print("%s" % self.__commit_message, file=logfile, end="")
logfile.close()
commit_file = logfile.name
cmd_args = ["git", "commit", "-F", commit_file] + self.__extra_args
cmdname = " ".join(cmd_args[:2]).upper()
while True:
for line in run_generator(cmd_args, cmdname=cmdname,
returncode_handler=self.__hndl_rtncd,
stderr_handler=self.handle_stderr,
working_directory=self.__sandbox_dir,
debug=self.__debug,
dry_run=self.__dry_run,
verbose=self.__verbose):
self.__process_line(line)
# no errors seen, we're done
if not self.__saw_error:
break
# reset flags and try again
self.__saw_error = False
self.__auto_pack_err = False
finally:
os.unlink(logfile.name)
return self.tuple
@property
def tuple(self):
return (self.__branch, self.__hash_id, self.__changed,
self.__inserted, self.__deleted)
def git_commit(sandbox_dir=None, author=None, commit_message=None,
date_string=None, filelist=None, allow_empty=False,
commit_all=False, debug=False, dry_run=False, verbose=False):
"""
Commit all changes to the local repository
Return a tuple containing:
(branch_name, hash_id, number_changed, number_inserted, number_deleted)
"""
handler = CommitHandler(sandbox_dir, author, commit_message, date_string,
filelist, allow_empty=allow_empty,
commit_all=commit_all, debug=debug,
dry_run=dry_run, verbose=verbose)
return handler.run_handler()
def __config_returncode_handler(cmdname, returncode, saved_output,
verbose=False):
if not verbose:
print("Output from '%s'" % cmdname, file=sys.stderr)
for line in saved_output:
print(">> %s" % line, file=sys.stderr)
def git_config(name, value=None, get_value=False, sandbox_dir=None,
debug=False, dry_run=False, verbose=False):
"Set a repository or global option"
if not get_value and value is None:
raise GitException("No value supplied for config option \"%s\"" %
(name, ))
elif get_value and value is not None:
raise GitException("Cannot supply value while get_value is True for"
" config option \"%s\"" % (name, ))
cmd_args = ["git", "config"]
cmd_args.append(unicode(name))
if not get_value:
cmd_args.append(unicode(value))
returned_value = None
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
returncode_handler=__config_returncode_handler,
debug=debug,
dry_run=dry_run, verbose=verbose):
if get_value:
if returned_value is not None:
raise GitException("Found multiple values for %s:"
" \"%s\" and \"%s\"" %
(name, returned_value, line))
returned_value = line
elif line != "":
raise GitException("%s returned \"%s\"" %
(" ".join(cmd_args[:2]), line))
return returned_value
def git_diff(unified=False, sandbox_dir=None, debug=False, dry_run=False,
verbose=False):
"Return a list of changes to all files"
cmd_args = ["git", "diff"]
if unified:
cmd_args.append("-U")
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_handler=__handle_generic_stderr,
debug=debug, dry_run=dry_run, verbose=verbose):
yield line
def git_fetch(remote=None, fetch_all=False, sandbox_dir=None, debug=False,
dry_run=False, verbose=False):
"""
Fetch all changes from the remote repository
"""
cmd_args = ["git", "fetch"]
if fetch_all:
cmd_args.append("--all")
if remote is not None:
cmd_args.append(unicode(remote))
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_handler=__handle_generic_stderr, debug=debug,
dry_run=dry_run, verbose=verbose)
def git_init(bare=False, sandbox_dir=None, template=None, debug=False,
dry_run=False, verbose=False):
"Add the specified file/directory to the SVN commit"
cmd_args = ["git", "init"]
if bare:
cmd_args.append("--bare")
sandbox_dir, project = sandbox_dir.rsplit("/", 1)
if not project.endswith(".git"):
project += ".git"
cmd_args.append(project)
if template is not None:
cmd_args.append("--template=%s" % (template, ))
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir, debug=debug, dry_run=dry_run,
verbose=verbose)
def git_list_branches(sandbox_dir=None, debug=False, dry_run=False,
verbose=False):
"""
Return a list of all branches, where the first element is the default
branch
"""
cmd_args = ("git", "branch", "--list")
branches = []
default_branch = None
for line in run_generator(cmd_args, cmdname="GIT CURRENT_BRANCH",
working_directory=sandbox_dir, debug=debug,
dry_run=dry_run, verbose=verbose):
branch_name = line.rstrip()
if branch_name.startswith("*"):
default_branch = branch_name[1:].strip()
else:
branches.append(branch_name.strip())
if default_branch is None:
print("WARNING: No default branch found in %s" % (sandbox_dir, ),
file=sys.stderr)
else:
branches.insert(0, default_branch)
return branches
def git_log(sandbox_dir=None, debug=False, dry_run=False, verbose=False):
"Return the log entries for the sandbox"
cmd_args = ("git", "log")
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir, debug=debug,
dry_run=dry_run, verbose=verbose):
yield line
(LIST_CACHED, LIST_DELETED, LIST_IGNORED, LIST_KILLED,
LIST_MODIFIED, LIST_OTHERS, LIST_STAGE, LIST_UNMERGED) = \
("cached", "deleted", "ignored", "killed",
"modified", "others", "stage", "unmerged")
LIST_OPTIONS = (LIST_CACHED, LIST_DELETED, LIST_IGNORED, LIST_KILLED,
LIST_MODIFIED, LIST_OTHERS, LIST_STAGE, LIST_UNMERGED)
def git_ls_files(filelist=None, list_option=None, sandbox_dir=None,
debug=False, dry_run=False, verbose=False):
"Remove the specified files/directories from the GIT commit index"
if list_option is not None:
if list_option not in LIST_OPTIONS:
raise GitException("Bad list option \"--%s\"" % (list_option, ))
flag = "--%s" % (list_option, )
elif filelist is None or len(filelist) == 0:
raise GitException("No files specified")
else:
flag = "-r"
if filelist is None:
cmd_args = ["git", "ls-files", flag]
elif isinstance(filelist, (tuple, list)):
cmd_args = ["git", "ls-files", flag] + filelist
else:
cmd_args = ("git", "ls-files", flag, unicode(filelist))
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_handler=__handle_generic_stderr,
debug=debug, dry_run=dry_run, verbose=verbose):
yield line
class PullHandler(object):
(PULL_SUBMOD_NO, PULL_SUBMOD_ON_DEMAND, PULL_SUBMOD_YES) = \
("no", "on-demand", "yes")
OPTIONS = (PULL_SUBMOD_NO, PULL_SUBMOD_ON_DEMAND, PULL_SUBMOD_YES)
# if True, throw an exception if any .svn data is found
CHECK_FOR_SVN_METADATA = True
def __init__(self):
self.__branches = None
self.__expect_error = False
self.__saw_untracked = False
self.__untracked = None
@property
def branches(self):
return self.__branches
def finalize_stderr(self, cmdname, verbose=False):
if self.__untracked is not None:
raise GitUntrackedException(self.__untracked)
def handle_rtncode(self, cmdname, rtncode, lines, verbose=False):
if not self.__expect_error:
default_returncode_handler(cmdname, rtncode, lines,
verbose=verbose)
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line, ), file=sys.stderr)
if self.__saw_untracked:
if line.startswith("Please move or remove them"):
self.__saw_untracked = False
return
if self.CHECK_FOR_SVN_METADATA and line.startswith(".svn/"):
raise Exception("Found SVN metadata in Git repo")
if self.__untracked is None:
self.__untracked = []
self.__untracked.append(line)
return
if line.startswith("* [new branch]"):
flds = line[14:].split("->")
if len(flds) != 2:
raise GitException("Bad 'pull' line: %s" % (line.rstrip(), ))
if self.__branches is None:
self.__branches = {}
self.__branches[flds[0].strip()] = flds[1].strip()
return
if line.startswith("error: ") and line.find(" untracked working ") > 0:
self.__saw_untracked = True
return
self.__expect_error = True
def git_pull(remote=None, branch=None, pull_all=False, recurse_submodules=None,
sandbox_dir=None, debug=False, dry_run=False, verbose=False):
"""
Pull all changes from the remote repository and merge them into the sandbox
"""
cmd_args = ["git", "pull"]
if pull_all:
cmd_args.append("--all")
if recurse_submodules is not None:
if recurse_submodules not in PullHandler.OPTIONS:
raise GitException("Bad --recurse-submodules argument \"%s\"" %
(recurse_submodules, ))
cmd_args += ("--recurse-submodules=%s" % (recurse_submodules, ))
if remote is not None and branch is not None:
cmd_args += (unicode(remote), unicode(branch))
elif remote is not None or branch is not None:
if remote is None:
raise GitException("'branch' argument is \"%s\" but 'remote'"
" is not specified" % (branch, ))
raise GitException("'remote' argument is \"%s\" but 'branch'"
" is not specified" % (remote, ))
handler = PullHandler()
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
returncode_handler=handler.handle_rtncode,
stderr_finalizer=handler.finalize_stderr,
stderr_handler=handler.handle_stderr, debug=debug,
dry_run=dry_run, verbose=verbose)
return handler.branches
def git_push(remote_name=None, upstream=None, sandbox_dir=None, debug=False,
dry_run=False, verbose=False):
"Push all changes to the remote Git repository"
cmd_args = ["git", "push"]
if upstream is not None:
cmd_args += ("-u", upstream)
if remote_name is not None:
cmd_args.append(remote_name)
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
stderr_handler=__handle_generic_stderr,
debug=debug, dry_run=dry_run, verbose=verbose):
yield line
def git_remote_add(remote_name, url, sandbox_dir=None, debug=False,
dry_run=False, verbose=False):
"Add a new remote to the Git sandbox"
cmd_args = ("git", "remote", "add", remote_name, url)
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:3]).upper(),
working_directory=sandbox_dir, debug=debug,
dry_run=dry_run, verbose=verbose):
yield line
def git_rev_parse(object_name, abbrev_ref=None, sandbox_dir=None, debug=False,
dry_run=False, verbose=False):
"Add a new remote to the Git sandbox"
cmd_args = ["git", "rev-parse"]
if abbrev_ref is not None:
if abbrev_ref is True:
cmd_args.append("--abbrev-ref")
elif abbrev_ref in ("strict", "loose"):
cmd_args.append("--abbrev-ref=%s" % abbrev_ref)
else:
raise GitException("Bad mode \"%s\" for --abbrev-ref" %
(abbrev_ref, ))
cmd_args.append(object_name)
rev_hash = None
for line in run_generator(cmd_args, cmdname=" ".join(cmd_args[:3]).upper(),
working_directory=sandbox_dir, debug=debug,
dry_run=dry_run, verbose=verbose):
if rev_hash is not None:
if sandbox_dir is None:
sandbox_dir = "."
raise Exception("Found multiple hash values for \"%s\" in %s" %
(object_name, sandbox_dir))
rev_hash = line.rstrip()
return rev_hash
class RemoveHandler(object):
MATCH_PAT = re.compile(r"^.*fatal: pathspec .\(.*\). did not match any"
r" files\s*$")
def __init__(self):
self.__expect_error = False
self.__migrating = False
def handle_rtncode(self, cmdname, rtncode, lines, verbose=False):
if self.__expect_error:
default_returncode_handler(cmdname, rtncode, lines,
verbose=verbose)
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line, ), file=sys.stderr)
if self.__migrating or line.startswith("Migrating git directory"):
print("%s## %s (ignored)" % (cmdname, line, ))
self.__migrating = True
return
if line.startswith("fatal: pathspec"):
mtch = self.MATCH_PAT.match(line)
if mtch is None:
raise GitException("Cannot parse pathspec error \"%s\"" %
(line, ))
raise GitBadPathspecException(mtch.group(1))
raise GitException("Remove failed: %s" % line.strip())
def git_remove(filelist, cached=False, recursive=False, sandbox_dir=None,
debug=False, dry_run=False, verbose=False):
"Remove the specified files/directories from the GIT commit index"
cmd_args = ["git", "rm"]
if cached:
cmd_args.append("--cached")
if recursive:
cmd_args.append("-r")
if isinstance(filelist, (tuple, list)):
cmd_args += filelist
else:
cmd_args.append(unicode(filelist))
handler = RemoveHandler()
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
returncode_handler=handler.handle_rtncode,
stderr_handler=handler.handle_stderr, debug=debug,
dry_run=dry_run, verbose=verbose)
def handle_reset_stderr(cmdname, line, verbose=False):
print("%s!! %s" % (cmdname, line, ), file=sys.stderr)
def git_reset(start_point, hard=False, sandbox_dir=None, debug=False,
dry_run=False, verbose=False):
"Reset the current HEAD to the specified state"
cmd_args = ["git", "reset"]
if hard is not None:
cmd_args.append("--hard")
cmd_args.append(start_point)
run_command(cmd_args, cmdname=" ".join(cmd_args[:2]).upper(),
stderr_handler=handle_reset_stderr,
working_directory=sandbox_dir, debug=debug, dry_run=dry_run,
verbose=verbose)
class ShowHashHandler(ErrorCatcher):
NO_PATCH_SUPPORTED = True
def __init__(self):
self.__no_patch_error = False
super(ShowHashHandler, self).__init__()
def clear_no_patch_error(self):
self.__no_patch_error = False
@classmethod
def disable_no_patch(cls):
cls.NO_PATCH_SUPPORTED = False
def handle_rtncode(self, cmdname, rtncode, lines, verbose=False):
if not self.__no_patch_error:
default_returncode_handler(cmdname, rtncode, lines,
verbose=verbose)
def handle_stderr(self, cmdname, line, verbose=False):
if verbose:
print("%s!! %s" % (cmdname, line, ), file=sys.stderr)
if line.startswith("fatal: unrecognized") and \
line.find("--no-patch") > 0:
self.__no_patch_error = True
self.disable_no_patch()
return
super(ShowHashHandler, self).handle_stderr(cmdname, line)
@property
def saw_no_patch_error(self):
return self.__no_patch_error
# TODO: Make this a more comprehensive implementation of 'git show'
def git_show_hash(sandbox_dir=None, debug=False, dry_run=False, verbose=False):
"Return the full hash of the current Git sandbox"
handler = ShowHashHandler()
for no_patch in True, False:
cmd_args = ["git", "show", "--format=%H"]
if no_patch and ShowHashHandler.NO_PATCH_SUPPORTED:
cmd_args.append("--no-patch")
handler.clear_no_patch_error()
full_hash = None
for line in run_generator(cmd_args,
cmdname=" ".join(cmd_args[:2]).upper(),
working_directory=sandbox_dir,
returncode_handler=handler.handle_rtncode,
stderr_handler=handler.handle_stderr,
stderr_finalizer=handler.finalize_stderr,
debug=debug, dry_run=dry_run,
verbose=verbose):
line = line.rstrip()
if line == "":
continue
if line.startswith("fatal: ") and line.find("--no-patch") > 0:
break
if full_hash is None: