10 class ShellError(Exception):
11 def __init__(self
, cmd
, rc
):
16 return "%s returned %d" % (self
.cmd
, self
.rc
)
18 class YapError(Exception):
19 def __init__(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')
33 files
= self
._get
_new
_files
()
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')
42 files
= pickle
.load(file(path
))
49 if get_output("git ls-files --cached '%s'" % f
) != []:
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')
61 pickle
.dump(files
, open(path
, 'w'))
65 def _clear_new_files(self
):
66 repo
= get_output('git rev-parse --git-dir')[0]
67 path
= os
.path
.join(repo
, 'yap', 'new-files')
70 def _assert_file_exists(self
, file):
71 if not os
.access(file, os
.R_OK
):
72 raise YapError("No such file: %s" % file)
74 def _get_staged_files(self
):
75 if run_command("git rev-parse HEAD"):
76 files
= get_output("git ls-files --cached")
78 files
= get_output("git diff-index --cached --name-only HEAD")
79 unmerged
= self
._get
_unmerged
_files
()
81 unmerged
= set(unmerged
)
82 files
= set(files
).difference(unmerged
)
86 def _get_unstaged_files(self
):
87 files
= get_output("git ls-files -m")
88 prefix
= get_output("git rev-parse --show-prefix")
90 files
= [ os
.path
.join(prefix
[0], x
) for x
in files
]
91 files
+= self
._get
_new
_files
()
92 unmerged
= self
._get
_unmerged
_files
()
94 unmerged
= set(unmerged
)
95 files
= set(files
).difference(unmerged
)
99 def _get_unmerged_files(self
):
100 files
= get_output("git ls-files -u")
101 files
= [ x
.replace('\t', ' ').split(' ')[3] for x
in files
]
102 prefix
= get_output("git rev-parse --show-prefix")
104 files
= [ os
.path
.join(prefix
[0], x
) for x
in files
]
105 return list(set(files
))
107 def _delete_branch(self
, branch
, force
):
108 current
= get_output("git symbolic-ref HEAD")
110 current
= current
[0].replace('refs/heads/', '')
111 if branch
== current
:
112 raise YapError("Can't delete current branch")
114 ref
= get_output("git rev-parse --verify 'refs/heads/%s'" % branch
)
116 raise YapError("No such branch: %s" % branch
)
117 run_safely("git update-ref -d 'refs/heads/%s' '%s'" % (branch
, ref
[0]))
120 name
= get_output("git name-rev --name-only '%s'" % ref
[0])[0]
121 if name
== 'undefined':
122 run_command("git update-ref 'refs/heads/%s' '%s'" % (branch
, ref
[0]))
123 raise YapError("Refusing to delete leaf branch (use -f to force)")
124 def _get_pager_cmd(self
):
125 if 'YAP_PAGER' in os
.environ
:
126 return os
.environ
['YAP_PAGER']
127 elif 'GIT_PAGER' in os
.environ
:
128 return os
.environ
['GIT_PAGER']
129 elif 'PAGER' in os
.environ
:
130 return os
.environ
['PAGER']
134 def _add_one(self
, file):
135 self
._assert
_file
_exists
(file)
136 x
= get_output("git ls-files '%s'" % file)
138 raise YapError("File '%s' already in repository" % file)
139 self
._add
_new
_file
(file)
141 def _rm_one(self
, file):
142 self
._assert
_file
_exists
(file)
143 if get_output("git ls-files '%s'" % file) != []:
144 run_safely("git rm --cached '%s'" % file)
145 self
._remove
_new
_file
(file)
147 def _stage_one(self
, file, allow_unmerged
=False):
148 self
._assert
_file
_exists
(file)
149 prefix
= get_output("git rev-parse --show-prefix")
151 tmp
= os
.path
.normpath(os
.path
.join(prefix
[0], file))
154 if not allow_unmerged
and tmp
in self
._get
_unmerged
_files
():
155 raise YapError("Refusing to stage conflicted file: %s" % file)
156 run_safely("git update-index --add '%s'" % file)
158 def _unstage_one(self
, file):
159 self
._assert
_file
_exists
(file)
160 if run_command("git rev-parse HEAD"):
161 rc
= run_command("git update-index --force-remove '%s'" % file)
163 rc
= run_command("git diff-index --cached -p HEAD '%s' | git apply -R --cached" % file)
165 raise YapError("Failed to unstage")
167 def _revert_one(self
, file):
168 self
._assert
_file
_exists
(file)
170 self
._unstage
_one
(file)
173 run_safely("git checkout-index -u -f '%s'" % file)
175 def _parse_commit(self
, commit
):
176 lines
= get_output("git cat-file commit '%s'" % commit
)
181 if mode
!= 'commit' and l
.strip() == "":
186 commit
['log'].append(l
)
193 commit
['log'] = '\n'.join(commit
['log'])
196 def _check_commit(self
, **flags
):
197 if '-a' in flags
and '-d' in flags
:
198 raise YapError("Conflicting flags: -a and -d")
200 if '-d' not in flags
and self
._get
_unstaged
_files
():
201 if '-a' not in flags
and self
._get
_staged
_files
():
202 raise YapError("Staged and unstaged changes present. Specify what to commit")
203 os
.system("git diff-files -p | git apply --cached")
204 for f
in self
._get
_new
_files
():
207 def _do_uncommit(self
):
208 commit
= self
._parse
_commit
("HEAD")
209 repo
= get_output('git rev-parse --git-dir')[0]
210 dir = os
.path
.join(repo
, 'yap')
215 msg_file
= os
.path
.join(dir, 'msg')
216 fd
= file(msg_file
, 'w')
217 print >>fd
, commit
['log']
220 tree
= get_output("git rev-parse --verify HEAD^")
221 run_safely("git update-ref -m uncommit HEAD '%s'" % tree
[0])
223 def _do_commit(self
, msg
=None):
224 tree
= get_output("git write-tree")[0]
226 repo
= get_output('git rev-parse --git-dir')[0]
227 head_file
= os
.path
.join(repo
, 'yap', 'merge')
229 parent
= pickle
.load(file(head_file
))
231 parent
= get_output("git rev-parse --verify HEAD 2> /dev/null")
233 if os
.environ
.has_key('YAP_EDITOR'):
234 editor
= os
.environ
['YAP_EDITOR']
235 elif os
.environ
.has_key('GIT_EDITOR'):
236 editor
= os
.environ
['GIT_EDITOR']
237 elif os
.environ
.has_key('EDITOR'):
238 editor
= os
.environ
['EDITOR']
242 fd
, tmpfile
= tempfile
.mkstemp("yap")
247 msg_file
= os
.path
.join(repo
, 'yap', 'msg')
248 if os
.access(msg_file
, os
.R_OK
):
250 fd2
= file(tmpfile
, 'w')
251 for l
in fd1
.xreadlines():
252 print >>fd2
, l
.strip()
255 if os
.system("%s '%s'" % (editor
, tmpfile
)) != 0:
256 raise YapError("Editing commit message failed")
263 raise YapError("Refusing to use empty commit message")
265 (fd_w
, fd_r
) = os
.popen2("git stripspace > %s" % tmpfile
)
271 parent
= ' -p '.join(parent
)
272 commit
= get_output("git commit-tree '%s' -p %s < '%s'" % (tree
, parent
, tmpfile
))
274 commit
= get_output("git commit-tree '%s' < '%s'" % (tree
, tmpfile
))
277 run_safely("git update-ref HEAD '%s'" % commit
[0])
280 def _check_rebasing(self
):
281 repo
= get_output('git rev-parse --git-dir')[0]
282 dotest
= os
.path
.join(repo
, '.dotest')
283 if os
.access(dotest
, os
.R_OK
):
284 raise YapError("A git operation is in progress. Complete it first")
285 dotest
= os
.path
.join(repo
, '..', '.dotest')
286 if os
.access(dotest
, os
.R_OK
):
287 raise YapError("A git operation is in progress. Complete it first")
289 def _check_git(self
):
290 if run_command("git rev-parse --git-dir"):
291 raise YapError("That command must be run from inside a git repository")
293 def _list_remotes(self
):
294 remotes
= get_output("git config --get-regexp '^remote.*.url'")
296 remote
, url
= x
.split(' ')
297 remote
= remote
.replace('remote.', '')
298 remote
= remote
.replace('.url', '')
301 def _unstage_all(self
):
303 run_safely("git read-tree -m HEAD")
305 run_safely("git read-tree HEAD")
306 run_safely("git update-index -q --refresh")
308 def _get_tracking(self
, current
):
309 remote
= get_output("git config branch.%s.remote" % current
)
311 raise YapError("No tracking branch configured for '%s'" % current
)
313 merge
= get_output("git config branch.%s.merge" % current
)
315 raise YapError("No tracking branch configured for '%s'" % current
)
316 return remote
[0], merge
[0]
318 def _confirm_push(self
, current
, rhs
, repo
):
319 print "About to push local branch '%s' to '%s' on '%s'" % (current
, rhs
, repo
)
320 print "Continue (y/n)? ",
322 ans
= sys
.stdin
.readline().strip()
324 if ans
.lower() != 'y' and ans
.lower() != 'yes':
325 raise YapError("Aborted.")
327 def _clear_state(self
):
328 repo
= get_output('git rev-parse --git-dir')[0]
329 dir = os
.path
.join(repo
, 'yap')
330 for f
in "new-files", "merge", "msg":
332 os
.unlink(os
.path
.join(dir, f
))
336 def _get_attr(self
, name
, attr
):
338 for c
in self
.__class
__.__bases
__:
340 m2
= c
.__dict
__[name
]
344 val
= m2
.__getattribute__(attr
)
345 except AttributeError:
349 @short_help("make a local copy of an existing repository")
351 The first argument is a URL to the existing repository. This can be an
352 absolute path if the repository is local, or a URL with the git://,
353 ssh://, or http:// schemes. By default, the directory used is the last
354 component of the URL, sans '.git'. This can be overridden by providing
357 def cmd_clone(self
, url
, directory
=None):
360 if '://' not in url
and url
[0] != '/':
361 url
= os
.path
.join(os
.getcwd(), url
)
363 url
= url
.rstrip('/')
364 if directory
is None:
365 directory
= url
.rsplit('/')[-1]
366 directory
= directory
.replace('.git', '')
371 raise YapError("Directory exists: %s" % directory
)
374 self
.cmd_repo("origin", url
)
375 self
.cmd_fetch("origin")
378 if not run_command("git rev-parse --verify refs/remotes/origin/HEAD"):
379 hash = get_output("git rev-parse refs/remotes/origin/HEAD")[0]
380 for b
in get_output("git for-each-ref --format='%(refname)' 'refs/remotes/origin/*'"):
381 if get_output("git rev-parse %s" % b
)[0] == hash:
385 if not run_command("git rev-parse --verify refs/remotes/origin/master"):
386 branch
= "refs/remotes/origin/master"
388 branch
= get_output("git for-each-ref --format='%(refname)' 'refs/remotes/origin/*'")
391 hash = get_output("git rev-parse %s" % branch
)
393 branch
= branch
.replace('refs/remotes/origin/', '')
394 run_safely("git update-ref refs/heads/%s %s" % (branch
, hash[0]))
395 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch
)
396 self
.cmd_revert(**{'-a': 1})
398 @short_help("turn a directory into a repository")
400 Converts the current working directory into a repository. The primary
401 side-effect of this command is the creation of a '.git' subdirectory.
402 No files are added nor commits made.
405 os
.system("git init")
407 @short_help("add a new file to the repository")
409 The arguments are the files to be added to the repository. Once added,
410 the files will show as "unstaged changes" in the output of 'status'. To
411 reverse the effects of this command, see 'rm'.
413 def cmd_add(self
, *files
):
424 @short_help("delete a file from the repository")
426 The arguments are the files to be removed from the current revision of
427 the repository. The files will still exist in any past commits that the
428 files may have been a part of. The file is not actually deleted, it is
429 just no longer tracked as part of the repository.
431 def cmd_rm(self
, *files
):
441 @short_help("stage changes in a file for commit")
443 The arguments are the files to be staged. Staging changes is a way to
444 build up a commit when you do not want to commit all changes at once.
445 To commit only staged changes, use the '-d' flag to 'commit.' To
446 reverse the effects of this command, see 'unstage'. Once staged, the
447 files will show as "staged changes" in the output of 'status'.
449 def cmd_stage(self
, *files
):
459 @short_help("unstage changes in a file")
461 The arguments are the files to be unstaged. Once unstaged, the files
462 will show as "unstaged changes" in the output of 'status'. The '-a'
463 flag can be used to unstage all staged changes at once.
466 def cmd_unstage(self
, *files
, **flags
):
470 files
= self
._get
_staged
_files
()
479 @short_help("show files with staged and unstaged changes")
481 Show the files in the repository with changes since the last commit,
482 categorized based on whether the changes are staged or not. A file may
483 appear under each heading if the same file has both staged and unstaged
486 def cmd_status(self
):
489 branch
= get_output("git symbolic-ref HEAD")
491 branch
= branch
[0].replace('refs/heads/', '')
494 print "Current branch: %s" % branch
496 print "Files with staged changes:"
497 files
= self
._get
_staged
_files
()
503 print "Files with unstaged changes:"
504 files
= self
._get
_unstaged
_files
()
510 files
= self
._get
_unmerged
_files
()
512 print "Files with conflicts:"
516 @short_help("remove uncommitted changes from a file (*)")
518 The arguments are the files whose changes will be reverted. If the '-a'
519 flag is given, then all files will have uncommitted changes removed.
520 Note that there is no way to reverse this command short of manually
521 editing each file again.
524 def cmd_revert(self
, *files
, **flags
):
529 run_safely("git checkout-index -u -f -a")
541 @short_help("record changes to files as a new commit")
543 Create a new commit recording changes since the last commit. If there
544 are only unstaged changes, those will be recorded. If there are only
545 staged changes, those will be recorded. Otherwise, you will have to
546 specify either the '-a' flag or the '-d' flag to commit all changes or
547 only staged changes, respectively. To reverse the effects of this
548 command, see 'uncommit'.
550 @takes_options("adm:")
551 def cmd_commit(self
, **flags
):
552 "[-a | -d] [-m <msg>]"
554 self
._check
_rebasing
()
555 self
._check
_commit
(**flags
)
556 if not self
._get
_staged
_files
():
557 raise YapError("No changes to commit")
558 msg
= flags
.get('-m', None)
562 @short_help("reverse the actions of the last commit")
564 Reverse the effects of the last 'commit' operation. The changes that
565 were part of the previous commit will show as "staged changes" in the
566 output of 'status'. This means that if no files were changed since the
567 last commit was created, 'uncommit' followed by 'commit' is a lossless
570 def cmd_uncommit(self
):
576 @short_help("report the current version of yap")
577 def cmd_version(self
):
578 print "Yap version 0.1"
580 @short_help("show the changelog for particular versions or files")
582 The arguments are the files with which to filter history. If none are
583 given, all changes are listed. Otherwise only commits that affected one
584 or more of the given files are listed. The -r option changes the
585 starting revision for traversing history. By default, history is listed
588 @takes_options("pr:")
589 def cmd_log(self
, *paths
, **flags
):
590 "[-p] [-r <rev>] <path>..."
592 rev
= flags
.get('-r', 'HEAD')
601 paths
= ' '.join(paths
)
602 os
.system("git log -M -C %s %s '%s' -- %s"
603 % (follow
, flags
.get('-p', '--name-status'), rev
, paths
))
605 @short_help("show staged, unstaged, or all uncommitted changes")
607 Show staged, unstaged, or all uncommitted changes. By default, all
608 changes are shown. The '-u' flag causes only unstaged changes to be
609 shown. The '-d' flag causes only staged changes to be shown.
612 def cmd_diff(self
, **flags
):
615 if '-u' in flags
and '-d' in flags
:
616 raise YapError("Conflicting flags: -u and -d")
618 pager
= self
._get
_pager
_cmd
()
621 os
.system("git diff-files -p | %s" % pager
)
623 os
.system("git diff-index --cached -p HEAD | %s" % pager
)
625 os
.system("git diff-index -p HEAD | %s" % pager
)
627 @short_help("list, create, or delete branches")
629 If no arguments are specified, a list of local branches is given. The
630 current branch is indicated by a "*" next to the name. If an argument
631 is given, it is taken as the name of a new branch to create. The branch
632 will start pointing at the current HEAD. See 'point' for details on
633 changing the revision of the new branch. Note that this command does
634 not switch the current working branch. See 'switch' for details on
635 changing the current working branch.
637 The '-d' flag can be used to delete local branches. If the delete
638 operation would remove the last branch reference to a given line of
639 history (colloquially referred to as "dangling commits"), yap will
640 report an error and abort. The '-f' flag can be used to force the delete
643 @takes_options("fd:")
644 def cmd_branch(self
, branch
=None, **flags
):
645 "[ [-f] -d <branch> | <branch> ]"
647 force
= '-f' in flags
649 self
._delete
_branch
(flags
['-d'], force
)
653 if branch
is not None:
654 ref
= get_output("git rev-parse --verify HEAD")
656 raise YapError("No branch point yet. Make a commit")
657 run_safely("git update-ref 'refs/heads/%s' '%s'" % (branch
, ref
[0]))
659 current
= get_output("git symbolic-ref HEAD")
660 branches
= get_output("git for-each-ref --format='%(refname)' 'refs/heads/*'")
662 if current
and b
== current
[0]:
666 b
= b
.replace('refs/heads/', '')
669 @short_help("change the current working branch")
671 The argument is the name of the branch to make the current working
672 branch. This command will fail if there are uncommitted changes to any
673 files. Otherwise, the contents of the files in the working directory
674 are updated to reflect their state in the new branch. Additionally, any
675 future commits are added to the new branch instead of the previous line
679 def cmd_switch(self
, branch
, **flags
):
682 self
._check
_rebasing
()
683 ref
= get_output("git rev-parse --verify 'refs/heads/%s'" % branch
)
685 raise YapError("No such branch: %s" % branch
)
687 if '-f' not in flags
:
688 if (self
._get
_staged
_files
()
689 or (self
._get
_unstaged
_files
()
690 and run_command("git update-index --refresh"))):
691 raise YapError("You have uncommitted changes. Use -f to continue anyway")
693 if self
._get
_unstaged
_files
() and self
._get
_staged
_files
():
694 raise YapError("You have staged and unstaged changes. Perhaps unstage -a?")
696 staged
= bool(self
._get
_staged
_files
())
698 run_command("git diff-files -p | git apply --cached")
699 for f
in self
._get
_new
_files
():
702 idx
= get_output("git write-tree")
703 new
= get_output("git rev-parse refs/heads/%s" % branch
)
704 readtree
= "git read-tree --aggressive -u -m HEAD %s %s" % (idx
[0], new
[0])
705 if run_command(readtree
):
706 run_command("git update-index --refresh")
707 if os
.system(readtree
):
708 raise YapError("Failed to switch")
709 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch
)
711 if '-f' not in flags
:
718 @short_help("move the current branch to a different revision")
720 The argument is the hash of the commit to which the current branch
721 should point, or alternately a branch or tag (a.k.a, "committish"). If
722 moving the branch would create "dangling commits" (see 'branch'), yap
723 will report an error and abort. The '-f' flag can be used to force the
724 operation in spite of this.
727 def cmd_point(self
, where
, **flags
):
730 self
._check
_rebasing
()
732 head
= get_output("git rev-parse --verify HEAD")
734 raise YapError("No commit yet; nowhere to point")
736 ref
= get_output("git rev-parse --verify '%s^{commit}'" % where
)
738 raise YapError("Not a valid ref: %s" % where
)
740 if self
._get
_unstaged
_files
() or self
._get
_staged
_files
():
741 raise YapError("You have uncommitted changes. Commit them first")
743 run_safely("git update-ref HEAD '%s'" % ref
[0])
745 if '-f' not in flags
:
746 name
= get_output("git name-rev --name-only '%s'" % head
[0])[0]
747 if name
== "undefined":
748 os
.system("git update-ref HEAD '%s'" % head
[0])
749 raise YapError("Pointing there will lose commits. Use -f to force")
752 run_safely("git read-tree -u -m HEAD")
754 run_safely("git read-tree HEAD")
755 run_safely("git checkout-index -u -f -a")
758 @short_help("alter history by dropping or amending commits")
760 This command operates in two distinct modes, "amend" and "drop" mode.
761 In drop mode, the given commit is removed from the history of the
762 current branch, as though that commit never happened. By default the
765 In amend mode, the uncommitted changes present are merged into a
766 previous commit. This is useful for correcting typos or adding missed
767 files into past commits. By default the commit used is HEAD.
769 While rewriting history it is possible that conflicts will arise. If
770 this happens, the rewrite will pause and you will be prompted to resolve
771 the conflicts and stage them. Once that is done, you will run "yap
772 history continue." If instead you want the conflicting commit removed
773 from history (perhaps your changes supercede that commit) you can run
774 "yap history skip". Once the rewrite completes, your branch will be on
775 the same commit as when the rewrite started.
777 def cmd_history(self
, subcmd
, *args
):
778 "amend | drop <commit>"
781 if subcmd
not in ("amend", "drop", "continue", "skip"):
785 When you have resolved the conflicts run \"yap history continue\".
786 To skip the problematic patch, run \"yap history skip\"."""
788 if subcmd
== "continue":
789 os
.system("git am -3 -r --resolvemsg='%s'" % resolvemsg
)
792 os
.system("git reset --hard")
793 os
.system("git am -3 --skip --resolvemsg='%s'" % resolvemsg
)
796 if subcmd
== "amend":
797 flags
, args
= getopt
.getopt(args
, "ad")
807 if run_command("git rev-parse --verify '%s'" % commit
):
808 raise YapError("Not a valid commit: %s" % commit
)
810 self
._check
_rebasing
()
812 if subcmd
== "amend":
813 self
._check
_commit
(**flags
)
814 if self
._get
_unstaged
_files
():
815 # XXX: handle unstaged changes better
816 raise YapError("Commit away changes that you aren't amending")
820 start
= get_output("git rev-parse HEAD")
821 stash
= get_output("git stash create")
822 run_command("git reset --hard")
824 fd
, tmpfile
= tempfile
.mkstemp("yap")
828 os
.system("git format-patch -k --stdout '%s' > %s" % (commit
, tmpfile
))
829 if subcmd
== "amend":
830 self
.cmd_point(commit
, **{'-f': True})
832 if subcmd
== "amend":
834 rc
= os
.system("git stash apply %s" % stash
[0])
836 self
.cmd_point(start
[0], **{'-f': True})
837 os
.system("git stash apply %s" % stash
[0])
838 raise YapError("Failed to apply stash")
841 if subcmd
== "amend":
843 self
._check
_commit
(**{'-a': True})
846 self
.cmd_point("%s^" % commit
, **{'-f': True})
848 stat
= os
.stat(tmpfile
)
851 run_safely("git update-index --refresh")
852 rc
= os
.system("git am -3 --resolvemsg=\'%s\' %s" % (resolvemsg
, tmpfile
))
854 raise YapError("Failed to apply changes")
859 run_command("git stash apply %s" % stash
[0])
862 @short_help("show the changes introduced by a given commit")
864 By default, the changes in the last commit are shown. To override this,
865 specify a hash, branch, or tag (committish). The hash of the commit,
866 the commit's author, log message, and a diff of the changes are shown.
868 def cmd_show(self
, commit
="HEAD"):
871 os
.system("git show '%s'" % commit
)
873 @short_help("apply the changes in a given commit to the current branch")
875 The argument is the hash, branch, or tag (committish) of the commit to
876 be applied. In general, it only makes sense to apply commits that
877 happened on another branch. The '-r' flag can be used to have the
878 changes in the given commit reversed from the current branch. In
879 general, this only makes sense for commits that happened on the current
883 def cmd_cherry_pick(self
, commit
, **flags
):
887 os
.system("git revert '%s'" % commit
)
889 os
.system("git cherry-pick '%s'" % commit
)
891 @short_help("list, add, or delete configured remote repositories")
893 When invoked with no arguments, this command will show the list of
894 currently configured remote repositories, giving both the name and URL
895 of each. To add a new repository, give the desired name as the first
896 argument and the URL as the second. The '-d' flag can be used to remove
897 a previously added repository.
900 def cmd_repo(self
, name
=None, url
=None, **flags
):
901 "[<name> <url> | -d <name>]"
903 if name
is not None and url
is None:
907 if flags
['-d'] not in [ x
[0] for x
in self
._list
_remotes
() ]:
908 raise YapError("No such repository: %s" % flags
['-d'])
909 os
.system("git config --unset remote.%s.url" % flags
['-d'])
910 os
.system("git config --unset remote.%s.fetch" % flags
['-d'])
913 if name
in [ x
[0] for x
in self
._list
_remotes
() ]:
914 raise YapError("Repository '%s' already exists" % flags
['-d'])
915 os
.system("git config remote.%s.url %s" % (name
, url
))
916 os
.system("git config remote.%s.fetch +refs/heads/*:refs/remotes/%s/*" % (name
, name
))
918 for remote
, url
in self
._list
_remotes
():
919 print "%-20s %s" % (remote
, url
)
921 @short_help("send local commits to a remote repository (*)")
923 When invoked with no arguments, the current branch is synchronized to
924 the tracking branch of the tracking remote. If no tracking remote is
925 specified, the repository will have to be specified on the command line.
926 In that case, the default is to push to a branch with the same name as
927 the current branch. This behavior can be overridden by giving a second
928 argument to specify the remote branch.
930 If the remote branch does not currently exist, the command will abort
931 unless the -c flag is provided. If the remote branch is not a direct
932 descendent of the local branch, the command will abort unless the -f
933 flag is provided. Forcing a push in this way can be problematic to
934 other users of the repository if they are not expecting it.
936 To delete a branch on the remote repository, use the -d flag.
938 @takes_options("cdf")
939 def cmd_push(self
, repo
=None, rhs
=None, **flags
):
942 if '-c' in flags
and '-d' in flags
:
945 if repo
and repo
not in [ x
[0] for x
in self
._list
_remotes
() ]:
946 raise YapError("No such repository: %s" % repo
)
948 current
= get_output("git symbolic-ref HEAD")
950 raise YapError("Not on a branch!")
952 self
._check
_rebasing
()
954 current
= current
[0].replace('refs/heads/', '')
955 remote
= get_output("git config branch.%s.remote" % current
)
956 if repo
is None and remote
:
960 raise YapError("No tracking branch configured; specify destination repository")
962 if rhs
is None and remote
and remote
[0] == repo
:
963 merge
= get_output("git config branch.%s.merge" % current
)
968 rhs
= "refs/heads/%s" % current
970 if '-c' not in flags
and '-d' not in flags
:
971 if run_command("git rev-parse --verify refs/remotes/%s/%s"
972 % (repo
, rhs
.replace('refs/heads/', ''))):
973 raise YapError("No matching branch on that repo. Use -c to create a new branch there.")
974 if '-f' not in flags
:
975 hash = get_output("git rev-parse refs/remotes/%s/%s" % (repo
, rhs
.replace('refs/heads/', '')))
976 base
= get_output("git merge-base HEAD %s" % hash[0])
978 if base
[0] != hash[0]:
979 raise YapError("Branch not up-to-date with remote. Update or use -f")
981 self
._confirm
_push
(current
, rhs
, repo
)
988 lhs
= "refs/heads/%s" % current
989 rc
= os
.system("git push %s %s %s:%s" % (flags
.get('-f', ''), repo
, lhs
, rhs
))
991 raise YapError("Push failed.")
993 @short_help("retrieve commits from a remote repository")
995 When run with no arguments, the command will retrieve new commits from
996 the remote tracking repository. Note that this does not in any way
997 alter the current branch. For that, see "update". If a remote other
998 than the tracking remote is desired, it can be specified as the first
1001 def cmd_fetch(self
, repo
=None):
1004 current
= get_output("git symbolic-ref HEAD")
1006 raise YapError("Not on a branch!")
1008 if repo
and repo
not in [ x
[0] for x
in self
._list
_remotes
() ]:
1009 raise YapError("No such repository: %s" % repo
)
1011 current
= current
[0].replace('refs/heads/', '')
1012 remote
= get_output("git config branch.%s.remote" % current
)
1016 raise YapError("No tracking branch configured; specify a repository")
1017 os
.system("git fetch %s" % repo
)
1019 @short_help("update the current branch relative to its tracking branch")
1021 Updates the current branch relative to its remote tracking branch. This
1022 command requires that the current branch have a remote tracking branch
1023 configured. If any conflicts occur while applying your changes to the
1024 updated remote, the command will pause to allow you to fix them. Once
1025 that is done, run "update" with the "continue" subcommand. Alternately,
1026 the "skip" subcommand can be used to discard the conflicting changes.
1028 def cmd_update(self
, subcmd
=None):
1031 if subcmd
and subcmd
not in ["continue", "skip"]:
1035 When you have resolved the conflicts run \"yap update continue\".
1036 To skip the problematic patch, run \"yap update skip\"."""
1038 if subcmd
== "continue":
1039 os
.system("git am -3 -r --resolvemsg='%s'" % resolvemsg
)
1041 if subcmd
== "skip":
1042 os
.system("git reset --hard")
1043 os
.system("git am -3 --skip --resolvemsg='%s'" % resolvemsg
)
1046 self
._check
_rebasing
()
1047 if self
._get
_unstaged
_files
() or self
._get
_staged
_files
():
1048 raise YapError("You have uncommitted changes. Commit them first")
1050 current
= get_output("git symbolic-ref HEAD")
1052 raise YapError("Not on a branch!")
1054 current
= current
[0].replace('refs/heads/', '')
1055 remote
, merge
= self
._get
_tracking
(current
)
1056 merge
= merge
.replace('refs/heads/', '')
1058 self
.cmd_fetch(remote
)
1059 base
= get_output("git merge-base HEAD refs/remotes/%s/%s" % (remote
, merge
))
1062 fd
, tmpfile
= tempfile
.mkstemp("yap")
1064 os
.system("git format-patch -k --stdout '%s' > %s" % (base
[0], tmpfile
))
1065 self
.cmd_point("refs/remotes/%s/%s" % (remote
, merge
), **{'-f': True})
1067 stat
= os
.stat(tmpfile
)
1070 rc
= os
.system("git am -3 --resolvemsg=\'%s\' %s" % (resolvemsg
, tmpfile
))
1072 raise YapError("Failed to apply changes")
1076 @short_help("query and configure remote branch tracking")
1078 When invoked with no arguments, the command displays the tracking
1079 information for the current branch. To configure the tracking
1080 information, two arguments for the remote repository and remote branch
1081 are given. The tracking information is used to provide defaults for
1082 where to push local changes and from where to get updates to the branch.
1084 def cmd_track(self
, repo
=None, branch
=None):
1088 current
= get_output("git symbolic-ref HEAD")
1090 raise YapError("Not on a branch!")
1091 current
= current
[0].replace('refs/heads/', '')
1093 if repo
is None and branch
is None:
1094 repo
, merge
= self
._get
_tracking
(current
)
1095 merge
= merge
.replace('refs/heads/', '')
1096 print "Branch '%s' tracking refs/remotes/%s/%s" % (current
, repo
, merge
)
1099 if repo
is None or branch
is None:
1102 if repo
not in [ x
[0] for x
in self
._list
_remotes
() ]:
1103 raise YapError("No such repository: %s" % repo
)
1105 if run_command("git rev-parse --verify refs/remotes/%s/%s" % (repo
, branch
)):
1106 raise YapError("No such branch '%s' on repository '%s'" % (branch
, repo
))
1108 os
.system("git config branch.%s.remote '%s'" % (current
, repo
))
1109 os
.system("git config branch.%s.merge 'refs/heads/%s'" % (current
, branch
))
1110 print "Branch '%s' now tracking refs/remotes/%s/%s" % (current
, repo
, branch
)
1112 @short_help("mark files with conflicts as resolved")
1114 The arguments are the files to be marked resolved. When a conflict
1115 occurs while merging changes to a file, that file is marked as
1116 "unmerged." Until the file(s) with conflicts are marked resolved,
1117 commits cannot be made.
1119 def cmd_resolved(self
, *files
):
1126 self
._stage
_one
(f
, True)
1129 @short_help("merge a branch into the current branch")
1130 def cmd_merge(self
, branch
):
1134 branch_name
= branch
1135 branch
= get_output("git rev-parse --verify %s" % branch
)
1137 raise YapError("No such branch: %s" % branch
)
1140 base
= get_output("git merge-base HEAD %s" % branch
)
1142 raise YapError("Branch '%s' is not a fork of the current branch"
1145 readtree
= ("git read-tree --aggressive -u -m %s HEAD %s"
1146 % (base
[0], branch
))
1147 if run_command(readtree
):
1148 run_command("git update-index --refresh")
1149 if os
.system(readtree
):
1150 raise YapError("Failed to merge")
1152 repo
= get_output('git rev-parse --git-dir')[0]
1153 dir = os
.path
.join(repo
, 'yap')
1158 msg_file
= os
.path
.join(dir, 'msg')
1159 msg
= file(msg_file
, 'w')
1160 print >>msg
, "Merge branch '%s'" % branch_name
1163 head
= get_output("git rev-parse --verify HEAD")
1165 heads
= [head
[0], branch
]
1166 head_file
= os
.path
.join(dir, 'merge')
1167 pickle
.dump(heads
, file(head_file
, 'w'))
1169 self
._merge
_index
(branch
, base
[0])
1170 if self
._get
_unmerged
_files
():
1172 raise YapError("Fix conflicts then commit")
1176 def _merge_index(self
, branch
, base
):
1177 for f
in self
._get
_unmerged
_files
():
1178 fd
, bfile
= tempfile
.mkstemp("yap")
1180 rc
= os
.system("git show %s:%s > %s" % (base
, f
, bfile
))
1183 fd
, ofile
= tempfile
.mkstemp("yap")
1185 rc
= os
.system("git show %s:%s > %s" % (branch
, f
, ofile
))
1188 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
)
1189 rc
= os
.system(command
)
1195 self
._stage
_one
(f
, True)
1197 def cmd_help(self
, cmd
=None):
1200 cmd
= "cmd_" + cmd
.replace('-', '_')
1202 attr
= self
.__getattribute
__(cmd
)
1203 except AttributeError:
1204 raise YapError("No such command: %s" % cmd
)
1207 help = self
._get
_attr
(cmd
, "long_help")
1208 except AttributeError:
1210 raise YapError("Sorry, no help for '%s'. Ask Steven." % cmd
)
1212 print >>sys
.stderr
, "The '%s' command" % oldcmd
1213 print >>sys
.stderr
, "\tyap %s %s" % (oldcmd
, attr
.__doc
__)
1214 print >>sys
.stderr
, "%s" % help
1217 print >> sys
.stderr
, "Yet Another (Git) Porcelein"
1220 for name
in dir(self
):
1221 if not name
.startswith('cmd_'):
1223 attr
= self
.__getattribute
__(name
)
1224 if not callable(attr
):
1228 short_msg
= self
._get
_attr
(name
, "short_help")
1229 except AttributeError:
1232 name
= name
.replace('cmd_', '')
1233 name
= name
.replace('_', '-')
1234 print >> sys
.stderr
, "%-16s%s" % (name
, short_msg
)
1237 print >> sys
.stderr
, "(*) Indicates that the command is not readily reversible"
1239 @short_help("show information about loaded plugins")
1240 def cmd_plugins(self
):
1242 print >> sys
.stderr
, "Loaded plugins:"
1243 plugins
= load_plugins()
1244 for name
, cls
in plugins
.items():
1245 print "\t%-16s: %s" % (name
, cls
.__doc
__)
1247 print "\t%-16s" % "None"
1249 def cmd_usage(self
):
1250 print >> sys
.stderr
, "usage: %s <command>" % os
.path
.basename(sys
.argv
[0])
1251 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"
1254 plugindir
= os
.path
.join("~", ".yap", "plugins")
1255 plugindir
= os
.path
.expanduser(plugindir
)
1256 plugindir
= os
.path
.join(plugindir
, "*.py")
1259 for p
in glob
.glob(os
.path
.expanduser(plugindir
)):
1260 plugin
= os
.path
.basename(p
).replace('.py', '')
1261 m
= __import__(plugin
)
1264 if not type(cls
) == type:
1266 if not issubclass(cls
, YapCore
):
1273 def yap_metaclass(name
, bases
, dct
):
1274 plugindir
= os
.path
.join("~", ".yap", "plugins")
1275 plugindir
= os
.path
.expanduser(plugindir
)
1276 sys
.path
.insert(0, plugindir
)
1278 plugins
= set(load_plugins().values())
1281 p2
-= set(cls
.__bases
__)
1283 bases
= list(plugins
) + list(bases
)
1284 return type(name
, tuple(bases
), dct
)
1287 __metaclass__
= yap_metaclass
1289 def main(self
, args
):
1297 if run_command("git --version"):
1298 print >>sys
.stderr
, "Failed to run git; is it installed?"
1301 debug
= os
.getenv('YAP_DEBUG')
1304 command
= command
.replace('-', '_')
1305 meth
= self
.__getattribute
__("cmd_"+command
)
1306 doc
= self
._get
_attr
("cmd_"+command
, "__doc__")
1310 for c
in self
.__class
__.__bases
__:
1312 t
= c
.__dict
__["cmd_"+command
]
1315 if "options" in t
.__dict
__:
1316 options
+= t
.options
1320 flags
, args
= getopt
.getopt(args
, options
)
1322 except getopt
.GetoptError
, e
:
1325 print "Usage: %s %s %s" % (os
.path
.basename(sys
.argv
[0]), command
, doc
)
1331 meth(*args
, **flags
)
1332 except (TypeError, getopt
.GetoptError
):
1335 print "Usage: %s %s %s" % (os
.path
.basename(sys
.argv
[0]), command
, doc
)
1339 print >> sys
.stderr
, e
1341 except AttributeError: