Merge subdirectory from another git repository

Let’s say I have two repositories: cvhci-standards and labeltracks.

cvhci-standards

matlab/
...
python/
__init__.py
cvhcistandards.py
cpp/
...
README

labeltracks

labeltracks.py
utilities/
...

I’d like to merge the content of subdirectory python from cvhci-standards into labelstracks/utilities/cvhcistandards. To do this, we use git read-tree, with the following steps:

1. Add remote repository and fetch it:

git remote add -f cvhci-standards git://git.kit.edu:cvhci-standards.git

2. Merge, but don’t commit just yet:

git merge -s ours --no-commit cvhci-standards/master

3. Read a specific subdirectory from cvhci-standards. We specify the the target subdirectory as prefix, and the source-subdirectory (python) after the colon in the tree-ish repository description.

git read-tree --prefix=utilities/cvhcistandards/ -u cvhci-standards/master:python

4. Commit

git commit -m 'merging in cvhci-standards python module'

Later:
5. Pull from cvhci-standards to update the subtree.

git pull -s subtree -X subtree=utilities/cvhcistandards cvhci-standards master

We need to specify the subdirectory to merge to, otherwise git can’t match the subtrees correctly.

Advertisements

Sort strings with numbers naturally with GNU sort

Say you have a list of filenames or identifiers with numbers, e.g.

unknowns.txt:

unknown_E1_2
unknown_E1_1
unknown_E2_45
unknown_E10_34
unknown_E1_10
unknown_E1_23

and you want to sort them “naturally”, e.g. unknown_E1_2 should come before unknown_E1_10. If you would just sort them as strings (character by character), 10 would come before 2, because the character ‘1’ comes before ‘2’, and the trailing ‘0’ just doesn’t matter for the sort order anymore.

sort from the GNU coreutils let’s you do this easily with the -V switch. Note that this not only works for numbers at the end of a string, but also for the numbers in the middle:

# sort -V unknowns.txt
unknown_E1_1
unknown_E1_2
unknown_E1_10
unknown_E1_23
unknown_E2_45
unknown_E10_34

See Ned Batchfelder’s blog post on how to do this in Python. Especially check out Toothy’s comment for a brief solution.