I’ve been working wit git lately but I have also missed the darcs user interface. I honestly think the darcs user interface is the best I’ve ever seen, it’s such a joy to record/push/pull (when darcs doesn’t eat your cpu) 🙂
I looked at git add --interactive
because it had hunk-based commit, a pre-requisite for darcs record
-style commit, but it has a terrible user interface, so i just copied the concept: running a git diff
, filtering hunks, and then outputing the filtered diff through git apply --cached
.
It supports binary diffs, file additions and removal. It also asks for new files to be added even if this is not exactly how darcs behave but I always forget to add new files, so I added it. It will probably break on some extreme corner cases I haven’t been confronted to, but I gladly accept any patches 🙂
Here’s a sample session of git-darcs-record script
:
$ git-darcs-record Add file: newfile.txt Shall I add this file? (1/1) [Ynda] : y Binary file changed: document.pdf Shall I record this change? (1/7) [Ynda] : y foobar.txt @@ -1,3 +1,5 @@ line1 line2 +line3 line4 +line5 Shall I record this change? (2/7) [Ynda] : y git-darcs-record @@ -1,17 +1,5 @@ #!/usr/bin/env python -# git-darcs-record, emulate "darcs record" interface on top of a git repository -# -# Usage: -# git-darcs-record first asks for any new file (previously -# untracked) to be added to the index. -# git-darcs-record then asks for each hunk to be recorded in -# the next commit. File deletion and binary blobs are supported -# git-darcs-record finally asks for a small commit message and -# executes the 'git commit' command with the newly created -# changeset in the index - - # Copyright (C) 2007 Raphaël Slinckx # # This program is free software; you can redistribute it and/or Shall I record this change? (3/7) [Ynda] : y git-darcs-record @@ -28,6 +16,19 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# git-darcs-record, emulate "darcs record" interface on top of a git repository +# +# Usage: +# git-darcs-record first asks for any new file (previously +# untracked) to be added to the index. +# git-darcs-record then asks for each hunk to be recorded in +# the next commit. File deletion and binary blobs are supported +# git-darcs-record finally asks for a small commit message and +# executes the 'git commit' command with the newly created +# changeset in the index + + + import re, pprint, sys, os BINARY = re.compile("GIT binary patch") Shall I record this change? (4/7) [Ynda] : n git-darcs-record @@ -151,16 +152,6 @@ def read_answer(question, allowed_responses=["Y", "n", "d", "a"]): return resp -def setup_git_dir(): - global GIT_DIR - GIT_DIR = os.getcwd() - while not os.path.exists(os.path.join(GIT_DIR, ".git")): - GIT_DIR = os.path.dirname(GIT_DIR) - if GIT_DIR == "/": - return False - os.chdir(GIT_DIR) - return True - def git_get_untracked_files(): Shall I record this change? (5/7) [Ynda] : y # On branch master # Changes to be committed: # (use "git reset HEAD file..." to unstage) # # modified: document.pdf # modified: foobar.txt # modified: git-darcs-record # new file: newfile.txt # # Changed but not updated: # (use "git add file file..." to update what will be committed) # # modified: git-darcs-record # What is the patch name? Some cute patch name Created commit a08f34e: Some cute patch name 4 files changed, 3 insertions(+), 29 deletions(-) create mode 100644 newfile.txt
Get the script here: git-darcs-record script and put in somewhere in your $PATH. Any comments or improvements is welcome !
Comments
10 responses to “git commit / darcs record”
Nice script! But, someone I know wrote a similar script, that emulates a lot more of the darcs interface on top of git.
http://ftp.frugalware.org/pub/other/pacman-tools/pacman-tools/darcs-git.py
http://ftp.frugalware.org/pub/other/pacman-tools/pacman-tools/darcs-git.txt
Looks interesting enough. I just switched from svn, but I think I’m going to try it. What I find confusing (not having darcs background) is what does Ynda stand for. Y and n I can guess, but d and a? Reading the patch also doesn’t enlighten me 🙁
Y = yes, select this hunk
n = no, don’t select this hunk
d = done, answer no to any remaining patches
a = all, answer yes to any remaining patches
Y is default action if you hit enter.
What about f and s?
Tester: I never use them, but they could be trivially added 😉
Cool! Does it do the colours too? (particularly the red $ sign after trailing spaces)
No colors undortunately. It would require re-parsing the diff output. Or finding a way to request a colored diff from git along with the uncolored one that is parsable
Re: “It also asks for new files to be added even if this is not exactly how darcs behave” …
Actually, this is exactly how my darcs behaves. 🙂 Are you aware of the “–look-for-adds” option? I turned it on for exactly the reason you state — with CVS, I was tired of missing new files I forgot to explicitly add.
(In fact, my SCM path has been CVS –> GNU arch –> darcs, and I instated my “all files must be added unless explicitly ignored” policy very early in my “arch” usage. Switching to darcs just meant I could relax that to “all files will be prompted for adding unless explicitly ignored”. Nicer to use, but with the same result.)
To make this a global default, I just stuck these lines into my ~/.darcs/defaults:
record look-for-adds
whatsnew look-for-adds
Beware, though — if you ever use the “tailor” utility (http://progetti.arstecnica.it/tailor/) to convert to/from darcs, you should be careful to *disable* this in the tailor’ed darcs tree. In the case of darcs as a ‘tailor’ target, it issues ‘remove’ commands but does *not* delete the files in question from the tree. Leaving ‘look-for-adds’ on will silently fail to record deletions, with all the consequences that entails.
Thanks for this writeup. I’m just getting started with git, but it seems incredible.
I just discovered git add –patch, which is a similar and built right into git:
-p, –patch
Similar to Interactive mode but the initial command loop is bypassed and the patch subcommand is
invoked using each of the specified filepatterns before exiting.