9 output
= fd
.readlines()
11 return [x
.strip() for x
in output
]
14 rc
= os
.system("%s > /dev/null 2>&1" % cmd
)
18 class YapError(Exception):
19 def __init__(self
, msg
):
25 def takes_options(options
):
27 func
.options
= options
32 def _add_new_file(self
, file):
33 repo
= get_output('git rev-parse --git-dir')[0]
34 dir = os
.path
.join(repo
, 'yap')
39 files
= self
._get
_new
_files
()
41 path
= os
.path
.join(dir, 'new-files')
42 pickle
.dump(files
, open(path
, 'w'))
44 def _get_new_files(self
):
45 repo
= get_output('git rev-parse --git-dir')[0]
46 path
= os
.path
.join(repo
, 'yap', 'new-files')
48 files
= pickle
.load(file(path
))
55 if get_output("git ls-files --cached '%s'" % f
) != []:
60 def _remove_new_file(self
, file):
61 files
= self
._get
_new
_files
()
62 files
= filter(lambda x
: x
!= file, files
)
64 repo
= get_output('git rev-parse --git-dir')[0]
65 path
= os
.path
.join(repo
, 'yap', 'new-files')
66 pickle
.dump(files
, open(path
, 'w'))
68 def _clear_new_files(self
):
69 repo
= get_output('git rev-parse --git-dir')[0]
70 path
= os
.path
.join(repo
, 'yap', 'new-files')
73 def _assert_file_exists(self
, file):
74 if not os
.access(file, os
.R_OK
):
75 raise YapError("No such file: %s" % file)
77 def _get_staged_files(self
):
78 if run_command("git rev-parse HEAD"):
79 files
= get_output("git ls-files --cached")
81 files
= get_output("git diff-index --cached --name-only HEAD")
84 def _get_unstaged_files(self
):
85 files
= self
._get
_new
_files
()
86 files
+= get_output("git ls-files -m")
89 def cmd_clone(self
, url
, directory
=""):
90 # XXX: implement in terms of init + remote add + fetch
91 os
.system("git clone '%s' %s" % (url
, directory
))
96 def cmd_add(self
, file):
97 self
._assert
_file
_exists
(file)
98 x
= get_output("git ls-files '%s'" % file)
100 raise YapError("File '%s' already in repository" % file)
101 self
._add
_new
_file
(file)
104 def cmd_rm(self
, file):
105 self
._assert
_file
_exists
(file)
106 if get_output("git ls-files '%s'" % file) != []:
107 os
.system("git rm --cached '%s'" % file)
108 self
._remove
_new
_file
(file)
111 def cmd_stage(self
, file, quiet
=False):
112 self
._assert
_file
_exists
(file)
113 os
.system("git update-index --add '%s'" % file)
117 def cmd_unstage(self
, file):
118 self
._assert
_file
_exists
(file)
119 if run_command("git rev-parse HEAD"):
120 os
.system("git update-index --force-remove '%s'" % file)
122 os
.system("git diff-index HEAD '%s' | git apply -R --cached" % file)
125 def cmd_status(self
):
126 branch
= get_output("git symbolic-ref HEAD")[0]
127 branch
= branch
.replace('refs/heads/', '')
128 print "Current branch: %s" % branch
130 print "Files with staged changes:"
131 files
= self
._get
_staged
_files
()
137 print "Files with unstaged changes:"
138 files
= self
._get
_unstaged
_files
()
144 def cmd_unedit(self
, file):
145 self
._assert
_file
_exists
(file)
146 os
.system("git checkout-index -f '%s'" % file)
149 def cmd_commit(self
):
150 if self
._get
_unstaged
_files
():
151 if self
._get
_staged
_files
():
152 raise YapError("Staged and unstaged changes present. Specify what to commit")
153 os
.system("git diff-files -p | git apply --cached 2>/dev/null")
154 for f
in self
._get
_new
_files
():
155 self
.cmd_stage(f
, True)
157 if not self
._get
_staged
_files
():
158 raise YapError("No changes to commit")
160 tree
= get_output("git write-tree")[0]
162 parent
= get_output("git rev-parse HEAD 2> /dev/null")[0]
164 if os
.environ
.has_key('YAP_EDITOR'):
165 editor
= os
.environ
['YAP_EDITOR']
166 elif os
.environ
.has_key('GIT_EDITOR'):
167 editor
= os
.environ
['GIT_EDITOR']
168 elif os
.environ
.has_key('EDITOR'):
169 editor
= os
.environ
['EDITOR']
173 fd
, tmpfile
= tempfile
.mkstemp("yap")
175 if os
.system("%s '%s'" % (editor
, tmpfile
)) != 0:
176 raise YapError("Editing commit message failed")
178 commit
= get_output("git commit-tree '%s' -p '%s' < '%s'" % (tree
, parent
, tmpfile
))
180 commit
= get_output("git commit-tree '%s' < '%s'" % (tree
, tmpfile
))
182 raise YapError("Commit failed; no log message?")
184 os
.system("git update-ref HEAD '%s'" % commit
[0])
186 def cmd_version(self
):
187 print "Yap version 0.1"
190 print >> sys
.stderr
, "usage: %s <command>" % sys
.argv
[0]
191 print >> sys
.stderr
, " valid commands: version"
193 def main(self
, args
):
201 debug
= os
.getenv('YAP_DEBUG')
204 meth
= self
.__getattribute
__("cmd_"+command
)
206 if "option" in meth
.__dict
__:
207 flags
, args
= getopt
.getopt(args
, meth
.options
)
213 except (TypeError, getopt
.GetoptError
):
216 print "%s %s %s" % (sys
.argv
[0], command
, meth
.__doc
__)
218 print >> sys
.stderr
, e
220 except AttributeError: