Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for git clean #48

Merged
merged 1 commit into from
Nov 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/clean/a.coffee
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions test/fixtures/clean/b.coffee
Original file line number Diff line number Diff line change
@@ -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"
44 changes: 44 additions & 0 deletions test/fixtures/clean/d.js
Original file line number Diff line number Diff line change
@@ -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;
})();
1 change: 1 addition & 0 deletions test/fixtures/clean/git.git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
5 changes: 5 additions & 0 deletions test/fixtures/clean/git.git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
Binary file added test/fixtures/clean/git.git/index
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
xm��
�@D��+�����V��(�YX_���,9�l@�z�@
�ݝ�7SJ(am�xD�;,;�=��B�7@]a<l�ug�$�Bl��aMi`�xn �{_����iF�X�?��OnA�Td�8��C�qR�Uc!cX<w�:���6���3�����Dg
Expand Down
Binary file not shown.
2 changes: 2 additions & 0 deletions test/fixtures/clean/git.git/objects/info/packs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
P pack-70f08ce8f755306c59b6d804345abf334b5586f8.pack

Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/clean/git.git/refs/heads/f-gitCheckoutFiles
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
40216d142a8bf81e327f510990f2357dfd404486
1 change: 1 addition & 0 deletions test/fixtures/clean/git.git/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bdc76865cfc1d089ee4e238035a5860357c36506
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/clean/rawr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
71 changes: 71 additions & 0 deletions test/repo.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,77 @@ describe "Repo", ->
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"
Expand Down