Correct users of for-each-ref
[yap.git] / yap / yap.py
blob62915d16ad880aa257325bf3507fc035b1564de4
1 import sys
2 import os
3 import glob
4 import getopt
5 import pickle
6 import tempfile
8 from util import *
10 class ShellError(Exception):
11 def __init__(self, cmd, rc):
12 self.cmd = cmd
13 self.rc = rc
15 def __str__(self):
16 return "%s returned %d" % (self.cmd, self.rc)
18 class YapError(Exception):
19 def __init__(self, msg):
20 self.msg = msg
22 def __str__(self):
23 return self.msg
25 class YapCore(object):
26 def _add_new_file(self, file):
27 repo = get_output('git rev-parse --git-dir')[0]
28 dir = os.path.join(repo, 'yap')
29 try:
30 os.mkdir(dir)
31 except OSError:
32 pass
33 files = self._get_new_files()
34 files.append(file)
35 path = os.path.join(dir, 'new-files')
36 pickle.dump(files, open(path, 'w'))
38 def _get_new_files(self):
39 repo = get_output('git rev-parse --git-dir')[0]
40 path = os.path.join(repo, 'yap', 'new-files')
41 try:
42 files = pickle.load(file(path))
43 except IOError:
44 files = []
46 x = []
47 for f in files:
48 # if f in the index
49 if get_output("git ls-files --cached '%s'" % f) != []:
50 continue
51 x.append(f)
52 return x
54 def _remove_new_file(self, file):
55 files = self._get_new_files()
56 files = filter(lambda x: x != file, files)
58 repo = get_output('git rev-parse --git-dir')[0]
59 path = os.path.join(repo, 'yap', 'new-files')
60 try:
61 pickle.dump(files, open(path, 'w'))
62 except IOError:
63 pass
65 def _assert_file_exists(self, file):
66 if not os.access(file, os.R_OK):
67 raise YapError("No such file: %s" % file)
69 def _repo_path_to_rel(self, path):
70 prefix = get_output("git rev-parse --show-prefix")
71 if not prefix:
72 return path
74 prefix = [ prefix[0] ]
75 while True:
76 head, tail = os.path.split(prefix[0])
77 if not head:
78 break
79 prefix[0] = head
80 if tail:
81 prefix.insert(1, tail)
83 path = [ path ]
84 while True:
85 head, tail = os.path.split(path[0])
86 if not head:
87 break
88 path[0] = head
89 if tail:
90 path.insert(1, tail)
92 common = 0
93 for a, b in zip(prefix, path):
94 if a != b:
95 break
96 common += 1
98 path = path[common:]
99 cdup = [".."] * (len(prefix) - common)
100 path = cdup + list(path)
101 path = os.path.join(*path)
102 return path
104 def _get_staged_files(self):
105 if run_command("git rev-parse HEAD"):
106 files = get_output("git ls-files --cached")
107 else:
108 files = get_output("git diff-index --cached --name-only HEAD")
109 unmerged = self._get_unmerged_files()
110 if unmerged:
111 unmerged = set(unmerged)
112 files = set(files).difference(unmerged)
113 files = list(files)
114 return files
116 def _get_unstaged_files(self):
117 cwd = os.getcwd()
118 cdup = get_output("git rev-parse --show-cdup")
119 assert cdup
120 if cdup[0]:
121 os.chdir(cdup[0])
122 files = get_output("git ls-files -m")
123 os.chdir(cwd)
125 new_files = self._get_new_files()
126 if new_files:
127 staged = self._get_staged_files()
128 if staged:
129 staged = set(staged)
130 new_files = set(new_files).difference(staged)
131 new_files = list(new_files)
132 files += new_files
133 unmerged = self._get_unmerged_files()
134 if unmerged:
135 unmerged = set(unmerged)
136 files = set(files).difference(unmerged)
137 files = list(files)
138 return files
140 def _get_unmerged_files(self):
141 cwd = os.getcwd()
142 cdup = get_output("git rev-parse --show-cdup")
143 assert cdup
144 if cdup[0]:
145 os.chdir(cdup[0])
146 files = get_output("git ls-files -u")
147 os.chdir(cwd)
148 files = [ x.replace('\t', ' ').split(' ')[3] for x in files ]
149 return list(set(files))
151 def _resolve_rev(self, rev):
152 ref = get_output("git rev-parse --verify %s 2>/dev/null" % rev)
153 if not ref:
154 raise YapError("No such revision: %s" % rev)
155 return ref[0]
157 def _delete_branch(self, branch, force):
158 current = get_output("git symbolic-ref HEAD")
159 if current:
160 current = current[0].replace('refs/heads/', '')
161 if branch == current:
162 raise YapError("Can't delete current branch")
164 ref = self._resolve_rev('refs/heads/'+branch)
165 run_safely("git update-ref -d 'refs/heads/%s' '%s'" % (branch, ref))
167 if not force:
168 name = get_output("git name-rev --name-only '%s'" % ref)[0]
169 if name == 'undefined':
170 run_command("git update-ref 'refs/heads/%s' '%s'" % (branch, ref))
171 raise YapError("Refusing to delete leaf branch (use -f to force)")
172 def _get_pager_cmd(self):
173 if 'YAP_PAGER' in os.environ:
174 return os.environ['YAP_PAGER']
175 elif 'GIT_PAGER' in os.environ:
176 return os.environ['GIT_PAGER']
177 elif 'PAGER' in os.environ:
178 return os.environ['PAGER']
179 else:
180 return "less"
182 def _add_one(self, file):
183 self._assert_file_exists(file)
184 x = get_output("git ls-files '%s'" % file)
185 if x != []:
186 raise YapError("File '%s' already in repository" % file)
187 self._add_new_file(file)
189 def _rm_one(self, file):
190 self._assert_file_exists(file)
191 if get_output("git ls-files '%s'" % file) != []:
192 run_safely("git rm --cached '%s'" % file)
193 self._remove_new_file(file)
195 def _stage_one(self, file, allow_unmerged=False):
196 self._assert_file_exists(file)
197 prefix = get_output("git rev-parse --show-prefix")
198 if prefix:
199 tmp = os.path.normpath(os.path.join(prefix[0], file))
200 else:
201 tmp = file
202 if not allow_unmerged and tmp in self._get_unmerged_files():
203 raise YapError("Refusing to stage conflicted file: %s" % file)
204 run_safely("git update-index --add '%s'" % file)
206 def _unstage_one(self, file):
207 self._assert_file_exists(file)
208 if run_command("git rev-parse HEAD"):
209 rc = run_command("git update-index --force-remove '%s'" % file)
210 else:
211 cdup = get_output("git rev-parse --show-cdup")
212 assert cdup
213 if cdup[0]:
214 cdup = cdup[0]
215 else:
216 cdup = '.'
218 rc = run_command("git diff-index --cached -p HEAD '%s' | (cd %s; git apply -R --cached)" % (file, cdup))
219 if rc:
220 raise YapError("Failed to unstage")
222 def _revert_one(self, file):
223 self._assert_file_exists(file)
224 try:
225 self._unstage_one(file)
226 except YapError:
227 pass
228 run_safely("git checkout-index -u -f '%s'" % file)
230 def _parse_commit(self, commit):
231 lines = get_output("git cat-file commit '%s'" % commit)
232 commit = {}
234 mode = None
235 for l in lines:
236 if mode != 'commit' and l.strip() == "":
237 mode = 'commit'
238 commit['log'] = []
239 continue
240 if mode == 'commit':
241 commit['log'].append(l)
242 continue
244 x = l.split(' ')
245 k = x[0]
246 v = ' '.join(x[1:])
247 commit[k] = v
248 commit['log'] = '\n'.join(commit['log'])
249 return commit
251 def _check_commit(self, **flags):
252 if '-a' in flags and '-d' in flags:
253 raise YapError("Conflicting flags: -a and -d")
255 if '-d' not in flags and self._get_unstaged_files():
256 if '-a' not in flags and self._get_staged_files():
257 raise YapError("Staged and unstaged changes present. Specify what to commit")
258 os.system("git diff-files -p | git apply --cached")
259 for f in self._get_new_files():
260 self._stage_one(f)
262 def _do_uncommit(self):
263 commit = self._parse_commit("HEAD")
264 repo = get_output('git rev-parse --git-dir')[0]
265 dir = os.path.join(repo, 'yap')
266 try:
267 os.mkdir(dir)
268 except OSError:
269 pass
270 msg_file = os.path.join(dir, 'msg')
271 fd = file(msg_file, 'w')
272 print >>fd, commit['log']
273 fd.close()
275 tree = get_output("git rev-parse --verify HEAD^")
276 run_safely("git update-ref -m uncommit HEAD '%s'" % tree[0])
278 def _do_commit(self, msg=None):
279 tree = get_output("git write-tree")[0]
281 repo = get_output('git rev-parse --git-dir')[0]
282 head_file = os.path.join(repo, 'yap', 'merge')
283 try:
284 parent = pickle.load(file(head_file))
285 except IOError:
286 parent = get_output("git rev-parse --verify HEAD 2> /dev/null")
288 if os.environ.has_key('YAP_EDITOR'):
289 editor = os.environ['YAP_EDITOR']
290 elif os.environ.has_key('GIT_EDITOR'):
291 editor = os.environ['GIT_EDITOR']
292 elif os.environ.has_key('EDITOR'):
293 editor = os.environ['EDITOR']
294 else:
295 editor = "vi"
297 fd, tmpfile = tempfile.mkstemp("yap")
298 os.close(fd)
300 if msg is None:
301 msg_file = os.path.join(repo, 'yap', 'msg')
302 if os.access(msg_file, os.R_OK):
303 fd1 = file(msg_file)
304 fd2 = file(tmpfile, 'w')
305 for l in fd1.xreadlines():
306 print >>fd2, l.strip()
307 fd2.close()
308 os.unlink(msg_file)
309 if os.system("%s '%s'" % (editor, tmpfile)) != 0:
310 raise YapError("Editing commit message failed")
311 fd = file(tmpfile)
312 msg = fd.readlines()
313 msg = ''.join(msg)
315 msg = msg.strip()
316 if not msg:
317 raise YapError("Refusing to use empty commit message")
319 fd = os.popen("git stripspace > %s" % tmpfile, 'w')
320 print >>fd, msg,
321 fd.close()
323 if parent:
324 parent = ' -p '.join(parent)
325 commit = get_output("git commit-tree '%s' -p %s < '%s'" % (tree, parent, tmpfile))
326 else:
327 commit = get_output("git commit-tree '%s' < '%s'" % (tree, tmpfile))
329 os.unlink(tmpfile)
330 run_safely("git update-ref HEAD '%s'" % commit[0])
331 self._clear_state()
333 def _check_rebasing(self):
334 repo = get_output('git rev-parse --git-dir')[0]
335 dotest = os.path.join(repo, '.dotest')
336 if os.access(dotest, os.R_OK):
337 raise YapError("A git operation is in progress. Complete it first")
338 dotest = os.path.join(repo, '..', '.dotest')
339 if os.access(dotest, os.R_OK):
340 raise YapError("A git operation is in progress. Complete it first")
342 def _check_git(self):
343 if run_command("git rev-parse --git-dir"):
344 raise YapError("That command must be run from inside a git repository")
346 def _list_remotes(self):
347 remotes = get_output("git config --get-regexp '^remote.*.url'")
348 for x in remotes:
349 remote, url = x.split(' ')
350 remote = remote.replace('remote.', '')
351 remote = remote.replace('.url', '')
352 yield remote, url
354 def _unstage_all(self):
355 try:
356 run_safely("git read-tree -m HEAD")
357 except ShellError:
358 run_safely("git read-tree HEAD")
359 run_safely("git update-index -q --refresh")
361 def _get_tracking(self, current):
362 remote = get_output("git config branch.%s.remote" % current)
363 if not remote:
364 raise YapError("No tracking branch configured for '%s'" % current)
366 merge = get_output("git config branch.%s.merge" % current)
367 if not merge:
368 raise YapError("No tracking branch configured for '%s'" % current)
369 return remote[0], merge[0]
371 def _confirm_push(self, current, rhs, repo):
372 print "About to push local branch '%s' to '%s' on '%s'" % (current, rhs, repo)
373 print "Continue (y/n)? ",
374 sys.stdout.flush()
375 ans = sys.stdin.readline().strip()
377 if ans.lower() != 'y' and ans.lower() != 'yes':
378 raise YapError("Aborted.")
380 def _clear_state(self):
381 repo = get_output('git rev-parse --git-dir')[0]
382 dir = os.path.join(repo, 'yap')
383 for f in "new-files", "merge", "msg":
384 try:
385 os.unlink(os.path.join(dir, f))
386 except OSError:
387 pass
389 def _get_attr(self, name, attr):
390 val = None
391 for c in self.__class__.__bases__:
392 try:
393 m2 = c.__dict__[name]
394 except KeyError:
395 continue
396 try:
397 val = m2.__getattribute__(attr)
398 except AttributeError:
399 continue
400 return val
402 def _filter_log(self, commit):
403 return commit
405 def _check_rename(self, rev, path):
406 renames = get_output("git diff-tree -C -M --diff-filter=R %s %s^"
407 % (rev, rev))
408 for r in renames:
409 r = r.replace('\t', ' ')
410 fields = r.split(' ')
411 mode1, mode2, hash1, hash2, rename, dst, src = fields
412 if dst == path:
413 return src
414 return None
416 @short_help("make a local copy of an existing repository")
417 @long_help("""
418 The first argument is a URL to the existing repository. This can be an
419 absolute path if the repository is local, or a URL with the git://,
420 ssh://, or http:// schemes. By default, the directory used is the last
421 component of the URL, sans '.git'. This can be overridden by providing
422 a second argument.
423 """)
424 def cmd_clone(self, url, directory=None):
425 "<url> [directory]"
427 if '://' not in url and url[0] != '/':
428 url = os.path.join(os.getcwd(), url)
430 url = url.rstrip('/')
431 if directory is None:
432 directory = url.rsplit('/')[-1]
433 directory = directory.replace('.git', '')
435 try:
436 os.mkdir(directory)
437 except OSError:
438 raise YapError("Directory exists: %s" % directory)
439 os.chdir(directory)
440 self.cmd_init()
441 self.cmd_repo("origin", url)
442 self.cmd_fetch("origin")
444 branch = None
445 if not run_command("git rev-parse --verify refs/remotes/origin/HEAD"):
446 hash = get_output("git rev-parse refs/remotes/origin/HEAD")[0]
447 for b in get_output("git for-each-ref --format='%(refname)' 'refs/remotes/origin'"):
448 if get_output("git rev-parse %s" % b)[0] == hash:
449 branch = b
450 break
451 if branch is None:
452 if not run_command("git rev-parse --verify refs/remotes/origin/master"):
453 branch = "refs/remotes/origin/master"
454 if branch is None:
455 branch = get_output("git for-each-ref --format='%(refname)' 'refs/remotes/origin'")
456 branch = branch[0]
458 hash = get_output("git rev-parse %s" % branch)
459 assert hash
460 branch = branch.replace('refs/remotes/origin/', '')
461 run_safely("git update-ref refs/heads/%s %s" % (branch, hash[0]))
462 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
463 self.cmd_revert(**{'-a': 1})
465 @short_help("turn a directory into a repository")
466 @long_help("""
467 Converts the current working directory into a repository. The primary
468 side-effect of this command is the creation of a '.git' subdirectory.
469 No files are added nor commits made.
470 """)
471 def cmd_init(self):
472 os.system("git init")
474 @short_help("add a new file to the repository")
475 @long_help("""
476 The arguments are the files to be added to the repository. Once added,
477 the files will show as "unstaged changes" in the output of 'status'. To
478 reverse the effects of this command, see 'rm'.
479 """)
480 def cmd_add(self, *files):
481 "<file>..."
482 self._check_git()
484 if not files:
485 raise TypeError
487 for f in files:
488 self._add_one(f)
489 self.cmd_status()
491 @short_help("delete a file from the repository")
492 @long_help("""
493 The arguments are the files to be removed from the current revision of
494 the repository. The files will still exist in any past commits that the
495 files may have been a part of. The file is not actually deleted, it is
496 just no longer tracked as part of the repository.
497 """)
498 def cmd_rm(self, *files):
499 "<file>..."
500 self._check_git()
501 if not files:
502 raise TypeError
504 for f in files:
505 self._rm_one(f)
506 self.cmd_status()
508 @short_help("stage changes in a file for commit")
509 @long_help("""
510 The arguments are the files to be staged. Staging changes is a way to
511 build up a commit when you do not want to commit all changes at once.
512 To commit only staged changes, use the '-d' flag to 'commit.' To
513 reverse the effects of this command, see 'unstage'. Once staged, the
514 files will show as "staged changes" in the output of 'status'.
515 """)
516 def cmd_stage(self, *files):
517 "<file>..."
518 self._check_git()
519 if not files:
520 raise TypeError
522 for f in files:
523 self._stage_one(f)
524 self.cmd_status()
526 @short_help("unstage changes in a file")
527 @long_help("""
528 The arguments are the files to be unstaged. Once unstaged, the files
529 will show as "unstaged changes" in the output of 'status'. The '-a'
530 flag can be used to unstage all staged changes at once.
531 """)
532 @takes_options("a")
533 def cmd_unstage(self, *files, **flags):
534 "[-a] | <file>..."
535 self._check_git()
536 if '-a' in flags:
537 files = self._get_staged_files()
539 if not files:
540 raise TypeError
542 for f in files:
543 self._unstage_one(f)
544 self.cmd_status()
546 @short_help("show files with staged and unstaged changes")
547 @long_help("""
548 Show the files in the repository with changes since the last commit,
549 categorized based on whether the changes are staged or not. A file may
550 appear under each heading if the same file has both staged and unstaged
551 changes.
552 """)
553 def cmd_status(self):
555 self._check_git()
556 branch = get_output("git symbolic-ref HEAD")
557 if branch:
558 branch = branch[0].replace('refs/heads/', '')
559 else:
560 branch = "DETACHED"
561 print "Current branch: %s" % branch
563 print "Files with staged changes:"
564 files = self._get_staged_files()
565 for f in files:
566 print "\t%s" % self._repo_path_to_rel(f)
567 if not files:
568 print "\t(none)"
570 print "Files with unstaged changes:"
571 files = self._get_unstaged_files()
572 for f in files:
573 print "\t%s" % self._repo_path_to_rel(f)
574 if not files:
575 print "\t(none)"
577 files = self._get_unmerged_files()
578 if files:
579 print "Files with conflicts:"
580 for f in files:
581 print "\t%s" % self._repo_path_to_rel(f)
583 @short_help("remove uncommitted changes from a file (*)")
584 @long_help("""
585 The arguments are the files whose changes will be reverted. If the '-a'
586 flag is given, then all files will have uncommitted changes removed.
587 Note that there is no way to reverse this command short of manually
588 editing each file again.
589 """)
590 @takes_options("a")
591 def cmd_revert(self, *files, **flags):
592 "(-a | <file>)"
593 self._check_git()
594 if '-a' in flags:
595 self._unstage_all()
596 run_safely("git checkout-index -u -f -a")
597 self._clear_state()
598 self.cmd_status()
599 return
601 if not files:
602 raise TypeError
604 for f in files:
605 self._revert_one(f)
606 self.cmd_status()
608 @short_help("record changes to files as a new commit")
609 @long_help("""
610 Create a new commit recording changes since the last commit. If there
611 are only unstaged changes, those will be recorded. If there are only
612 staged changes, those will be recorded. Otherwise, you will have to
613 specify either the '-a' flag or the '-d' flag to commit all changes or
614 only staged changes, respectively. To reverse the effects of this
615 command, see 'uncommit'.
616 """)
617 @takes_options("adm:")
618 def cmd_commit(self, **flags):
619 "[-a | -d] [-m <msg>]"
620 self._check_git()
621 self._check_rebasing()
622 self._check_commit(**flags)
623 if not self._get_staged_files():
624 raise YapError("No changes to commit")
625 msg = flags.get('-m', None)
626 self._do_commit(msg)
627 self.cmd_status()
629 @short_help("reverse the actions of the last commit")
630 @long_help("""
631 Reverse the effects of the last 'commit' operation. The changes that
632 were part of the previous commit will show as "staged changes" in the
633 output of 'status'. This means that if no files were changed since the
634 last commit was created, 'uncommit' followed by 'commit' is a lossless
635 operation.
636 """)
637 def cmd_uncommit(self):
639 self._check_git()
640 self._do_uncommit()
641 self.cmd_status()
643 @short_help("report the current version of yap")
644 def cmd_version(self):
645 print "Yap version %s" % self.version
647 @short_help("show the changelog for particular versions or files")
648 @long_help("""
649 The arguments are the files with which to filter history. If none are
650 given, all changes are listed. Otherwise only commits that affected one
651 or more of the given files are listed. The -r option changes the
652 starting revision for traversing history. By default, history is listed
653 starting at HEAD.
654 """)
655 @takes_options("pr:")
656 def cmd_log(self, *paths, **flags):
657 "[-p] [-r <rev>] <path>..."
658 self._check_git()
659 rev = flags.get('-r', 'HEAD')
660 rev = self._resolve_rev(rev)
661 paths = list(paths)
663 if '-p' in flags:
664 flags['-p'] = '-p'
666 try:
667 pager = os.popen(self._get_pager_cmd(), 'w')
668 rename = False
669 while True:
670 for hash in yield_output("git rev-list '%s' -- %s"
671 % (rev, ' '.join(paths))):
672 commit = get_output("git show -M -C %s %s"
673 % (flags.get('-p', '--name-status'), hash),
674 strip=False)
675 commit = self._filter_log(commit)
676 print >>pager, ''.join(commit)
678 # Check for renames
679 if len(paths) == 1:
680 src = self._check_rename(hash, paths[0])
681 if src is not None:
682 paths[0] = src
683 rename = True
684 rev = hash+"^"
685 break
686 if not rename:
687 break
688 rename = False
689 except (IOError, KeyboardInterrupt):
690 pass
692 @short_help("show staged, unstaged, or all uncommitted changes")
693 @long_help("""
694 Show staged, unstaged, or all uncommitted changes. By default, all
695 changes are shown. The '-u' flag causes only unstaged changes to be
696 shown. The '-d' flag causes only staged changes to be shown.
697 """)
698 @takes_options("ud")
699 def cmd_diff(self, **flags):
700 "[ -u | -d ]"
701 self._check_git()
702 if '-u' in flags and '-d' in flags:
703 raise YapError("Conflicting flags: -u and -d")
705 pager = self._get_pager_cmd()
707 if '-u' in flags:
708 os.system("git diff-files -p | %s" % pager)
709 elif '-d' in flags:
710 os.system("git diff-index --cached -p HEAD | %s" % pager)
711 else:
712 os.system("git diff-index -p HEAD | %s" % pager)
714 @short_help("list, create, or delete branches")
715 @long_help("""
716 If no arguments are specified, a list of local branches is given. The
717 current branch is indicated by a "*" next to the name. If an argument
718 is given, it is taken as the name of a new branch to create. The branch
719 will start pointing at the current HEAD. See 'point' for details on
720 changing the revision of the new branch. Note that this command does
721 not switch the current working branch. See 'switch' for details on
722 changing the current working branch.
724 The '-d' flag can be used to delete local branches. If the delete
725 operation would remove the last branch reference to a given line of
726 history (colloquially referred to as "dangling commits"), yap will
727 report an error and abort. The '-f' flag can be used to force the delete
728 in spite of this.
729 """)
730 @takes_options("fd:")
731 def cmd_branch(self, branch=None, **flags):
732 "[ [-f] -d <branch> | <branch> ]"
733 self._check_git()
734 force = '-f' in flags
735 if '-d' in flags:
736 self._delete_branch(flags['-d'], force)
737 self.cmd_branch()
738 return
740 if branch is not None:
741 ref = get_output("git rev-parse --verify HEAD")
742 if not ref:
743 raise YapError("No branch point yet. Make a commit")
744 run_safely("git update-ref 'refs/heads/%s' '%s'" % (branch, ref[0]))
746 current = get_output("git symbolic-ref HEAD")
747 branches = get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
748 for b in branches:
749 if current and b == current[0]:
750 print "* ",
751 else:
752 print " ",
753 b = b.replace('refs/heads/', '')
754 print b
756 @short_help("change the current working branch")
757 @long_help("""
758 The argument is the name of the branch to make the current working
759 branch. This command will fail if there are uncommitted changes to any
760 files. Otherwise, the contents of the files in the working directory
761 are updated to reflect their state in the new branch. Additionally, any
762 future commits are added to the new branch instead of the previous line
763 of history.
764 """)
765 @takes_options("f")
766 def cmd_switch(self, branch, **flags):
767 "[-f] <branch>"
768 self._check_git()
769 self._check_rebasing()
770 ref = self._resolve_rev('refs/heads/'+branch)
772 if '-f' not in flags:
773 if (self._get_staged_files()
774 or (self._get_unstaged_files()
775 and run_command("git update-index --refresh"))):
776 raise YapError("You have uncommitted changes. Use -f to continue anyway")
778 if self._get_unstaged_files() and self._get_staged_files():
779 raise YapError("You have staged and unstaged changes. Perhaps unstage -a?")
781 staged = bool(self._get_staged_files())
783 run_command("git diff-files -p | git apply --cached")
784 for f in self._get_new_files():
785 self._stage_one(f)
787 idx = get_output("git write-tree")
788 new = self._resolve_rev('refs/heads/'+branch)
790 run_command("git update-index --refresh")
791 readtree = "git read-tree -v --aggressive -u -m HEAD %s %s" % (idx[0], new)
792 if os.system(readtree):
793 raise YapError("Failed to switch")
794 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
796 if '-f' not in flags:
797 self._clear_state()
799 if not staged:
800 self._unstage_all()
801 self.cmd_status()
803 @short_help("move the current branch to a different revision")
804 @long_help("""
805 The argument is the hash of the commit to which the current branch
806 should point, or alternately a branch or tag (a.k.a, "committish"). If
807 moving the branch would create "dangling commits" (see 'branch'), yap
808 will report an error and abort. The '-f' flag can be used to force the
809 operation in spite of this.
810 """)
811 @takes_options("f")
812 def cmd_point(self, where, **flags):
813 "[-f] <where>"
814 self._check_git()
815 self._check_rebasing()
817 head = get_output("git rev-parse --verify HEAD")
818 if not head:
819 raise YapError("No commit yet; nowhere to point")
821 ref = self._resolve_rev(where)
822 ref = get_output("git rev-parse --verify '%s^{commit}'" % ref)
823 if not ref:
824 raise YapError("Not a commit: %s" % where)
826 if self._get_unstaged_files() or self._get_staged_files():
827 raise YapError("You have uncommitted changes. Commit them first")
829 run_safely("git update-ref HEAD '%s'" % ref[0])
831 if '-f' not in flags:
832 name = get_output("git name-rev --name-only '%s'" % head[0])[0]
833 if name == "undefined":
834 os.system("git update-ref HEAD '%s'" % head[0])
835 raise YapError("Pointing there will lose commits. Use -f to force")
837 run_command("git update-index --refresh")
838 rc = os.system("git read-tree -v --reset -u HEAD")
839 if rc:
840 raise YapError("checkout-index failed")
841 self._clear_state()
843 @short_help("alter history by dropping or amending commits")
844 @long_help("""
845 This command operates in two distinct modes, "amend" and "drop" mode.
846 In drop mode, the given commit is removed from the history of the
847 current branch, as though that commit never happened. By default the
848 commit used is HEAD.
850 In amend mode, the uncommitted changes present are merged into a
851 previous commit. This is useful for correcting typos or adding missed
852 files into past commits. By default the commit used is HEAD.
854 While rewriting history it is possible that conflicts will arise. If
855 this happens, the rewrite will pause and you will be prompted to resolve
856 the conflicts and stage them. Once that is done, you will run "yap
857 history continue." If instead you want the conflicting commit removed
858 from history (perhaps your changes supercede that commit) you can run
859 "yap history skip". Once the rewrite completes, your branch will be on
860 the same commit as when the rewrite started.
861 """)
862 def cmd_history(self, subcmd, *args):
863 "amend | drop <commit>"
864 self._check_git()
866 if subcmd not in ("amend", "drop", "continue", "skip"):
867 raise TypeError
869 resolvemsg = """
870 When you have resolved the conflicts run \"yap history continue\".
871 To skip the problematic patch, run \"yap history skip\"."""
873 if subcmd == "continue":
874 os.system("git am -3 -r --resolvemsg='%s'" % resolvemsg)
875 return
876 if subcmd == "skip":
877 os.system("git reset --hard")
878 os.system("git am -3 --skip --resolvemsg='%s'" % resolvemsg)
879 return
881 if subcmd == "amend":
882 flags, args = getopt.getopt(args, "ad")
883 flags = dict(flags)
885 if len(args) > 1:
886 raise TypeError
887 if args:
888 commit = args[0]
889 else:
890 commit = "HEAD"
892 self._resolve_rev(commit)
893 self._check_rebasing()
895 if subcmd == "amend":
896 self._check_commit(**flags)
897 if self._get_unstaged_files():
898 # XXX: handle unstaged changes better
899 raise YapError("Commit away changes that you aren't amending")
901 self._unstage_all()
903 start = get_output("git rev-parse HEAD")
904 stash = get_output("git stash create")
905 run_command("git reset --hard")
906 try:
907 fd, tmpfile = tempfile.mkstemp("yap")
908 try:
909 try:
910 os.close(fd)
911 os.system("git format-patch -k --stdout '%s' > %s" % (commit, tmpfile))
912 if subcmd == "amend":
913 self.cmd_point(commit, **{'-f': True})
914 finally:
915 if subcmd == "amend":
916 if stash:
917 rc = os.system("git stash apply %s" % stash[0])
918 if rc:
919 self.cmd_point(start[0], **{'-f': True})
920 os.system("git stash apply %s" % stash[0])
921 raise YapError("Failed to apply stash")
922 stash = None
924 if subcmd == "amend":
925 self._do_uncommit()
926 self._check_commit(**{'-a': True})
927 self._do_commit()
928 else:
929 self.cmd_point("%s^" % commit, **{'-f': True})
931 stat = os.stat(tmpfile)
932 size = stat[6]
933 if size > 0:
934 run_safely("git update-index --refresh")
935 rc = os.system("git am -3 --resolvemsg=\'%s\' %s" % (resolvemsg, tmpfile))
936 if (rc):
937 raise YapError("Failed to apply changes")
938 finally:
939 os.unlink(tmpfile)
940 finally:
941 if stash:
942 run_command("git stash apply %s" % stash[0])
943 self.cmd_status()
945 @short_help("show the changes introduced by a given commit")
946 @long_help("""
947 By default, the changes in the last commit are shown. To override this,
948 specify a hash, branch, or tag (committish). The hash of the commit,
949 the commit's author, log message, and a diff of the changes are shown.
950 """)
951 def cmd_show(self, commit="HEAD"):
952 "[commit]"
953 self._check_git()
954 commit = self._resolve_rev(commit)
955 os.system("git show '%s'" % commit)
957 @short_help("apply the changes in a given commit to the current branch")
958 @long_help("""
959 The argument is the hash, branch, or tag (committish) of the commit to
960 be applied. In general, it only makes sense to apply commits that
961 happened on another branch. The '-r' flag can be used to have the
962 changes in the given commit reversed from the current branch. In
963 general, this only makes sense for commits that happened on the current
964 branch.
965 """)
966 @takes_options("r")
967 def cmd_cherry_pick(self, commit, **flags):
968 "[-r] <commit>"
969 self._check_git()
970 commit = self._resolve_rev(commit)
971 if '-r' in flags:
972 os.system("git revert '%s'" % commit)
973 else:
974 os.system("git cherry-pick '%s'" % commit)
976 @short_help("list, add, or delete configured remote repositories")
977 @long_help("""
978 When invoked with no arguments, this command will show the list of
979 currently configured remote repositories, giving both the name and URL
980 of each. To add a new repository, give the desired name as the first
981 argument and the URL as the second. The '-d' flag can be used to remove
982 a previously added repository.
983 """)
984 @takes_options("d:")
985 def cmd_repo(self, name=None, url=None, **flags):
986 "[<name> <url> | -d <name>]"
987 self._check_git()
988 if name is not None and url is None:
989 raise TypeError
991 if '-d' in flags:
992 if flags['-d'] not in [ x[0] for x in self._list_remotes() ]:
993 raise YapError("No such repository: %s" % flags['-d'])
994 os.system("git config --unset remote.%s.url" % flags['-d'])
995 os.system("git config --unset remote.%s.fetch" % flags['-d'])
996 for b in get_output("git for-each-ref --format='%(refname)' 'refs/remotes/origin'"):
997 hash = get_output("git rev-parse %s" % b)
998 assert hash
999 run_safely("git update-ref -d %s %s" % (b, hash[0]))
1001 if name:
1002 if name in [ x[0] for x in self._list_remotes() ]:
1003 raise YapError("Repository '%s' already exists" % name)
1004 os.system("git config remote.%s.url %s" % (name, url))
1005 os.system("git config remote.%s.fetch +refs/heads/*:refs/remotes/%s/*" % (name, name))
1007 for remote, url in self._list_remotes():
1008 print "%s" % remote
1009 print " URL: %s" % url
1010 first = True
1011 for b in get_output("git for-each-ref --format='%%(refname)' 'refs/remotes/%s'" % remote):
1012 b = b.replace('refs/remotes/', '')
1013 if first:
1014 branches = "Branches: "
1015 else:
1016 branches = " "
1017 print " %s%s" % (branches, b)
1018 first = False
1020 @short_help("send local commits to a remote repository (*)")
1021 @long_help("""
1022 When invoked with no arguments, the current branch is synchronized to
1023 the tracking branch of the tracking remote. If no tracking remote is
1024 specified, the repository will have to be specified on the command line.
1025 In that case, the default is to push to a branch with the same name as
1026 the current branch. This behavior can be overridden by giving a second
1027 argument to specify the remote branch.
1029 If the remote branch does not currently exist, the command will abort
1030 unless the -c flag is provided. If the remote branch is not a direct
1031 descendent of the local branch, the command will abort unless the -f
1032 flag is provided. Forcing a push in this way can be problematic to
1033 other users of the repository if they are not expecting it.
1035 To delete a branch on the remote repository, use the -d flag.
1036 """)
1037 @takes_options("cdf")
1038 def cmd_push(self, repo=None, rhs=None, **flags):
1039 "[-c | -d] <repo>"
1040 self._check_git()
1041 if '-c' in flags and '-d' in flags:
1042 raise TypeError
1044 if repo and repo not in [ x[0] for x in self._list_remotes() ]:
1045 raise YapError("No such repository: %s" % repo)
1047 current = get_output("git symbolic-ref HEAD")
1048 if not current:
1049 raise YapError("Not on a branch!")
1051 self._check_rebasing()
1053 current = current[0].replace('refs/heads/', '')
1054 remote = get_output("git config branch.%s.remote" % current)
1055 if repo is None and remote:
1056 repo = remote[0]
1058 if repo is None:
1059 raise YapError("No tracking branch configured; specify destination repository")
1061 if rhs is None and remote and remote[0] == repo:
1062 merge = get_output("git config branch.%s.merge" % current)
1063 if merge:
1064 rhs = merge[0]
1066 if rhs is None:
1067 rhs = "refs/heads/%s" % current
1069 if '-c' not in flags and '-d' not in flags:
1070 if run_command("git rev-parse --verify refs/remotes/%s/%s"
1071 % (repo, rhs.replace('refs/heads/', ''))):
1072 raise YapError("No matching branch on that repo. Use -c to create a new branch there.")
1073 if '-f' not in flags:
1074 hash = get_output("git rev-parse refs/remotes/%s/%s" % (repo, rhs.replace('refs/heads/', '')))
1075 base = get_output("git merge-base HEAD %s" % hash[0])
1076 assert base
1077 if base[0] != hash[0]:
1078 raise YapError("Branch not up-to-date with remote. Update or use -f")
1080 self._confirm_push(current, rhs, repo)
1081 if '-f' in flags:
1082 flags['-f'] = '-f'
1084 if '-d' in flags:
1085 lhs = ""
1086 else:
1087 lhs = "refs/heads/%s" % current
1088 rc = os.system("git push %s %s %s:%s" % (flags.get('-f', ''), repo, lhs, rhs))
1089 if rc:
1090 raise YapError("Push failed.")
1092 @short_help("retrieve commits from a remote repository")
1093 @long_help("""
1094 When run with no arguments, the command will retrieve new commits from
1095 the remote tracking repository. Note that this does not in any way
1096 alter the current branch. For that, see "update". If a remote other
1097 than the tracking remote is desired, it can be specified as the first
1098 argument.
1099 """)
1100 def cmd_fetch(self, repo=None):
1101 "<repo>"
1102 self._check_git()
1103 current = get_output("git symbolic-ref HEAD")
1104 if not current:
1105 raise YapError("Not on a branch!")
1107 if repo and repo not in [ x[0] for x in self._list_remotes() ]:
1108 raise YapError("No such repository: %s" % repo)
1109 if repo is None:
1110 current = current[0].replace('refs/heads/', '')
1111 remote = get_output("git config branch.%s.remote" % current)
1112 if remote:
1113 repo = remote[0]
1114 if repo is None:
1115 raise YapError("No tracking branch configured; specify a repository")
1116 os.system("git fetch %s" % repo)
1118 @short_help("update the current branch relative to its tracking branch")
1119 @long_help("""
1120 Updates the current branch relative to its remote tracking branch. This
1121 command requires that the current branch have a remote tracking branch
1122 configured. If any conflicts occur while applying your changes to the
1123 updated remote, the command will pause to allow you to fix them. Once
1124 that is done, run "update" with the "continue" subcommand. Alternately,
1125 the "skip" subcommand can be used to discard the conflicting changes.
1126 """)
1127 def cmd_update(self, subcmd=None):
1128 "[continue | skip]"
1129 self._check_git()
1130 if subcmd and subcmd not in ["continue", "skip"]:
1131 raise TypeError
1133 resolvemsg = """
1134 When you have resolved the conflicts run \"yap update continue\".
1135 To skip the problematic patch, run \"yap update skip\"."""
1137 if subcmd == "continue":
1138 os.system("git am -3 -r --resolvemsg='%s'" % resolvemsg)
1139 return
1140 if subcmd == "skip":
1141 os.system("git reset --hard")
1142 os.system("git am -3 --skip --resolvemsg='%s'" % resolvemsg)
1143 return
1145 self._check_rebasing()
1146 if self._get_unstaged_files() or self._get_staged_files():
1147 raise YapError("You have uncommitted changes. Commit them first")
1149 current = get_output("git symbolic-ref HEAD")
1150 if not current:
1151 raise YapError("Not on a branch!")
1153 current = current[0].replace('refs/heads/', '')
1154 remote, merge = self._get_tracking(current)
1155 merge = merge.replace('refs/heads/', '')
1157 self.cmd_fetch(remote)
1158 base = get_output("git merge-base HEAD refs/remotes/%s/%s" % (remote, merge))
1160 try:
1161 fd, tmpfile = tempfile.mkstemp("yap")
1162 os.close(fd)
1163 os.system("git format-patch -k --stdout '%s' > %s" % (base[0], tmpfile))
1164 self.cmd_point("refs/remotes/%s/%s" % (remote, merge), **{'-f': True})
1166 stat = os.stat(tmpfile)
1167 size = stat[6]
1168 if size > 0:
1169 rc = os.system("git am -3 --resolvemsg=\'%s\' %s" % (resolvemsg, tmpfile))
1170 if (rc):
1171 raise YapError("Failed to apply changes")
1172 finally:
1173 os.unlink(tmpfile)
1175 @short_help("query and configure remote branch tracking")
1176 @long_help("""
1177 When invoked with no arguments, the command displays the tracking
1178 information for the current branch. To configure the tracking
1179 information, two arguments for the remote repository and remote branch
1180 are given. The tracking information is used to provide defaults for
1181 where to push local changes and from where to get updates to the branch.
1182 """)
1183 def cmd_track(self, repo=None, branch=None):
1184 "[<repo> <branch>]"
1185 self._check_git()
1187 current = get_output("git symbolic-ref HEAD")
1188 if not current:
1189 raise YapError("Not on a branch!")
1190 current = current[0].replace('refs/heads/', '')
1192 if repo is None and branch is None:
1193 repo, merge = self._get_tracking(current)
1194 merge = merge.replace('refs/heads/', '')
1195 print "Branch '%s' tracking refs/remotes/%s/%s" % (current, repo, merge)
1196 return
1198 if repo is None or branch is None:
1199 raise TypeError
1201 if repo not in [ x[0] for x in self._list_remotes() ]:
1202 raise YapError("No such repository: %s" % repo)
1204 if run_command("git rev-parse --verify refs/remotes/%s/%s" % (repo, branch)):
1205 raise YapError("No such branch '%s' on repository '%s'" % (branch, repo))
1207 os.system("git config branch.%s.remote '%s'" % (current, repo))
1208 os.system("git config branch.%s.merge 'refs/heads/%s'" % (current, branch))
1209 print "Branch '%s' now tracking refs/remotes/%s/%s" % (current, repo, branch)
1211 @short_help("mark files with conflicts as resolved")
1212 @long_help("""
1213 The arguments are the files to be marked resolved. When a conflict
1214 occurs while merging changes to a file, that file is marked as
1215 "unmerged." Until the file(s) with conflicts are marked resolved,
1216 commits cannot be made.
1217 """)
1218 def cmd_resolved(self, *files):
1219 "<file>..."
1220 self._check_git()
1221 if not files:
1222 raise TypeError
1224 for f in files:
1225 self._stage_one(f, True)
1226 self.cmd_status()
1228 @short_help("merge a branch into the current branch")
1229 def cmd_merge(self, branch):
1230 "<branch>"
1231 self._check_git()
1233 branch_name = branch
1234 branch = self._resolve_rev(branch)
1235 base = get_output("git merge-base HEAD %s" % branch)
1236 if not base:
1237 raise YapError("Branch '%s' is not a fork of the current branch"
1238 % branch)
1240 readtree = ("git read-tree --aggressive -u -m %s HEAD %s"
1241 % (base[0], branch))
1242 if run_command(readtree):
1243 run_command("git update-index --refresh")
1244 if os.system(readtree):
1245 raise YapError("Failed to merge")
1247 repo = get_output('git rev-parse --git-dir')[0]
1248 dir = os.path.join(repo, 'yap')
1249 try:
1250 os.mkdir(dir)
1251 except OSError:
1252 pass
1253 msg_file = os.path.join(dir, 'msg')
1254 msg = file(msg_file, 'w')
1255 print >>msg, "Merge branch '%s'" % branch_name
1256 msg.close()
1258 head = get_output("git rev-parse --verify HEAD")
1259 assert head
1260 heads = [head[0], branch]
1261 head_file = os.path.join(dir, 'merge')
1262 pickle.dump(heads, file(head_file, 'w'))
1264 self._merge_index(branch, base[0])
1265 if self._get_unmerged_files():
1266 self.cmd_status()
1267 raise YapError("Fix conflicts then commit")
1269 self._do_commit()
1271 def _merge_index(self, branch, base):
1272 for f in self._get_unmerged_files():
1273 fd, bfile = tempfile.mkstemp("yap")
1274 os.close(fd)
1275 rc = os.system("git show %s:%s > %s" % (base, f, bfile))
1276 assert rc == 0
1278 fd, ofile = tempfile.mkstemp("yap")
1279 os.close(fd)
1280 rc = os.system("git show %s:%s > %s" % (branch, f, ofile))
1281 assert rc == 0
1283 command = "git merge-file -L %(file)s -L %(file)s.base -L %(file)s.%(branch)s %(file)s %(base)s %(other)s " % dict(file=f, branch=branch, base=bfile, other=ofile)
1284 rc = os.system(command)
1285 os.unlink(ofile)
1286 os.unlink(bfile)
1288 assert rc >= 0
1289 if rc == 0:
1290 self._stage_one(f, True)
1292 def cmd_help(self, cmd=None):
1293 if cmd is not None:
1294 oldcmd = cmd
1295 cmd = "cmd_" + cmd.replace('-', '_')
1296 try:
1297 attr = self.__getattribute__(cmd)
1298 except AttributeError:
1299 raise YapError("No such command: %s" % cmd)
1301 help = self._get_attr(cmd, "long_help")
1302 if help is None:
1303 raise YapError("Sorry, no help for '%s'. Ask Steven." % oldcmd)
1305 print >>sys.stderr, "The '%s' command" % oldcmd
1306 doc = self._get_attr(cmd, "__doc__")
1307 if doc is None:
1308 doc = ""
1309 print >>sys.stderr, "\tyap %s %s" % (oldcmd, doc)
1310 print >>sys.stderr, "%s" % help
1311 return
1313 print >> sys.stderr, "Yet Another (Git) Porcelein"
1314 print >> sys.stderr
1316 for name in dir(self):
1317 if not name.startswith('cmd_'):
1318 continue
1319 attr = self.__getattribute__(name)
1320 if not callable(attr):
1321 continue
1323 short_msg = self._get_attr(name, "short_help")
1324 if short_msg is None:
1325 continue
1327 name = name.replace('cmd_', '')
1328 name = name.replace('_', '-')
1329 print >> sys.stderr, "%-16s%s" % (name, short_msg)
1331 print >> sys.stderr
1332 print >> sys.stderr, "(*) Indicates that the command is not readily reversible"
1334 @short_help("show information about loaded plugins")
1335 def cmd_plugins(self):
1337 print >> sys.stderr, "Loaded plugins:"
1338 plugins = load_plugins()
1339 for name, cls in plugins.items():
1340 print "\t%-16s: %s" % (name, cls.__doc__)
1341 if not plugins:
1342 print "\t%-16s" % "None"
1344 def cmd_usage(self):
1345 print >> sys.stderr, "usage: %s <command>" % os.path.basename(sys.argv[0])
1346 print >> sys.stderr, " valid commands: help init clone add rm stage unstage status revert commit uncommit log show diff branch switch point cherry-pick repo track push fetch update history resolved version"
1348 def load_plugins():
1349 plugindir = os.path.join("~", ".yap", "plugins")
1350 plugindir = os.path.expanduser(plugindir)
1351 plugindir = os.path.join(plugindir, "*.py")
1353 plugins = dict()
1354 for p in glob.glob(os.path.expanduser(plugindir)):
1355 plugin = os.path.basename(p).replace('.py', '')
1356 m = __import__(plugin)
1357 for k in dir(m):
1358 cls = m.__dict__[k]
1359 if not type(cls) == type:
1360 continue
1361 if not issubclass(cls, YapCore):
1362 continue
1363 if cls is YapCore:
1364 continue
1365 plugins[k] = cls
1366 return plugins
1368 def yap_metaclass(name, bases, dct):
1369 plugindir = os.path.join("~", ".yap", "plugins")
1370 plugindir = os.path.expanduser(plugindir)
1371 sys.path.insert(0, plugindir)
1373 plugins = set(load_plugins().values())
1374 p2 = plugins.copy()
1375 for cls in plugins:
1376 p2 -= set(cls.__bases__)
1377 plugins = p2
1378 bases = list(plugins) + list(bases)
1379 return type(name, tuple(bases), dct)
1381 class Yap(YapCore):
1382 __metaclass__ = yap_metaclass
1384 def main(self, args):
1385 if len(args) < 1:
1386 self.cmd_usage()
1387 sys.exit(2)
1389 command = args[0]
1390 args = args[1:]
1392 if run_command("git --version"):
1393 print >>sys.stderr, "Failed to run git; is it installed?"
1394 sys.exit(1)
1396 debug = os.getenv('YAP_DEBUG')
1398 try:
1399 command = command.replace('-', '_')
1400 meth = self.__getattribute__("cmd_"+command)
1401 doc = self._get_attr("cmd_"+command, "__doc__")
1403 try:
1404 options = ""
1405 for c in self.__class__.__bases__:
1406 try:
1407 t = c.__dict__["cmd_"+command]
1408 except KeyError:
1409 continue
1410 if "options" in t.__dict__:
1411 options += t.options
1413 if options:
1414 try:
1415 flags, args = getopt.getopt(args, options)
1416 flags = dict(flags)
1417 except getopt.GetoptError, e:
1418 if debug:
1419 raise
1420 print "Usage: %s %s %s" % (os.path.basename(sys.argv[0]), command, doc)
1421 print e
1422 sys.exit(2)
1423 else:
1424 flags = dict()
1426 meth(*args, **flags)
1427 except (TypeError, getopt.GetoptError):
1428 if debug:
1429 raise
1430 print "Usage: %s %s %s" % (os.path.basename(sys.argv[0]), command, doc)
1431 except YapError, e:
1432 if debug:
1433 raise
1434 print >> sys.stderr, e
1435 sys.exit(1)
1436 except AttributeError:
1437 if debug:
1438 raise
1439 self.cmd_usage()
1440 sys.exit(2)