Skip to content

Instantly share code, notes, and snippets.

@avar
Created August 23, 2012 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avar/3440792 to your computer and use it in GitHub Desktop.
Save avar/3440792 to your computer and use it in GitHub Desktop.
A utility to commit untracked files in repository A to repository B
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw(:all);
use Data::Dumper;
use Getopt::Long;
# How to run this:
# 1. Clone this gist into ~/g/gist-git-untracked/
# 2. git init ~/g/to-repository
# 3. Git clone git.git in ~/g/
# 4. perl ~/g/gist-git-untracked/git-untracked-file-committer.pl --from-repository ~/g/git --to-repository ~/g/to-repository --tagname=some-tag
#
GetOptions(
'from-repository=s' => \(my $from_repository),
'to-repository=s' => \(my $to_repository),
'tagname=s' => \(my $tagname),
);
# Get the list of untracked files for this repo
chdir $from_repository;
my $untracked_files = list_untracked_files();
# Go to the target repository, nuke anything already there
chdir $to_repository;
system "git reset --hard";
system "git clean -dxf";
system "git ls-tree --name-only HEAD -z | xargs -0 rm -rf";
system "git add --update"; # stage any removals
# Copy over the untracked files from the other repository. Maybe doing
# this with something other than tar is better.
chdir $from_repository;
print STDERR "Copied " . @$untracked_files . " from $from_repository to $to_repository\n";
copy_untracked_files_to_repository($untracked_files, $to_repository);
# Unpack the new files
chdir $to_repository;
system "tar xvf incoming.tar";
system "rm incoming.tar";
system "git add * .??* || :"; # Might die if we empty the repo, TODO: make this use status -> add each file
system "git commit -m'Bump copy from $from_repository to $to_repository' || :"; # We might have nothing to change!
# Now we have everything committed, make a new tag. Assume that we've
# already created a $tagname tag in the original one.
system "git tag -a -m'Generated tag to go with $tagname' generated-$tagname";
# TODO: Add the code to combine the two repositories later on.
sub copy_untracked_files_to_repository {
my ($untracked_files, $to_repository) = @_;
my $untracked_files_by_null = join "\0", @$untracked_files;
open my $tar, "| tar --null -cf $to_repository/incoming.tar -T -";
print $tar $untracked_files_by_null;
close $tar;
}
sub list_untracked_files {
open my $fh, "git status -s --porcelain --untracked-files=all --ignored |";
my @files;
while (my $line = <$fh>) {
chomp $line;
die "We shouldn't have any untracked files"
unless $line =~ s/^!! //;
push @files => $line;
}
close $fh;
#print STDERR "We got '$_' as an untracked file\n" for @files;
return \@files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment