8 output
= fd
.readlines()
10 return [x
.strip() for x
in output
]
13 rc
= os
.system("%s > /dev/null 2>&1" % cmd
)
17 class YapError(Exception):
18 def __init__(self
, msg
):
24 def takes_options(options
):
26 func
.options
= options
31 def _add_new_file(self
, file):
32 repo
= get_output('git rev-parse --git-dir')[0]
33 dir = os
.path
.join(repo
, 'yap')
38 files
= self
._get
_new
_files
()
40 path
= os
.path
.join(dir, 'new-files')
41 pickle
.dump(files
, open(path
, 'w'))
43 def _get_new_files(self
):
44 repo
= get_output('git rev-parse --git-dir')[0]
45 path
= os
.path
.join(repo
, 'yap', 'new-files')
47 files
= pickle
.load(file(path
))
54 if get_output("git ls-files --cached '%s'" % f
) != []:
59 def _remove_new_file(self
, file):
60 files
= self
._get
_new
_files
()
61 files
= filter(lambda x
: x
!= file, files
)
63 repo
= get_output('git rev-parse --git-dir')[0]
64 path
= os
.path
.join(repo
, 'yap', 'new-files')
65 pickle
.dump(files
, open(path
, 'w'))
67 def _clear_new_files(self
):
68 repo
= get_output('git rev-parse --git-dir')[0]
69 path
= os
.path
.join(repo
, 'yap', 'new-files')
72 def _assert_file_exists(self
, file):
73 if not os
.access(file, os
.R_OK
):
74 raise YapError("No such file: %s" % file)
76 def cmd_clone(self
, url
, directory
=""):
77 # XXX: implement in terms of init + remote add + fetch
78 os
.system("git clone '%s' %s" % (url
, directory
))
83 def cmd_add(self
, file):
84 self
._assert
_file
_exists
(file)
85 x
= get_output("git ls-files '%s'" % file)
87 raise YapError("File '%s' already in repository" % file)
88 self
._add
_new
_file
(file)
91 def cmd_rm(self
, file):
92 self
._assert
_file
_exists
(file)
93 if get_output("git ls-files '%s'" % file) != []:
94 os
.system("git rm --cached '%s'" % file)
95 self
._remove
_new
_file
(file)
98 def cmd_stage(self
, file):
99 self
._assert
_file
_exists
(file)
100 os
.system("git update-index --add '%s'" % file)
103 def cmd_unstage(self
, file):
104 self
._assert
_file
_exists
(file)
105 if run_command("git rev-parse HEAD"):
106 os
.system("git update-index --force-remove '%s'" % file)
108 os
.system("git diff-index HEAD '%s' | git apply -R --cached" % file)
111 def cmd_status(self
):
112 branch
= get_output("git symbolic-ref HEAD")[0]
113 branch
= branch
.replace('refs/heads/', '')
114 print "Current branch: %s" % branch
116 print "Files with staged changes:"
117 if run_command("git rev-parse HEAD"):
118 files
= get_output("git ls-files --cached")
120 files
= get_output("git diff-index --name-only HEAD")
126 print "Files with unstages changes:"
127 files
= self
._get
_new
_files
()
128 files
+= get_output("git ls-files -m")
134 def cmd_unedit(self
, file):
135 self
._assert
_file
_exists
(file)
136 os
.system("git checkout-index -f '%s'" % file)
139 def cmd_version(self
):
140 print "Yap version 0.1"
143 print >> sys
.stderr
, "usage: %s <command>" % sys
.argv
[0]
144 print >> sys
.stderr
, " valid commands: version"
146 def main(self
, args
):
154 debug
= os
.getenv('YAP_DEBUG')
157 meth
= self
.__getattribute
__("cmd_"+command
)
159 if "option" in meth
.__dict
__:
160 flags
, args
= getopt
.getopt(args
, meth
.options
)
166 except (TypeError, getopt
.GetoptError
):
169 print "%s %s %s" % (sys
.argv
[0], command
, meth
.__doc
__)
171 print >> sys
.stderr
, e
173 except AttributeError: