123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #!/usr/bin/python -u
- #
- # dulwich - Simple command-line interface to Dulwich
- # Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@samba.org>
- # vim: expandtab
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; version 2
- # or (at your option) a later version of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301, USA.
- """Simple command-line interface to Dulwich>
- This is a very simple command-line wrapper for Dulwich. It is by
- no means intended to be a full-blown Git command-line interface but just
- a way to test Dulwich.
- """
- import os
- import sys
- from getopt import getopt
- import optparse
- import signal
- def signal_int(signal, frame):
- sys.exit(1)
- signal.signal(signal.SIGINT, signal_int)
- from dulwich import porcelain
- from dulwich.client import get_transport_and_path
- from dulwich.errors import ApplyDeltaError
- from dulwich.index import Index
- from dulwich.pack import Pack, sha_to_hex
- from dulwich.patch import write_tree_diff
- from dulwich.repo import Repo
- def cmd_archive(args):
- opts, args = getopt(args, "", [])
- client, path = get_transport_and_path(args.pop(0))
- location = args.pop(0)
- committish = args.pop(0)
- porcelain.archive(location, committish, outstream=sys.stdout,
- errstream=sys.stderr)
- def cmd_add(args):
- opts, args = getopt(args, "", [])
- porcelain.add(".", paths=args)
- def cmd_rm(args):
- opts, args = getopt(args, "", [])
- porcelain.rm(".", paths=args)
- def cmd_fetch_pack(args):
- opts, args = getopt(args, "", ["all"])
- opts = dict(opts)
- client, path = get_transport_and_path(args.pop(0))
- r = Repo(".")
- if "--all" in opts:
- determine_wants = r.object_store.determine_wants_all
- else:
- determine_wants = lambda x: [y for y in args if not y in r.object_store]
- client.fetch(path, r, determine_wants)
- def cmd_fetch(args):
- opts, args = getopt(args, "", [])
- opts = dict(opts)
- client, path = get_transport_and_path(args.pop(0))
- r = Repo(".")
- if "--all" in opts:
- determine_wants = r.object_store.determine_wants_all
- refs = client.fetch(path, r, progress=sys.stdout.write)
- print("Remote refs:")
- for item in refs.items():
- print("%s -> %s" % item)
- def cmd_log(args):
- opts, args = getopt(args, "", [])
- if len(args) > 0:
- path = args.pop(0)
- else:
- path = "."
- porcelain.log(repo=path, outstream=sys.stdout)
- def cmd_diff(args):
- opts, args = getopt(args, "", [])
- if args == []:
- print("Usage: dulwich diff COMMITID")
- sys.exit(1)
- r = Repo(".")
- commit_id = args[0]
- commit = r[commit_id]
- parent_commit = r[commit.parents[0]]
- write_tree_diff(sys.stdout, r.object_store, parent_commit.tree, commit.tree)
- def cmd_dump_pack(args):
- opts, args = getopt(args, "", [])
- if args == []:
- print("Usage: dulwich dump-pack FILENAME")
- sys.exit(1)
- basename, _ = os.path.splitext(args[0])
- x = Pack(basename)
- print("Object names checksum: %s" % x.name())
- print("Checksum: %s" % sha_to_hex(x.get_stored_checksum()))
- if not x.check():
- print("CHECKSUM DOES NOT MATCH")
- print("Length: %d" % len(x))
- for name in x:
- try:
- print("\t%s" % x[name])
- except KeyError as k:
- print("\t%s: Unable to resolve base %s" % (name, k))
- except ApplyDeltaError as e:
- print("\t%s: Unable to apply delta: %r" % (name, e))
- def cmd_dump_index(args):
- opts, args = getopt(args, "", [])
- if args == []:
- print("Usage: dulwich dump-index FILENAME")
- sys.exit(1)
- filename = args[0]
- idx = Index(filename)
- for o in idx:
- print(o, idx[o])
- def cmd_init(args):
- opts, args = getopt(args, "", ["bare"])
- opts = dict(opts)
- if args == []:
- path = os.getcwd()
- else:
- path = args[0]
- porcelain.init(path, bare=("--bare" in opts))
- def cmd_clone(args):
- opts, args = getopt(args, "", ["bare"])
- opts = dict(opts)
- if args == []:
- print("usage: dulwich clone host:path [PATH]")
- sys.exit(1)
- source = args.pop(0)
- if len(args) > 0:
- target = args.pop(0)
- else:
- target = None
- porcelain.clone(source, target, bare=("--bare" in opts))
- def cmd_commit(args):
- opts, args = getopt(args, "", ["message"])
- opts = dict(opts)
- porcelain.commit(".", message=opts["--message"])
- def cmd_commit_tree(args):
- opts, args = getopt(args, "", ["message"])
- if args == []:
- print("usage: dulwich commit-tree tree")
- sys.exit(1)
- opts = dict(opts)
- porcelain.commit_tree(".", tree=args[0], message=opts["--message"])
- def cmd_update_server_info(args):
- porcelain.update_server_info(".")
- def cmd_symbolic_ref(args):
- opts, args = getopt(args, "", ["ref-name", "force"])
- if not args:
- print("Usage: dulwich symbolic-ref REF_NAME [--force]")
- sys.exit(1)
- ref_name = args.pop(0)
- porcelain.symbolic_ref(".", ref_name=ref_name, force='--force' in args)
- def cmd_show(args):
- opts, args = getopt(args, "", [])
- porcelain.show(".", args)
- def cmd_diff_tree(args):
- opts, args = getopt(args, "", [])
- if len(args) < 2:
- print("Usage: dulwich diff-tree OLD-TREE NEW-TREE")
- sys.exit(1)
- porcelain.diff_tree(".", args[0], args[1])
- def cmd_rev_list(args):
- opts, args = getopt(args, "", [])
- if len(args) < 1:
- print('Usage: dulwich rev-list COMMITID...')
- sys.exit(1)
- porcelain.rev_list('.', args)
- def cmd_tag(args):
- opts, args = getopt(args, '', [])
- if len(args) < 2:
- print('Usage: dulwich tag NAME')
- sys.exit(1)
- porcelain.tag('.', args[0])
- def cmd_repack(args):
- opts, args = getopt(args, "", [])
- opts = dict(opts)
- porcelain.repack('.')
- def cmd_reset(args):
- opts, args = getopt(args, "", ["hard", "soft", "mixed"])
- opts = dict(opts)
- mode = ""
- if "--hard" in opts:
- mode = "hard"
- elif "--soft" in opts:
- mode = "soft"
- elif "--mixed" in opts:
- mode = "mixed"
- porcelain.reset('.', mode=mode, *args)
- def cmd_daemon(args):
- from dulwich import log_utils
- from dulwich.protocol import TCP_GIT_PORT
- parser = optparse.OptionParser()
- parser.add_option("-l", "--listen_address", dest="listen_address",
- default="localhost",
- help="Binding IP address.")
- parser.add_option("-p", "--port", dest="port", type=int,
- default=TCP_GIT_PORT,
- help="Binding TCP port.")
- options, args = parser.parse_args(args)
- log_utils.default_logging_config()
- if len(args) >= 1:
- gitdir = args[0]
- else:
- gitdir = '.'
- from dulwich import porcelain
- porcelain.daemon(gitdir, address=options.listen_address,
- port=options.port)
- def cmd_web_daemon(args):
- from dulwich import log_utils
- parser = optparse.OptionParser()
- parser.add_option("-l", "--listen_address", dest="listen_address",
- default="",
- help="Binding IP address.")
- parser.add_option("-p", "--port", dest="port", type=int,
- default=8000,
- help="Binding TCP port.")
- options, args = parser.parse_args(args)
- log_utils.default_logging_config()
- if len(args) >= 1:
- gitdir = args[0]
- else:
- gitdir = '.'
- from dulwich import porcelain
- porcelain.web_daemon(gitdir, address=options.listen_address,
- port=options.port)
- def cmd_receive_pack(args):
- parser = optparse.OptionParser()
- options, args = parser.parse_args(args)
- if len(args) >= 1:
- gitdir = args[0]
- else:
- gitdir = '.'
- porcelain.receive_pack(gitdir)
- def cmd_upload_pack(args):
- parser = optparse.OptionParser()
- options, args = parser.parse_args(args)
- if len(args) >= 1:
- gitdir = args[0]
- else:
- gitdir = '.'
- porcelain.upload_pack(gitdir)
- def cmd_status(args):
- parser = optparse.OptionParser()
- options, args = parser.parse_args(args)
- if len(args) >= 1:
- gitdir = args[0]
- else:
- gitdir = '.'
- status = porcelain.status(gitdir)
- if status.staged:
- sys.stdout.write("Changes to be committed:\n\n")
- for kind, names in status.staged.items():
- for name in names:
- sys.stdout.write("\t%s: %s\n" % (kind, name))
- sys.stdout.write("\n")
- if status.unstaged:
- sys.stdout.write("Changes not staged for commit:\n\n")
- for name in status.unstaged:
- sys.stdout.write("\t%s\n" %
- name.decode(sys.getfilesystemencoding()))
- sys.stdout.write("\n")
- if status.untracked:
- sys.stdout.write("Untracked files:\n\n")
- for name in status.untracked:
- sys.stdout.write("\t%s\n" % name)
- sys.stdout.write("\n")
- def cmd_ls_remote(args):
- opts, args = getopt(args, '', [])
- if len(args) < 1:
- print('Usage: dulwich ls-remote URL')
- sys.exit(1)
- refs = porcelain.ls_remote(args[0])
- for ref in sorted(refs):
- sys.stdout.write("%s\t%s\n" % (ref, refs[ref]))
- def cmd_pack_objects(args):
- opts, args = getopt(args, '', ['stdout'])
- opts = dict(opts)
- if len(args) < 1 and not '--stdout' in args:
- print('Usage: dulwich pack-objects basename')
- sys.exit(1)
- object_ids = [l.strip() for l in sys.stdin.readlines()]
- basename = args[0]
- if '--stdout' in opts:
- packf = getattr(sys.stdout, 'buffer', sys.stdout)
- idxf = None
- close = []
- else:
- packf = open(basename + '.pack', 'w')
- idxf = open(basename + '.idx', 'w')
- close = [packf, idxf]
- porcelain.pack_objects('.', object_ids, packf, idxf)
- for f in close:
- f.close()
- commands = {
- "add": cmd_add,
- "archive": cmd_archive,
- "clone": cmd_clone,
- "commit": cmd_commit,
- "commit-tree": cmd_commit_tree,
- "daemon": cmd_daemon,
- "diff": cmd_diff,
- "diff-tree": cmd_diff_tree,
- "dump-pack": cmd_dump_pack,
- "dump-index": cmd_dump_index,
- "fetch-pack": cmd_fetch_pack,
- "fetch": cmd_fetch,
- "init": cmd_init,
- "log": cmd_log,
- "ls-remote": cmd_ls_remote,
- "pack-objects": cmd_pack_objects,
- "receive-pack": cmd_receive_pack,
- "repack": cmd_repack,
- "reset": cmd_reset,
- "rev-list": cmd_rev_list,
- "rm": cmd_rm,
- "show": cmd_show,
- "status": cmd_status,
- "symbolic-ref": cmd_symbolic_ref,
- "tag": cmd_tag,
- "update-server-info": cmd_update_server_info,
- "upload-pack": cmd_upload_pack,
- "web-daemon": cmd_web_daemon,
- }
- if len(sys.argv) < 2:
- print("Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys())))
- sys.exit(1)
- cmd = sys.argv[1]
- if not cmd in commands:
- print("No such subcommand: %s" % cmd)
- sys.exit(1)
- commands[cmd](sys.argv[2:])
|