diff --git a/src/repo.coffee b/src/repo.coffee index 9bcd5a4..404e6d9 100644 --- a/src/repo.coffee +++ b/src/repo.coffee @@ -355,6 +355,19 @@ module.exports = class Repo checkout: (treeish, callback) -> @git "checkout", {}, treeish, callback + # Public: Clean the git repo by removing untracked files + # + # options - The {Object} containing any of the options available to git clean: + # :force - {Boolean) In the default repo config, clean will not take effect unless this option is given. + # :d - {Boolean) also removes untracked directories + # :n - {Boolean) Dry run - don't actually delete, just report what would be deleted + # :quiet - {Boolean) only report errors + # callback - The {Function} to callback. + # + clean: (options, callback) -> + options ?= {} + @git "clean", options, callback + # Public: Reset the git repo. # # treeish - The {String} to reset to. diff --git a/test/fixtures/clean/a.coffee b/test/fixtures/clean/a.coffee new file mode 100644 index 0000000..0db65dd --- /dev/null +++ b/test/fixtures/clean/a.coffee @@ -0,0 +1,28 @@ +# Assignment: +number = 42 +opposite = true + +# Conditions: +number = -42 if opposite + +# Functions: +square = (x) -> x * x + +# Arrays: +list = [1, 2, 3, 4, 5] + +# Objects: +math = + root: Math.sqrt + square: square + cube: (x) -> x * square x + +# Splats: +race = (winner, runners...) -> + print winner, runners + +# Existence: +alert "I knew it!" if elvis? + +# Array comprehensions: +cubes = (math.cube num for num in list) diff --git a/test/fixtures/clean/b.coffee b/test/fixtures/clean/b.coffee new file mode 100644 index 0000000..3463c49 --- /dev/null +++ b/test/fixtures/clean/b.coffee @@ -0,0 +1,9 @@ +grade = (student) -> + if student.excellentWork + "A+" + else if student.okayStuff + if student.triedHard then "B" else "B-" + else + "C" + +eldest = if 24 > 21 then "Liz" else "Ike" diff --git a/test/fixtures/clean/d.js b/test/fixtures/clean/d.js new file mode 100644 index 0000000..83357ab --- /dev/null +++ b/test/fixtures/clean/d.js @@ -0,0 +1,44 @@ +var cubes, list, math, num, number, opposite, race, square, + __slice = [].slice; + +number = 42; + +opposite = true; + +if (opposite) { + number = -42; +} + +square = function(x) { + return x * x; +}; + +list = [1, 2, 3, 4, 5]; + +math = { + root: Math.sqrt, + square: square, + cube: function(x) { + return x * square(x); + } +}; + +race = function() { + var runners, winner; + winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; + return print(winner, runners); +}; + +if (typeof elvis !== "undefined" && elvis !== null) { + alert("I knew it!"); +} + +cubes = (function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = list.length; _i < _len; _i++) { + num = list[_i]; + _results.push(math.cube(num)); + } + return _results; +})(); diff --git a/test/fixtures/clean/git.git/HEAD b/test/fixtures/clean/git.git/HEAD new file mode 100644 index 0000000..cb089cd --- /dev/null +++ b/test/fixtures/clean/git.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/fixtures/clean/git.git/config b/test/fixtures/clean/git.git/config new file mode 100644 index 0000000..515f483 --- /dev/null +++ b/test/fixtures/clean/git.git/config @@ -0,0 +1,5 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true diff --git a/test/fixtures/clean/git.git/index b/test/fixtures/clean/git.git/index new file mode 100644 index 0000000..936ee3b Binary files /dev/null and b/test/fixtures/clean/git.git/index differ diff --git a/test/fixtures/clean/git.git/objects/14/e5a1a3a459e05d486b98df2802ee4b30c33995 b/test/fixtures/clean/git.git/objects/14/e5a1a3a459e05d486b98df2802ee4b30c33995 new file mode 100644 index 0000000..7ca6048 Binary files /dev/null and b/test/fixtures/clean/git.git/objects/14/e5a1a3a459e05d486b98df2802ee4b30c33995 differ diff --git a/test/fixtures/clean/git.git/objects/41/2d321c36a34331125a77493b5dd41f5313a568 b/test/fixtures/clean/git.git/objects/41/2d321c36a34331125a77493b5dd41f5313a568 new file mode 100644 index 0000000..e4969ec Binary files /dev/null and b/test/fixtures/clean/git.git/objects/41/2d321c36a34331125a77493b5dd41f5313a568 differ diff --git a/test/fixtures/clean/git.git/objects/55/80d8a9feeadf1c36a73a2778c62206314aebac b/test/fixtures/clean/git.git/objects/55/80d8a9feeadf1c36a73a2778c62206314aebac new file mode 100644 index 0000000..7d66d78 Binary files /dev/null and b/test/fixtures/clean/git.git/objects/55/80d8a9feeadf1c36a73a2778c62206314aebac differ diff --git a/test/fixtures/clean/git.git/objects/56/02c1d7079f5ff3f49f341722c6d5baf93bb26e b/test/fixtures/clean/git.git/objects/56/02c1d7079f5ff3f49f341722c6d5baf93bb26e new file mode 100644 index 0000000..23c64cd --- /dev/null +++ b/test/fixtures/clean/git.git/objects/56/02c1d7079f5ff3f49f341722c6d5baf93bb26e @@ -0,0 +1,3 @@ +xmŽ± +Â@D­ï+†­‰ÃVÁØ(ØYX_¼†,9¸l@ýz“@ +»ÝÙ7SJ(am¾xDç;,;í=·ºB¶7@]a should.exist err done() + describe "#clean", -> + repo = null + git_dir = __dirname + "/fixtures/junk_clean" + status = null + file = "bla.txt" + dir = 'blah' + + # given a fresh new repo + beforeEach (done) -> + status = null + fs.remove git_dir, (err) -> + return done err if err + fs.copy "#{__dirname}/fixtures/clean", "#{git_dir}", (err) -> + return done err if err + fs.rename "#{git_dir}/git.git", "#{git_dir}/.git", (err) -> + return done err if err + git.init git_dir, (err) -> + repo = git git_dir + fs.writeFile "#{git_dir}/#{file}", "hello", (err) -> + return done err if err? + fs.mkdir "#{git_dir}/#{dir}", (err) -> + done err + + after (done) -> + fs.remove git_dir, (err) -> + done err + + describe "clean with no args shouldn't do anything", -> + beforeEach (done) -> + repo.clean -> + repo.status (err, _status) -> + status = _status + done err + + it "leaves the untracked file alone", -> + fs.existsSync("#{git_dir}/iamuntracked").should.be.true + fs.existsSync("#{git_dir}/iamuntracked/untracked.txt").should.be.true + fs.existsSync("#{git_dir}/#{dir}").should.be.true + status.files.should.have.a.property file + status.files[file].staged.should.be.false + status.files[file].tracked.should.be.false + status.files[file].should.not.have.a.property 'type' + + describe "clean --force", -> + beforeEach (done) -> + repo.clean {force: true}, -> + repo.status (err, _status) -> + status = _status + done err + + it "should remove the file but not the directory", -> + status.files.should.not.have.a.property file + fs.existsSync("#{git_dir}/#{dir}").should.be.true + fs.existsSync("#{git_dir}/iamuntracked").should.be.true + + # git does not clean untracked files in untracked directories + fs.existsSync("#{git_dir}/iamuntracked/untracked.txt").should.be.true + + describe "clean -df", -> + beforeEach (done) -> + repo.clean {force: true, d: true}, -> + repo.status (err, _status) -> + status = _status + done err + + it "removes the file and directory", -> + status.files.should.not.have.a.property file + fs.existsSync("#{git_dir}/#{dir}").should.be.false + fs.existsSync("#{git_dir}/iamuntracked").should.be.false + fs.existsSync("#{git_dir}/iamuntracked/untracked.txt").should.be.false + describe "#reset", -> repo = null git_dir = __dirname + "/fixtures/junk_reset"